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
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 0ff392328416601e36ba46368afcbb591eeb811b
4
- data.tar.gz: e3317b3b98779651c2cfc7bd99621500f57a101d
3
+ metadata.gz: fb0747d2916db26b8867f729d2fc45ec8c0ae298
4
+ data.tar.gz: 6f32be4761236faaca1495948d8bd4abe33581c0
5
5
  SHA512:
6
- metadata.gz: 61bb3e5a0bfac06e1c930bb78d0e2cf89e07453e8ed1911a8874197d7152e69975457d9e923c2984b92e2a01f44a14180ec9f03707f5495120c585912aab2227
7
- data.tar.gz: d5e4f4475e8cc0fc395d2c747206cbb60e569dd3dc8cceb11b3f3478eb9f00e82431197062a4e7d2dbacd3f90e1e1c2aacf8112194dc067a4b3de73441c54856
6
+ metadata.gz: 5f0cdda6e658f79be9a7056e2f49d5a634cb8cb74664b2e54753bce1d36ff8c6e3120e5c4f69536dac1ed4fb9d0124516e2c8ba838a50a0351beaa16ea1458d6
7
+ data.tar.gz: 6fbba84db39bfe3efbf8085b82c2c32931d913745b972299e8d4560a86abbab9f02c82cab48e70f4dfbaa878e18274b0c58e88bf1254d1a9069782c6981eeed3
@@ -2,6 +2,15 @@
2
2
 
3
3
  An [unreleased] version is not available on rubygems and is subject to changes and must not be considered final. Elements of unreleased list may be edited or removed at any time.
4
4
 
5
+ ## [0.5.0] 2016-11-26
6
+
7
+ - [Added] Add search object and filtering api
8
+ - [Added] Add support for document uploading
9
+ - [Added] Add link API
10
+ - [Added] Add tests for attributes
11
+ - [Added] Add charge method on customer
12
+ - [Added] Add pagination on list
13
+
5
14
  ## [0.4.0] 2016-06-01
6
15
 
7
16
  - [Added] Add charge reversal method (@zentetsukenz)
@@ -0,0 +1,9 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "omise"
5
+
6
+ Omise.api_key = ENV["OMISE_API_KEY"]
7
+
8
+ require "pry"
9
+ Pry.start
@@ -0,0 +1,17 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+ #
4
+ # This file was generated by Bundler.
5
+ #
6
+ # The application 'rake' is installed as part of a gem, and
7
+ # this file is here to facilitate running it.
8
+ #
9
+
10
+ require "pathname"
11
+ ENV["BUNDLE_GEMFILE"] ||= File.expand_path("../../Gemfile",
12
+ Pathname.new(__FILE__).realpath)
13
+
14
+ require "rubygems"
15
+ require "bundler/setup"
16
+
17
+ load Gem.bin_path("rake", "rake")
@@ -4,9 +4,12 @@ require "omise/card"
4
4
  require "omise/charge"
5
5
  require "omise/customer"
6
6
  require "omise/dispute"
7
+ require "omise/document"
7
8
  require "omise/event"
9
+ require "omise/link"
8
10
  require "omise/recipient"
9
11
  require "omise/refund"
12
+ require "omise/search"
10
13
  require "omise/token"
11
14
  require "omise/transaction"
12
15
  require "omise/transfer"
@@ -2,9 +2,10 @@ require "omise/util"
2
2
 
3
3
  module Omise
4
4
  module Attributes
5
- def initialize(attributes = {})
5
+ def initialize(attributes = {}, options = {})
6
+ @attributes = attributes
7
+ @options = options
6
8
  @expanded_attributes = {}
7
- @attributes = attributes
8
9
  end
9
10
 
10
11
  def attributes
@@ -62,7 +63,15 @@ module Omise
62
63
  end
63
64
 
64
65
  def list_attribute(klass, key)
65
- klass.new(self, @attributes[key])
66
+ klass.new(@attributes[key], parent: self)
67
+ end
68
+
69
+ def list_nested_resource(klass, key, options = {})
70
+ if @attributes.key?(key) && options.empty?
71
+ return list_attribute(klass, key)
72
+ end
73
+
74
+ klass.new(nested_resource(key, options).get, parent: self)
66
75
  end
