omise 0.4.0 → 0.5.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (52) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +9 -0
  3. data/bin/console +9 -0
  4. data/bin/rake +17 -0
  5. data/lib/omise/all.rb +3 -0
  6. data/lib/omise/attributes.rb +12 -3
  7. data/lib/omise/card_list.rb +1 -6
  8. data/lib/omise/charge.rb +5 -0
  9. data/lib/omise/charge_list.rb +10 -0
  10. data/lib/omise/customer.rb +12 -2
  11. data/lib/omise/dispute.rb +10 -0
  12. data/lib/omise/document.rb +15 -0
  13. data/lib/omise/document_list.rb +15 -0
  14. data/lib/omise/error.rb +3 -5
  15. data/lib/omise/link.rb +37 -0
  16. data/lib/omise/list.rb +53 -2
  17. data/lib/omise/recipient.rb +5 -0
  18. data/lib/omise/refund.rb +4 -0
  19. data/lib/omise/refund_list.rb +0 -5
  20. data/lib/omise/resource.rb +27 -13
  21. data/lib/omise/search.rb +9 -0
  22. data/lib/omise/search_scope.rb +59 -0
  23. data/lib/omise/testing/resource.rb +1 -1
  24. data/lib/omise/transfer.rb +4 -0
  25. data/lib/omise/util.rb +44 -1
  26. data/lib/omise/version.rb +1 -1
  27. data/omise.gemspec +2 -1
  28. data/test/fixtures/api.omise.co/charges-get-limit-20-offset-0.json +59 -0
  29. data/test/fixtures/api.omise.co/charges-get-limit-20-offset-20.json +59 -0
  30. data/test/fixtures/api.omise.co/disputes/dspt_test_5089off452g5m5te7xs/documents-get.json +19 -0
  31. data/test/fixtures/api.omise.co/disputes/dspt_test_5089off452g5m5te7xs/documents-post.json +7 -0
  32. data/test/fixtures/api.omise.co/disputes/dspt_test_5089off452g5m5te7xs/documents/docu_test_55869onwfm2g3bsw8d8-delete.json +6 -0
  33. data/test/fixtures/api.omise.co/disputes/dspt_test_5089off452g5m5te7xs/documents/docu_test_55869onwfm2g3bsw8d8-get.json +7 -0
  34. data/test/fixtures/api.omise.co/links-get.json +94 -0
  35. data/test/fixtures/api.omise.co/links-post.json +82 -0
  36. data/test/fixtures/api.omise.co/links/link_test_55pcclmznvrv9lc7r9s-get.json +82 -0
  37. data/test/fixtures/api.omise.co/search-get-scope-charge.json +62 -0
  38. data/test/omise/test_attributes.rb +79 -0
  39. data/test/omise/test_charge.rb +5 -0
  40. data/test/omise/test_customer.rb +11 -0
  41. data/test/omise/test_dispute.rb +9 -0
  42. data/test/omise/test_document.rb +33 -0
  43. data/test/omise/test_link.rb +36 -0
  44. data/test/omise/test_list.rb +101 -0
  45. data/test/omise/test_recipient.rb +5 -0
  46. data/test/omise/test_resource.rb +2 -1
  47. data/test/omise/test_search.rb +10 -0
  48. data/test/omise/test_search_scope.rb +138 -0
  49. data/test/omise/test_transfer.rb +1 -1
  50. data/test/omise/test_util.rb +65 -0
  51. data/test/support.rb +2 -2
  52. metadata +59 -4
@@ -0,0 +1,9 @@
1
+ require "omise/list"
2
+
3
+ module Omise
4
+ class Search < List
5
+ def self.execute(attributes = {})
6
+ new resource("/search", attributes).get(attributes)
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,59 @@
1
+ require "omise/search"
2
+
3
+ module Omise
4
+ class SearchScope
5
+ def initialize(scope, options = {})
6
+ @scope = scope.to_s
7
+ @filters = options.delete(:filters) { Hash.new }
8
+ @order = options.delete(:order)
9
+ @page = options.delete(:page)
10
+ @query = options.delete(:query)
11
+ end
12
+
13
+ attr_reader :scope
14
+
15
+ def filter(filters = {})
16
+ renew(filters: @filters.merge(filters))
17
+ end
18
+
19
+ def query(terms = nil)
20
+ return self if !terms
21
+ renew(query: terms)
22
+ end
23
+
24
+ def order(order)
25
+ renew(order: order)
26
+ end
27
+
28
+ def page(number)
29
+ renew(page: number)
30
+ end
31
+
32
+ def execute
33
+ Search.execute(to_attributes)
34
+ end
35
+
36
+ def to_attributes
37
+ attributes = {}
38
+
39
+ attributes[:scope] = @scope
40
+ attributes[:filters] = @filters if @filters.any?
41
+ attributes[:query] = @query if @query
42
+ attributes[:order] = @order if @order
43
+ attributes[:page] = @page if @page
44
+
45
+ attributes
46
+ end
47
+
48
+ private
49
+
50
+ def renew(attributes)
51
+ self.class.new(@scope, {
52
+ query: @query,
53
+ filters: @filters,
54
+ order: @order,
55
+ page: @page,
56
+ }.merge(attributes))
57
+ end
58
+ end
59
+ end
@@ -31,7 +31,7 @@ module Omise
31
31
 
