omise 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (74) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +14 -0
  3. data/Gemfile +4 -0
  4. data/LICENSE.txt +22 -0
  5. data/README.md +56 -0
  6. data/Rakefile +8 -0
  7. data/data/ca_certificates.pem +105 -0
  8. data/lib/omise.rb +6 -0
  9. data/lib/omise/account.rb +8 -0
  10. data/lib/omise/all.rb +9 -0
  11. data/lib/omise/attributes.rb +63 -0
  12. data/lib/omise/balance.rb +8 -0
  13. data/lib/omise/card.rb +19 -0
  14. data/lib/omise/card_list.rb +22 -0
  15. data/lib/omise/charge.rb +55 -0
  16. data/lib/omise/config.rb +36 -0
  17. data/lib/omise/customer.rb +50 -0
  18. data/lib/omise/error.rb +12 -0
  19. data/lib/omise/list.rb +37 -0
  20. data/lib/omise/object.rb +60 -0
  21. data/lib/omise/refund.rb +32 -0
  22. data/lib/omise/refund_list.rb +19 -0
  23. data/lib/omise/resource.rb +43 -0
  24. data/lib/omise/singleton_resource.rb +21 -0
  25. data/lib/omise/testing/resource.rb +38 -0
  26. data/lib/omise/token.rb +26 -0
  27. data/lib/omise/transaction.rb +16 -0
  28. data/lib/omise/transfer.rb +32 -0
  29. data/lib/omise/util.rb +24 -0
  30. data/lib/omise/vault.rb +15 -0
  31. data/lib/omise/version.rb +3 -0
  32. data/omise.gemspec +25 -0
  33. data/test/fixtures/api.omise.co/account-get.json +6 -0
  34. data/test/fixtures/api.omise.co/balance-get.json +7 -0
  35. data/test/fixtures/api.omise.co/charges-get.json +59 -0
  36. data/test/fixtures/api.omise.co/charges-post.json +36 -0
  37. data/test/fixtures/api.omise.co/charges/404-get.json +6 -0
  38. data/test/fixtures/api.omise.co/charges/chrg_test_4yq7duw15p9hdrjp8oq-get.json +58 -0
  39. data/test/fixtures/api.omise.co/charges/chrg_test_4yq7duw15p9hdrjp8oq-patch.json +58 -0
  40. data/test/fixtures/api.omise.co/charges/chrg_test_4yq7duw15p9hdrjp8oq/refunds-get.json +21 -0
  41. data/test/fixtures/api.omise.co/charges/chrg_test_4yq7duw15p9hdrjp8oq/refunds-post.json +10 -0
  42. data/test/fixtures/api.omise.co/charges/chrg_test_4yq7duw15p9hdrjp8oq/refunds/404-get.json +6 -0
  43. data/test/fixtures/api.omise.co/charges/chrg_test_4yq7duw15p9hdrjp8oq/refunds/rfnd_test_4yqmv79ahghsiz23y3c-get.json +10 -0
  44. data/test/fixtures/api.omise.co/customers-get.json +49 -0
  45. data/test/fixtures/api.omise.co/customers-post.json +22 -0
  46. data/test/fixtures/api.omise.co/customers/404-get.json +6 -0
  47. data/test/fixtures/api.omise.co/customers/cust_test_4yq6txdpfadhbaqnwp3-delete.json +6 -0
  48. data/test/fixtures/api.omise.co/customers/cust_test_4yq6txdpfadhbaqnwp3-get.json +39 -0
  49. data/test/fixtures/api.omise.co/customers/cust_test_4yq6txdpfadhbaqnwp3-patch.json +39 -0
  50. data/test/fixtures/api.omise.co/customers/cust_test_4yq6txdpfadhbaqnwp3/cards/404-get.json +6 -0
  51. data/test/fixtures/api.omise.co/customers/cust_test_4yq6txdpfadhbaqnwp3/cards/card_test_4yq6tuucl9h4erukfl0-delete.json +6 -0
  52. data/test/fixtures/api.omise.co/customers/cust_test_4yq6txdpfadhbaqnwp3/cards/card_test_4yq6tuucl9h4erukfl0-get.json +18 -0
  53. data/test/fixtures/api.omise.co/customers/cust_test_4yq6txdpfadhbaqnwp3/cards/card_test_4yq6tuucl9h4erukfl0-patch.json +18 -0
  54. data/test/fixtures/api.omise.co/transactions-get.json +26 -0
  55. data/test/fixtures/api.omise.co/transactions/trxn_test_4yq7duwb9jts1vxgqua-get.json +8 -0
  56. data/test/fixtures/api.omise.co/transactions/trxn_test_4yqafnvlztbf3908vs1-get.json +8 -0
  57. data/test/fixtures/api.omise.co/transactions/trxn_test_4yqmv79fzpy0gmz5mmq-get.json +8 -0
  58. data/test/fixtures/api.omise.co/transfers-get.json +24 -0
  59. data/test/fixtures/api.omise.co/transfers-post.json +14 -0
  60. data/test/fixtures/api.omise.co/transfers/trsf_test_4yqacz8t3cbipcj766u-get.json +14 -0
  61. data/test/fixtures/vault.omise.co/tokens-post.json +25 -0
  62. data/test/fixtures/vault.omise.co/tokens/tokn_test_4yq8lbecl0q6dsjzxr5-get.json +25 -0
  63. data/test/omise/test_account.rb +19 -0
  64. data/test/omise/test_balance.rb +19 -0
  65. data/test/omise/test_card.rb +52 -0
  66. data/test/omise/test_charge.rb +56 -0
  67. data/test/omise/test_customer.rb +59 -0
  68. data/test/omise/test_refund.rb +41 -0
  69. data/test/omise/test_token.rb +31 -0
  70. data/test/omise/test_transaction.rb +18 -0
  71. data/test/omise/test_transfer.rb +24 -0
  72. data/test/support.rb +12 -0
  73. data/test/test_omise.rb +7 -0
  74. metadata +214 -0