67
76
 
68
77
  def expand_attribute(object, key, options = {})
@@ -4,18 +4,13 @@ require "omise/token"
4
4
 
5
5
  module Omise
6
6
  class CardList < List
7
- def initialize(customer, attributes = {})
8
- super(attributes)
9
- @customer = customer
10
- end
11
-
12
7
  def retrieve(id, attributes = {})
13
8
  Card.new self.class.resource(location(id), attributes).get(attributes)
14
9
  end
15
10
 
16
11
  def create(attributes = {})
17
12
  token = Token.create(card: attributes)
18
- @customer.update(card: token.id)
13
+ parent.update(card: token.id)
19
14
  retrieve(token.card.id)
20
15
  end
21
16
  end
@@ -3,12 +3,17 @@ require "omise/list"
3
3
  require "omise/customer"
4
4
  require "omise/dispute"
5
5
  require "omise/refund_list"
6
+ require "omise/search_scope"
6
7
  require "omise/transaction"
7
8
 
8
9
  module Omise
9
10
  class Charge < OmiseObject
10
11
  self.endpoint = "/charges"
11
12
 
13
+ def self.search
14
+ SearchScope.new(:charge)
15
+ end
16
+
12
17
  def self.retrieve(id, attributes = {})
13
18
  new resource(location(id), attributes).get(attributes)
14
19
  end
@@ -0,0 +1,10 @@
1
+ require "omise/list"
2
+ require "omise/charge"
3
+
4
+ module Omise
5
+ class ChargeList < List
6
+ def retrieve(id, attributes = {})
7
+ Charge.retrieve(id)
8
+ end
9
+ end
10
+ end
@@ -1,11 +1,17 @@
1
1
  require "omise/object"
2
- require "omise/list"
3
2
  require "omise/card_list"
3
+ require "omise/charge"
4
+ require "omise/list"
5
+ require "omise/search_scope"
4
6
 
5
7
  module Omise
6
8
  class Customer < OmiseObject
7
9
  self.endpoint = "/customers"
8
10
 
11
+ def self.search
12
+ SearchScope.new(:customer)
13
+ end
14
+
9
15
  def self.retrieve(id = nil, attributes = {})
10
16
  new resource(location(id), attributes).get(attributes)
11
17
  end
@@ -30,6 +36,10 @@ module Omise
30
36
  assign_attributes resource(attributes).delete
31
37
  end
32
38
 
39
+ def charge(attributes = {})
40
+ Charge.create(attributes.merge(customer: id))
41
+ end
42
+
33
43
  def default_card(options = {})
34
44
  expand_attribute cards, "default_card", options
35
45
  end
@@ -39,7 +49,7 @@ module Omise
39
49
  list_attribute CardList, "cards"
40
50
  else
41
51
  response = collection.resource(location("cards")).get(options)
42
- CardList.new(self, response)
52
+ CardList.new(response, parent: self)
43
53
  end
44
54
  end
45
55
  end
@@ -1,11 +1,17 @@
1
1
  require "omise/object"
2
2
  require "omise/charge"
3
+ require "omise/document_list"
3
4
  require "omise/list"
5
+ require "omise/search_scope"
4
6
 
5
7
  module Omise
6
8
  class Dispute < OmiseObject
7
9
  self.endpoint = "/disputes"
8
10
 
11
+ def self.search
12
+ SearchScope.new(:dispute)
13
+ end
14
+
9
15
  def self.list(attributes = {})
10
16
  status = attributes.delete(:status)
11
17
  List.new resource(location(status), attributes).get(attributes)
@@ -26,5 +32,9 @@ module Omise
26
32
  def charge(options = {})
27
33
  expand_attribute Charge, "charge", options
28
34
  end
35
+
36
+ def documents(options = {})
37
+ list_nested_resource DocumentList, "documents", options
38
+ end
29
39
  end
30
40
  end