32
32
  def generate_path(verb, attributes)
33
33
  return verb if attributes.empty?
34
- params = attributes.to_a.sort { |x,y| x.first <=> x.last }.flatten.join("-")
34
+ params = attributes.to_a.sort { |x,y| x.first.to_s <=> y.first.to_s }.flatten.join("-")
35
35
  [verb, params].compact.join("-")
36
36
  end
37
37
 
@@ -7,6 +7,10 @@ module Omise
7
7
  class Transfer < OmiseObject
8
8
  self.endpoint = "/transfers"
9
9
 
10
+ def self.search
11
+ SearchScope.new(:transfer)
12
+ end
13
+
10
14
  def self.create(attributes = {})
11
15
  new resource(location, attributes).post(attributes)
12
16
  end
@@ -1,3 +1,4 @@
1
+ require "cgi"
1
2
  require "json"
2
3
 
3
4
  require "omise/object"
@@ -18,8 +19,50 @@ module Omise
18
19
 
19
20
  def load_response(response)
20
21
  object = JSON.load(response)
21
- raise Omise::Error.new(object) if object["object"] == "error"
22
+
23
+ if object["object"] == "error"
24
+ raise Omise::Error, object
25
+ end
26
+
22
27
  object
23
28
  end
29
+
30
+ def generate_query(object, namespace = nil)
31
+ if object.is_a?(Hash)
32
+ return object.map do |key, value|
33
+ unless (value.is_a?(Hash) || value.is_a?(Array)) && value.empty?
34
+ generate_query(value, namespace ? "#{namespace}[#{key}]" : key)
35
+ end
36
+ end.compact.sort! * "&"
37
+ end
38
+
39
+ if object.is_a?(Array)
40
+ prefix = "#{namespace}[]"
41
+
42
+ if object.empty?
43
+ return generate_query(nil, prefix)
44
+ else
45
+ return object.map { |value| generate_query(value, prefix) }.join("&")
46
+ end
47
+ end
48
+
49
+ "#{CGI.escape(generate_param(namespace))}=#{CGI.escape(generate_param(object).to_s)}"
50
+ end
51
+
52
+ def generate_param(object)
53
+ if object.is_a?(Hash)
54
+ return generate_query(object)
55
+ end
56
+
57
+ if object.is_a?(Array)
58
+ return object.map { |o| generate_param(o) }.join("/")
59
+ end
60
+
61
+ if object.is_a?(NilClass) || object.is_a?(TrueClass) || object.is_a?(FalseClass)
62
+ return object
63
+ end
64
+
65
+ object.to_s
66
+ end
24
67
  end
25
68
  end
@@ -1,3 +1,3 @@
1
1
  module Omise
2
- VERSION = "0.4.0"
2
+ VERSION = "0.5.0"
3
3
  end
@@ -13,7 +13,7 @@ Gem::Specification.new do |spec|
13
13
  spec.license = "MIT"
14
14
 
15
15
  spec.files = `git ls-files -z`.split("\x0")
16
- spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
16
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
17
17
  spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
18
18
  spec.require_paths = ["lib"]
19
19
 
@@ -22,5 +22,6 @@ Gem::Specification.new do |spec|
22
22
 
23
23
  spec.add_development_dependency "bundler"
24
24
  spec.add_development_dependency "rake"
25
+ spec.add_development_dependency "pry"
25
26
  spec.add_development_dependency "minitest", "~> 5.4.2"
26
27
  end