@@ -0,0 +1,50 @@
1
+ require "omise/object"
2
+ require "omise/list"
3
+ require "omise/card_list"
4
+
5
+ module Omise
6
+ class Customer < OmiseObject
7
+ self.endpoint = "/customers"
8
+
9
+ def self.retrieve(id = nil, attributes = {})
10
+ new resource(location(id), attributes).get
11
+ end
12
+
13
+ def self.list(attributes = {})
14
+ List.new resource(location, attributes).get
15
+ end
16
+
17
+ def self.create(attributes = {})
18
+ new resource(location, attributes).post(attributes)
19
+ end
20
+
21
+ def reload(attributes = {})
22
+ assign_attributes resource(attributes).get
23
+ end
24
+
25
+ def update(attributes = {})
26
+ assign_attributes resource(attributes).patch(attributes)
27
+ end
28
+
29
+ def destroy(attributes = {})
30
+ assign_attributes resource(attributes).delete
31
+ end
32
+
33
+ def default_card(options = {})
34
+ if @attributes["default_card"]
35
+ @default_card ||= cards.retrieve(@attributes["default_card"], options)
36
+ end
37
+ end
38
+
39
+ def cards
40
+ @cards ||= CardList.new(self, @attributes["cards"])
41
+ end
42
+
43
+ private
44
+
45
+ def cleanup!
46
+ @default_card = nil
47
+ @cards = nil
48
+ end
49
+ end
50
+ end
@@ -0,0 +1,12 @@
1
+ module Omise
2
+ class Error < StandardError
3
+ def initialize(attributes = {})
4
+ @code = attributes["code"]
5
+ @message = attributes["message"]
6
+ end
7
+
8
+ def to_s
9
+ "#{@message} (#{@code})"
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,37 @@
1
+ require "omise/util"
2
+ require "omise/object"
3
+
4
+ module Omise
5
+ class List < OmiseObject
6
+ include Enumerable
7
+
8
+ def initialize(attributes = {})
9
+ super(attributes)
10
+ setup_data
11
+ end
12
+
13
+ def reload(attributes = {})
14
+ assign_attributes resource(attributes).get do
15
+ setup_data
16
+ end
17
+ end
18
+
19
+ def each(*args, &block)
20
+ to_a.each(*args, &block)
21
+ end
22
+
23
+ def to_a
24
+ @data
25
+ end
26
+
27
+ def last
28
+ to_a.last
29
+ end
30
+
31
+ private
32
+
33
+ def setup_data
34
+ @data = @attributes["data"].map { |o| Omise::Util.typecast(o) }
35
+ end
36
+ end
37
+ end
@@ -0,0 +1,60 @@
1
+ require "omise/config"
2
+ require "omise/attributes"
3
+ require "omise/resource"
4
+
5
+ module Omise
6
+ class OmiseObject
7
+ include Attributes
8
+
9
+ class << self
10
+ attr_accessor :endpoint
11
+
12
+ def location(id = nil)
13
+ [endpoint, id].compact.join("/")
14
+ end
15
+
16
+ def resource(path, attributes = {})
17
+ key = attributes.delete(:key) { resource_key }
18
+ preprocess_attributes!(attributes)
19
+ Omise.resource.new(resource_url, path, key)
20
+ end
21
+
22
+ private
23
+
24
+ def collection
25
+ self
26
+ end
27
+
28
+ def singleton!
29
+ require "omise/singleton_resource"
30
+ include SingletonResource
31
+ end
32
+
33
+ def preprocess_attributes!(attributes)
34
+ if attributes[:card].is_a?(Hash)
35
+ require "omise/token"
36
+ card_attributes = attributes.delete(:card)
37
+ attributes[:card] = Token.create(card: card_attributes).id
38
+ end
39
+ end
40
+
41
+ def resource_url
42
+ Omise.api_url
43
+ end
44
+
45
+ def resource_key
46
+ Omise.api_key
47
+ end
48
+ end
49
+
50
+ private
51
+
52
+ def collection
53
+ self.class
54
+ end
55
+
56
+ def resource(*args)
57
+ collection.resource(location, *args)
58
+ end
59
+ end
60
+ end
@@ -0,0 +1,32 @@
1
+ require "omise/object"
2
+ require "omise/charge"
3
+ require "omise/transaction"
4
+
5
+ module Omise
6
+ class Refund < OmiseObject
7
+ self.endpoint = "/refunds"
8
+
9
+ def reload(attributes = {})
10
+ assign_attributes resource(attributes).get
11
+ end
12
+
13
+ def charge(options = {})
14
+ if @attributes["charge"]
15
+ @charge ||= Charge.retrieve(@attributes["charge"], options)
16
+ end
17
+ end
18
+
19
+ def transaction(options = {})
20
+ if @attributes["transaction"]
21
+ @transaction ||= Transaction.retrieve(@attributes["transaction"], options)
22
+ end
23
+ end
24
+
25
+ private
26
+
27
+ def cleanup!
28
+ @charge = nil
29
+ @transaction = nil
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,19 @@
1
+ require "omise/list"
2
+ require "omise/refund"
3
+
4
+ module Omise
5
+ class RefundList < List
6
+ def initialize(charge, attributes = {})
7
+ super(attributes)
8
+ @charge = charge
9
+ end
10
+
11
+ def create(attributes = {})
12
+ Refund.new self.class.resource(location, attributes).post(attributes)
13
+ end
14
+
15
+ def retrieve(id, attributes = {})
16
+ Refund.new self.class.resource(location(id), attributes).get
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,43 @@
1
+ require "uri"
2
+ require "json"
3
+ require "openssl"
4
+ require "rest-client"
5
+
6
+ require "omise/util"
7
+ require "omise/config"
8
+ require "omise/error"
9
+
10
+ module Omise
11
+ class Resource
12
+ CA_BUNDLE_PATH = File.expand_path("../../../data/ca_certificates.pem", __FILE__)
13
+
14
+ def initialize(url, path, key)
15
+ @uri = URI.parse(url)
16
+ @uri.path = [@uri.path, path].join
17
+ @resource = RestClient::Resource.new(@uri.to_s, {
18
+ user: key,
19
+ verify_ssl: OpenSSL::SSL::VERIFY_PEER,
20
+ ssl_ca_file: CA_BUNDLE_PATH,
21
+ headers: {
22
+ user_agent: "OmiseRuby/#{Omise::VERSION} OmiseAPI/#{Omise.api_version} Ruby/#{RUBY_VERSION}"
23
+ }
24
+ })
25
+ end
26
+
27
+ def get
28
+ @resource.get { |r| Omise::Util.load_response(r) }
29
+ end
30
+
31
+ def patch(attributes)
32
+ @resource.patch(attributes) { |r| Omise::Util.load_response(r) }
33
+ end
34
+
35
+ def post(attributes)
36
+ @resource.post(attributes) { |r| Omise::Util.load_response(r) }
37
+ end
38
+
39
+ def delete
40
+ @resource.delete { |r| Omise::Util.load_response(r) }
41
+ end
42
+ end
43
+ end
@@ -0,0 +1,21 @@
1
+ module Omise
2
+ module SingletonResource
3
+ def self.included(base)
4
+ base.extend ClassMethods
5
+ end
6
+
7
+ def reload(attributes = {})
8
+ assign_attributes resource(attributes).get
9
+ end
10
+
11
+ def location
12
+ self.class.location
13
+ end
14
+
15
+ module ClassMethods
16
+ def retrieve(attributes = {})
17
+ new resource(location, attributes).get
18
+ end
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,38 @@
1
+ require "uri"
2
+
3
+ require "omise/util"
4
+
5
+ module Omise
6
+ module Testing
7
+ class Resource
8
+ def initialize(url, path, key)
9
+ @uri = URI.parse(url)
10
+ @uri.path = [@uri.path, path].join
11
+ @key = key
12
+ end
13
+
14
+ def get(attributes = {})
15
+ Omise::Util.load_response(read_file("get"))
16
+ end
17
+
18
+ def patch(attributes = {})
19
+ Omise::Util.load_response(read_file("patch"))
20
+ end
21
+
22
+ def delete(attributes = {})
23
+ Omise::Util.load_response(read_file("delete"))
24
+ end
25
+
26
+ def post(attributes = {})
27
+ Omise::Util.load_response(read_file("post"))
28
+ end
29
+
30
+ def read_file(verb)
31
+ File.read(File.expand_path(File.join(
32
+ Omise::LIB_PATH, "..", "test", "fixtures",
33
+ [@uri.host, @uri.path, "-#{verb}.json"].join
34
+ )))
35
+ end
36
+ end
37
+ end
38
+ end
@@ -0,0 +1,26 @@
1
+ require "omise/object"
2
+ require "omise/vault"
3
+
4
+ module Omise
5
+ class Token < OmiseObject
6
+ self.endpoint = "/tokens"
7
+
8
+ extend Vault
9
+
10
+ def self.retrieve(id, attributes = {})
11
+ new resource(location(id), attributes).get
12
+ end
13
+
14
+ def self.create(attributes = {})
15
+ new resource(location, attributes).post(attributes)
16
+ end
17
+
18
+ def reload(attributes = {})
19
+ assign_attributes resource(attributes).get
20
+ end
21
+
22
+ def self.preprocess_attributes!(attributes)
23
+ # noop
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,16 @@
1
+ require "omise/object"
2
+ require "omise/list"
3
+
4
+ module Omise
5
+ class Transaction < OmiseObject
6
+ self.endpoint = "/transactions"
7
+
8
+ def self.retrieve(id = nil, attributes = {})
9
+ new resource(location(id), attributes).get
10
+ end
11
+
12
+ def self.list(attributes = {})
13
+ List.new resource(location, attributes).get
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,32 @@
1
+ require "omise/object"
2
+ require "omise/list"
3
+
4
+ module Omise
5
+ class Transfer < OmiseObject
6
+ self.endpoint = "/transfers"
7
+
8
+ def self.create(attributes = {})
9
+ new resource(location, attributes).post(attributes)
10
+ end
11
+
12
+ def self.retrieve(id, attributes = {})
13
+ new resource(location(id), attributes).get
14
+ end
15
+
16
+ def self.list(attributes = {})
17
+ List.new resource(location, attributes).get
18
+ end
19
+
20
+ def reload(attributes = {})
21
+ assign_attributes resource(attributes).get
22
+ end
23
+
24
+ def update(attributes = {})
25
+ assign_attributes resource(attributes).patch(attributes)
26
+ end
27
+
28
+ def destroy(attributes = {})
29
+ assign_attributes resource(attributes).delete
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,24 @@
1
+ require "json"
2
+
3
+ require "omise/object"
4
+ require "omise/error"
5
+
6
+ module Omise
7
+ module Util module_function
8
+ def typecast(object)
9
+ klass = begin
10
+ const_get(object["object"].capitalize)
11
+ rescue NameError
12
+ OmiseObject
13
+ end
14
+
15
+ klass.new(object)
16
+ end
17
+
18
+ def load_response(response)
19
+ object = JSON.load(response)
20
+ raise Omise::Error.new(object) if object["object"] == "error"
21
+ object
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,15 @@
1
+ require "omise/config"
2
+
3
+ module Omise
4
+ module Vault
5
+ private
6
+
7
+ def resource_url
8
+ Omise.vault_url
9
+ end
10
+
11
+ def resource_key
12
+ Omise.vault_key
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,3 @@
1
+ module Omise
2
+ VERSION = "0.1.0"
3
+ end