@@ -0,0 +1,15 @@
1
+ require "omise/object"
2
+
3
+ module Omise
4
+ class Document < OmiseObject
5
+ self.endpoint = "/documents"
6
+
7
+ def reload(attributes = {})
8
+ assign_attributes resource(attributes).get(attributes)
9
+ end
10
+
11
+ def destroy(attributes = {})
12
+ assign_attributes resource(attributes).delete
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,15 @@
1
+ require "omise/document"
2
+ require "omise/list"
3
+
4
+ module Omise
5
+ class DocumentList < List
6
+ def retrieve(id, attributes = {})
7
+ Document.new self.class.resource(location(id), attributes).get(attributes)
8
+ end
9
+
10
+ def upload(file)
11
+ attributes = { file: file }
12
+ Document.new self.class.resource(location, attributes).post(attributes)
13
+ end
14
+ end
15
+ end
@@ -1,12 +1,10 @@
1
1
  module Omise
2
2
  class Error < StandardError
3
- def initialize(attributes = {})
3
+ def initialize(attributes)
4
4
  @code = attributes["code"]
5
- @message = attributes["message"]
5
+ super("#{attributes["message"]} (#{@code})")
6
6
  end
7
7
 
8
- def to_s
9
- "#{@message} (#{@code})"
10
- end
8
+ attr_reader :code
11
9
  end
12
10
  end
@@ -0,0 +1,37 @@
1
+ require "omise/object"
2
+ require "omise/charge_list"
3
+
4
+ module Omise
5
+ class Link < OmiseObject
6
+ self.endpoint = "/links"
7
+
8
+ def self.search
9
+ SearchScope.new(:link)
10
+ end
11
+
12
+ def self.retrieve(id, attributes = {})
13
+ new resource(location(id), attributes).get(attributes)
14
+ end
15
+
16
+ def self.list(attributes = {})
17
+ List.new resource(location, attributes).get(attributes)
18
+ end
19
+
20
+ def self.create(attributes = {})
21
+ new resource(location, attributes).post(attributes)
22
+ end
23
+
24
+ def reload(attributes = {})
25
+ assign_attributes resource(attributes).get(attributes)
26
+ end
27
+
28
+ def charges(options = {})
29
+ if options.empty?
30
+ list_attribute ChargeList, "charges"
31
+ else
32
+ response = collection.resource(location("charges")).get(options)
33
+ ChargeList.new(self, response)
34
+ end
35
+ end
36
+ end
37
+ end
@@ -5,8 +5,8 @@ module Omise
5
5
  class List < OmiseObject
6
6
  include Enumerable
7
7
 
8
- def initialize(attributes = {})
9
- super(attributes)
8
+ def initialize(attributes = {}, options = {})
9
+ super(attributes, options)
10
10
  setup_data
11
11
  end
12
12
 
@@ -16,6 +16,38 @@ module Omise
16
16
  end
17
17
  end
18
18
 
19
+ def parent
20
+ @options[:parent]
21
+ end
22
+
23
+ def first_page?
24
+ offset == 0
25
+ end
26
+
27
+ def last_page?
28
+ offset + limit >= total
29
+ end
30
+
31
+ def page
32
+ 1 + (offset / limit)
33
+ end
34
+
35
+ def total_pages
36
+ (total.to_f / limit).ceil
37
+ end
38
+
39
+ def next_page
40
+ self.class.new(@attributes, @options).send(:next_page!)
41
+ end
42
+
43
+ def previous_page
44
+ self.class.new(@attributes, @options).send(:previous_page!)
45
+ end
46
+
47
+ def jump_to_page(page)
48
+ self.class.new(@attributes, @options).send(:jump_to_page!, page)
49
+ end
50
+
19
51
  def each(*args, &block)
20
52
  to_a.each(*args, &block)
21
53
  end
@@ -30,6 +62,25 @@ module Omise
30
62
 
31
63
  private
32
64
 
65
+ def jump_to_page!(page)
66
+ new_offset = ((page - 1) * limit)
67
+ return nil if new_offset >= total
68
+ reload(offset: new_offset, limit: limit)
69
+ self
70
+ end
71
+
72
+ def next_page!
73
+ return nil if (offset + limit) >= total
74
+ reload(offset: offset + limit, limit: limit)
75
+ self
76
+ end
77
+
78
+ def previous_page!
79
+ return nil if (offset - limit) < 0
80
+ reload(offset: offset - limit, limit: limit)
81
+ self
82
+ end
83
+
33
84
  def setup_data