@@ -0,0 +1,59 @@
1
+ {
2
+ "object": "list",
3
+ "from": "1970-01-01T00:00:00+00:00",
4
+ "to": "2015-01-15T05:04:30+00:00",
5
+ "offset": 0,
6
+ "limit": 20,
7
+ "total": 2,
8
+ "data": [
9
+ {
10
+ "object": "charge",
11
+ "id": "chrg_test_4yq7duw15p9hdrjp8oq",
12
+ "livemode": false,
13
+ "location": "/charges/chrg_test_4yq7duw15p9hdrjp8oq",
14
+ "amount": 100000,
15
+ "currency": "thb",
16
+ "description": "Charge for order 3947",
17
+ "capture": true,
18
+ "authorized": true,
19
+ "captured": true,
20
+ "transaction": "trxn_test_4yq7duwb9jts1vxgqua",
21
+ "refunded": 0,
22
+ "refunds": {
23
+ "object": "list",
24
+ "from": "1970-01-01T00:00:00+00:00",
25
+ "to": "2015-01-15T05:04:30+00:00",
26
+ "offset": 0,
27
+ "limit": 20,
28
+ "total": 0,
29
+ "data": [
30
+
31
+ ],
32
+ "location": "/charges/chrg_test_4yq7duw15p9hdrjp8oq/refunds"
33
+ },
34
+ "failure_code": null,
35
+ "failure_message": null,
36
+ "card": {
37
+ "object": "card",
38
+ "id": "card_test_4yq6tuucl9h4erukfl0",
39
+ "livemode": false,
40
+ "location": "/customers/cust_test_4yq6txdpfadhbaqnwp3/cards/card_test_4yq6tuucl9h4erukfl0",
41
+ "country": "",
42
+ "city": "Bangkok",
43
+ "postal_code": "10320",
44
+ "financing": "",
45
+ "last_digits": "4242",
46
+ "brand": "Visa",
47
+ "expiration_month": 1,
48
+ "expiration_year": 2017,
49
+ "fingerprint": "sRF/oMw2UQJJp/WbU+2/ZbVzwROjpMf1lyhOHhOqziw=",
50
+ "name": "JOHN DOE",
51
+ "security_code_check": true,
52
+ "created": "2015-01-15T04:03:40Z"
53
+ },
54
+ "customer": "cust_test_4yq6txdpfadhbaqnwp3",
55
+ "ip": null,
56
+ "created": "2015-01-15T05:00:29Z"
57
+ }
58
+ ]
59
+ }
@@ -0,0 +1,59 @@
1
+ {
2
+ "object": "list",
3
+ "from": "1970-01-01T00:00:00+00:00",
4
+ "to": "2015-01-15T05:04:30+00:00",
5
+ "offset": 0,
6
+ "limit": 20,
7
+ "total": 2,
8
+ "data": [
9
+ {
10
+ "object": "charge",
11
+ "id": "chrg_test_4yq7duw15p9hdrjp8oq",
12
+ "livemode": false,
13
+ "location": "/charges/chrg_test_4yq7duw15p9hdrjp8oq",
14
+ "amount": 100000,
15
+ "currency": "thb",
16
+ "description": "Charge for order 3947",
17
+ "capture": true,
18
+ "authorized": true,
19
+ "captured": true,
20
+ "transaction": "trxn_test_4yq7duwb9jts1vxgqua",
21
+ "refunded": 0,
22
+ "refunds": {
23
+ "object": "list",
24
+ "from": "1970-01-01T00:00:00+00:00",
25
+ "to": "2015-01-15T05:04:30+00:00",
26
+ "offset": 0,
27
+ "limit": 20,
28
+ "total": 0,
29
+ "data": [
30
+
31
+ ],
32
+ "location": "/charges/chrg_test_4yq7duw15p9hdrjp8oq/refunds"
33
+ },
34
+ "failure_code": null,
35
+ "failure_message": null,
36
+ "card": {
37
+ "object": "card",
38
+ "id": "card_test_4yq6tuucl9h4erukfl0",
39
+ "livemode": false,
40
+ "location": "/customers/cust_test_4yq6txdpfadhbaqnwp3/cards/card_test_4yq6tuucl9h4erukfl0",
41
+ "country": "",
42
+ "city": "Bangkok",
43
+ "postal_code": "10320",
44
+ "financing": "",
45
+ "last_digits": "4242",
46
+ "brand": "Visa",
47
+ "expiration_month": 1,
48
+ "expiration_year": 2017,
49
+ "fingerprint": "sRF/oMw2UQJJp/WbU+2/ZbVzwROjpMf1lyhOHhOqziw=",
50
+ "name": "JOHN DOE",
51
+ "security_code_check": true,
52
+ "created": "2015-01-15T04:03:40Z"
53
+ },
54
+ "customer": "cust_test_4yq6txdpfadhbaqnwp3",
55
+ "ip": null,
56
+ "created": "2015-01-15T05:00:29Z"
57
+ }
58
+ ]
59
+ }
@@ -0,0 +1,19 @@
1
+ {
2
+ "object": "list",
3
+ "from": "1970-01-01T00:00:00+00:00",
4
+ "to": "2016-09-04T09:25:40+00:00",
5
+ "offset": 0,
6
+ "limit": 20,
7
+ "total": 1,
8
+ "order": "chronological",
9
+ "location": "/disputes/dspt_test_5089off452g5m5te7xs/documents",
10
+ "data": [
11
+ {
12
+ "object": "document",
13
+ "id": "docu_test_55869onwfm2g3bsw8d8",
14
+ "livemode": false,
15
+ "location": "/disputes/dspt_test_5089off452g5m5te7xs/documents/docu_test_55869onwfm2g3bsw8d8",
16
+ "filename": "dspt_test_5089off452g5m5te7xs_document_1.pdf"
17
+ }
18
+ ]
19
+ }
@@ -0,0 +1,7 @@
1
+ {
2
+ "object": "document",
3
+ "id": "docu_test_55869onwfm2g3bsw8d8",
4
+ "livemode": false,
5
+ "location": "/disputes/dspt_test_5089off452g5m5te7xs/documents/docu_test_55869onwfm2g3bsw8d8",
6
+ "filename": "dspt_test_5089off452g5m5te7xs_document_1.pdf"
7
+ }
@@ -0,0 +1,6 @@
1
+ {
2
+ "object": "document",
3
+ "id": "docu_test_55869onwfm2g3bsw8d8",
4
+ "livemode": false,
5
+ "deleted": true
6
+ }
@@ -0,0 +1,7 @@
1
+ {
2
+ "object": "document",
3
+ "id": "docu_test_55869onwfm2g3bsw8d8",
4
+ "livemode": false,
5
+ "location": "/disputes/dspt_test_5089off452g5m5te7xs/documents/docu_test_55869onwfm2g3bsw8d8",
6
+ "filename": "dspt_test_5089off452g5m5te7xs_document_1.pdf"
7
+ }
@@ -0,0 +1,94 @@
1
+ {
2
+ "object": "list",
3
+ "from": "1970-01-01T00:00:00+00:00",
4
+ "to": "2016-11-09T03:28:42+00:00",
5
+ "offset": 0,
6
+ "limit": 20,
7
+ "total": 1,
8
+ "order": "chronological",
9
+ "location": "/links",
10
+ "data": [
11
+ {
12
+ "object": "link",
13
+ "id": "link_test_55pcclmznvrv9lc7r9s",
14
+ "livemode": false,
15
+ "location": "/links/link_test_55pcclmznvrv9lc7r9s",
16
+ "amount": 10000,
17
+ "currency": "thb",
18
+ "used": true,
19
+ "multiple": false,
20
+ "title": "Lunch",
21
+ "description": "Pay me back for lunch",
22
+ "charges": {
23
+ "object": "list",
24
+ "from": "1970-01-01T00:00:00+00:00",
25
+ "to": "2016-11-09T03:28:42+00:00",
26
+ "offset": 0,
27
+ "limit": 20,
28
+ "total": 1,
29
+ "order": null,
30
+ "location": "/links/link_test_55pcclmznvrv9lc7r9s/charges",
31
+ "data": [
32
+ {
33
+ "object": "charge",
34
+ "id": "chrg_test_55pcd9rn4dz1r8rtokz",
35
+ "livemode": false,
36
+ "location": "/charges/chrg_test_55pcd9rn4dz1r8rtokz",
37
+ "amount": 10000,
38
+ "currency": "thb",
39
+ "description": "Lunch\n\nPay me back for lunch",
40
+ "status": "successful",
41
+ "capture": true,
42
+ "authorized": true,
43
+ "reversed": false,
44
+ "paid": true,
45
+ "transaction": "trxn_test_55pcd9t5vh5m7rflo7x",
46
+ "refunded": 0,
47
+ "refunds": {
48
+ "object": "list",
49
+ "from": "1970-01-01T00:00:00+00:00",
50
+ "to": "2016-11-09T03:28:42+00:00",
51
+ "offset": 0,
52
+ "limit": 20,
53
+ "total": 0,
54
+ "order": null,
55
+ "location": "/charges/chrg_test_55pcd9rn4dz1r8rtokz/refunds",
56
+ "data": [
57
+
58
+ ]
59
+ },
60
+ "return_uri": "https://link.omise.co/AC6BD2FD/complete",
61
+ "reference": "paym_test_55pcd9rzzt5hsko5loq",
62
+ "authorize_uri": "http://api.omise.co/payments/paym_test_55pcd9rzzt5hsko5loq/authorize",
63
+ "failure_code": null,
64
+ "failure_message": null,
65
+ "card": {
66
+ "object": "card",
67
+ "id": "card_test_55pcd8si7fqp1zuihwk",
68
+ "livemode": false,
69
+ "country": "us",
70
+ "city": null,
71
+ "postal_code": null,
72
+ "financing": "",
73
+ "bank": "",
74
+ "last_digits": "4242",
75
+ "brand": "Visa",
76
+ "expiration_month": 7,
77
+ "expiration_year": 2019,
78
+ "fingerprint": "hdEymlzWwHtOipbRxVUp3Ek7q13+1KUtwIvkkwz2cqE=",
79
+ "name": "sanjeev",
80
+ "security_code_check": true,
81
+ "created": "2016-10-18T06:56:47Z"
82
+ },
83
+ "customer": null,
84
+ "ip": "101.127.195.82",
85
+ "dispute": null,
86
+ "created": "2016-10-18T06:56:51Z"
87
+ }
88
+ ]
89
+ },
90
+ "payment_uri": "https://link.omise.co/AC6BD2FD",
91
+ "created": "2016-10-18T06:54:57Z"
92
+ }
93
+ ]
94
+ }
@@ -0,0 +1,82 @@
1
+ {
2
+ "object": "link",
3
+ "id": "link_test_55pcclmznvrv9lc7r9s",
4
+ "livemode": false,
5
+ "location": "/links/link_test_55pcclmznvrv9lc7r9s",
6
+ "amount": 10000,
7
+ "currency": "thb",
8
+ "used": true,
9
+ "multiple": false,
10
+ "title": "Lunch",
11
+ "description": "Pay me back for lunch",
12
+ "charges": {
13
+ "object": "list",
14
+ "from": "1970-01-01T00:00:00+00:00",
15
+ "to": "2016-11-09T03:28:42+00:00",
16
+ "offset": 0,
17
+ "limit": 20,
18
+ "total": 1,
19
+ "order": null,
20
+ "location": "/links/link_test_55pcclmznvrv9lc7r9s/charges",
21
+ "data": [
22
+ {
23
+ "object": "charge",
24
+ "id": "chrg_test_55pcd9rn4dz1r8rtokz",
25
+ "livemode": false,
26
+ "location": "/charges/chrg_test_55pcd9rn4dz1r8rtokz",
27
+ "amount": 10000,
28
+ "currency": "thb",
29
+ "description": "Lunch\n\nPay me back for lunch",
30
+ "status": "successful",
31
+ "capture": true,
32
+ "authorized": true,
33
+ "reversed": false,
34
+ "paid": true,
35
+ "transaction": "trxn_test_55pcd9t5vh5m7rflo7x",
36
+ "refunded": 0,
37
+ "refunds": {
38
+ "object": "list",
39
+ "from": "1970-01-01T00:00:00+00:00",
40
+ "to": "2016-11-09T03:28:42+00:00",
41
+ "offset": 0,
42
+ "limit": 20,
43
+ "total": 0,
44
+ "order": null,
45
+ "location": "/charges/chrg_test_55pcd9rn4dz1r8rtokz/refunds",
46
+ "data": [
47
+
48
+ ]
49
+ },
50
+ "return_uri": "https://link.omise.co/AC6BD2FD/complete",
51
+ "reference": "paym_test_55pcd9rzzt5hsko5loq",
52
+ "authorize_uri": "http://api.omise.co/payments/paym_test_55pcd9rzzt5hsko5loq/authorize",
53
+ "failure_code": null,
54
+ "failure_message": null,
55
+ "card": {
56
+ "object": "card",
57
+ "id": "card_test_55pcd8si7fqp1zuihwk",
58
+ "livemode": false,
59
+ "country": "us",
60
+ "city": null,
61
+ "postal_code": null,
62
+ "financing": "",
63
+ "bank": "",
64
+ "last_digits": "4242",
65
+ "brand": "Visa",
66
+ "expiration_month": 7,
67
+ "expiration_year": 2019,
68
+ "fingerprint": "hdEymlzWwHtOipbRxVUp3Ek7q13+1KUtwIvkkwz2cqE=",
69
+ "name": "sanjeev",
70
+ "security_code_check": true,
71
+ "created": "2016-10-18T06:56:47Z"
72
+ },
73
+ "customer": null,
74
+ "ip": "101.127.195.82",
75
+ "dispute": null,
76
+ "created": "2016-10-18T06:56:51Z"
77
+ }
78
+ ]
79
+ },
80
+ "payment_uri": "https://link.omise.co/AC6BD2FD",
81
+ "created": "2016-10-18T06:54:57Z"
82
+ }