34
85
  @data = @attributes["data"].map { |o| Omise::Util.typecast(o) }
35
86
  end
@@ -1,10 +1,15 @@
1
1
  require "omise/object"
2
2
  require "omise/bank_account"
3
+ require "omise/search_scope"
3
4
 
4
5
  module Omise
5
6
  class Recipient < OmiseObject
6
7
  self.endpoint = "/recipients"
7
8
 
9
+ def self.search
10
+ SearchScope.new(:recipient)
11
+ end
12
+
8
13
  def self.retrieve(id, attributes = {})
9
14
  new resource(location(id), attributes).get(attributes)
10
15
  end
@@ -6,6 +6,10 @@ module Omise
6
6
  class Refund < OmiseObject
7
7
  self.endpoint = "/refunds"
8
8
 
9
+ def self.search
10
+ SearchScope.new(:refund)
11
+ end
12
+
9
13
  def reload(attributes = {})
10
14
  assign_attributes resource(attributes).get(attributes)
11
15
  end
@@ -3,11 +3,6 @@ require "omise/refund"
3
3
 
4
4
  module Omise
5
5
  class RefundList < List
6
- def initialize(charge, attributes = {})
7
- super(attributes)
8
- @charge = charge
9
- end
10
-
11
6
  def create(attributes = {})
12
7
  Refund.new self.class.resource(location, attributes).post(attributes)
13
8
  end
@@ -12,26 +12,26 @@ module Omise
12
12
  class Resource
13
13
  CA_BUNDLE_PATH = File.expand_path("../../../data/ca_certificates.pem", __FILE__)
14
14
  DEFAULT_HEADERS = {
15
- user_agent: "OmiseRuby/#{Omise::VERSION} Ruby/#{RUBY_VERSION}"
15
+ user_agent: "OmiseRuby/#{Omise::VERSION} Ruby/#{RUBY_VERSION}",
16
16
  }
17
17
 
18
18
  def initialize(url, path, key)
19
- @uri = prepare_uri(url, path)
19
+ @uri = prepare_uri(url, path)
20
20
  @headers = prepare_headers
21
- @key = key
21
+ @key = key
22
22
 
23
- @resource = RestClient::Resource.new(@uri, {
24
- user: key,
25
- verify_ssl: OpenSSL::SSL::VERIFY_PEER,
26
- ssl_ca_file: CA_BUNDLE_PATH,
27
- headers: @headers,
28
- })
23
+ set_resource
29
24
  end
30
25
 
31
26
  attr_reader :uri, :headers, :key
32
27
 
33
28
  def get(attributes = {})
34
- @resource.get(params: attributes) { |r| Omise::Util.load_response(r) }
29
+ if attributes.any?
30
+ @uri.query = Omise::Util.generate_query(attributes)
31
+ set_resource
32
+ end
33
+
34
+ @resource.get { |r| Omise::Util.load_response(r) }
35
35
  end
36
36
 
37
37
  def patch(attributes = {})
@@ -48,10 +48,24 @@ module Omise
48
48
 
49
49
  private
50
50
 
51
+ def set_resource
52
+ @resource = RestClient::Resource.new(@uri.to_s, {
53
+ user: @key,
54
+ verify_ssl: OpenSSL::SSL::VERIFY_PEER,
55
+ ssl_ca_file: CA_BUNDLE_PATH,
56
+ headers: @headers,
57
+ })
58
+ end
59
+
51
60
  def prepare_uri(url, path)
52
- uri = URI.parse(url)
53
- uri.path = [uri.path, path].join
54
- uri.to_s
61
+ URI.parse(url).tap do |uri|
62
+ path = URI.parse(path)
63
+ query = path.query
64
+
65
+ path.query = nil
66
+ uri.path = [uri.path, path.to_s].join
67
+ uri.query = query
68
+ end
55
69
  end
56
70
 
57
71
  def prepare_headers