finix 0.15 → 0.16

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,15 +1,15 @@
1
1
  ---
2
2
  !binary "U0hBMQ==":
3
3
  metadata.gz: !binary |-
4
- ODliODZiNzMwY2VjMTQ2YzUwMWQ2ZjRjOWE5Mjc1NmY4OTkxNTgyNQ==
4
+ MTBhYjJkMzMzMDBlOGU1YWFjNzg3ZGE3ZGE5ZmIyNjUyNjdlOThmOA==
5
5
  data.tar.gz: !binary |-
6
- NDY0NjI3YzM2ZjNmZTVmNjRiZDdmNTA1MzJiNDhkNDZlNWZiN2RjYQ==
6
+ MjAwOWU5OGM0MzA0ODBjZTQyY2Q5MTIxZWM5MzA5ZWZjZjU2ODI1ZA==
7
7
  SHA512:
8
8
  metadata.gz: !binary |-
9
- ZjgwYzM1NGZiZmE5YTE3ZjM4ZGEyYTk5OTViNjI2ZDkxYzY1MTlmNzIyMTcw
10
- MTE3NDE3OGFjYjM3NGI3MmEwZmM0Y2FjNTBiN2UzZDE3YThiNDY2MWI4ZTYw
11
- YzU2MTkwOWQwNWY4NjIzMWJhZmI5NjU0NGFjNzc4MmFhYWIwNDc=
9
+ ZDIyMzA4YmYwMjYzMzhhNTkzZmJmYzQwMjRkNmMwZGNmZDFkZjYwMGJhZWM1
10
+ YmNlNDkyYmNjYjBhMTQ3YWQ3NTkxMmY2NDBiZDIxNmNkMmI5YzZlMmIzNDdl
11
+ Y2RlM2NjN2EzNDBiNzg5NWViMjM0OTU1NDE3NDYwYTkxZDI5NTA=
12
12
  data.tar.gz: !binary |-
13
- NDNlYWNmOTc5MTkxNzcyYTg1NzE3NzBmNDQ1ZTJhNmJmNDVjZjMzZjQwNmM4
14
- NDkxZTc5NjYwMzUxMTQyMGE0YTViYWY4ZWZlMjJiY2FlOGFkMDgxNDA5ZmE3
15
- MDdjZjIxODhiMTUwMjQ0ZWZlNDNlNzhhNmJlODhlM2I3MGQ4OGY=
13
+ MDRhNGViY2QxNmViYTAxNmM1ZDc1ZTA0YWQ0NmM3YzhlNTY4OGVkYmRiNjU5
14
+ MTIyZTI5OTVkMTlmOGE0YWViZDU0NWNlYzJkMjY0ZTIyMThkYmMwOTliOTYx
15
+ NWJkZTYwY2VmZjVlNGFjZDM1MmRiZWU4OTQzNTU4YmIyNTdmZWU=
data/.gitignore CHANGED
@@ -1,4 +1,11 @@
1
1
  Gemfile.lock
2
2
  .idea/
3
3
  *.gem
4
- .report/
4
+ .report/
5
+
6
+ *ignore*
7
+ .vcr/
8
+ *.iml
9
+ .byebug_history
10
+ .DS_Store
11
+ .env
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --color
2
+ --require spec_helper
data/Gemfile CHANGED
@@ -5,6 +5,13 @@ gem 'rake', '~> 10.4.2'
5
5
  group :test do
6
6
  gem 'minitest', '~> 5.10.1'
7
7
  gem 'minitest-reporters', '~> 1.1.13'
8
+ gem 'vcr', '~> 3.0.3'
9
+ gem 'rspec', '~> 3.5.0'
10
+ gem 'dotenv', '~> 2.1.1'
11
+
12
+ if RUBY_VERSION >= '2.0'
13
+ gem 'byebug', '~> 9.0.6'
14
+ end
8
15
  end
9
16
 
10
17
  gemspec
data/README.md CHANGED
@@ -3,4 +3,37 @@
3
3
  [![CircleCI](https://circleci.com/gh/finix-payments/processing-ruby.svg?style=svg&circle-token=326db95733638463152e926a1016ec0383e554d2)](https://circleci.com/gh/finix-payments/processing-ruby)
4
4
 
5
5
  ## Getting Started
6
- `gem install finix`
6
+ Add this line to your application's Gemfile:
7
+
8
+ gem 'finix'
9
+
10
+ Then execute:
11
+
12
+ $ bundle
13
+
14
+ Or install it yourself as:
15
+
16
+ $ gem install finix
17
+
18
+ ## Contributing
19
+
20
+ 1. Fork it
21
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
22
+ 3. Write your code **and unit tests**
23
+ 4. Ensure all tests still pass (`bundle exec rspec`)
24
+ 5. Commit your changes (`git commit -am 'Add some feature'`)
25
+ 6. Push to the branch (`git push origin my-new-feature`)
26
+ 7. Create new pull request
27
+
28
+
29
+ ### Tests
30
+
31
+ To run all tests:
32
+
33
+ $ bundle exec rspec
34
+
35
+
36
+ To run a specific test:
37
+
38
+ $ bundle exec rspec ./spec/finix/resources/application_spec.rb
39
+
data/Rakefile CHANGED
@@ -1,9 +1,21 @@
1
1
  require 'rake'
2
2
  require 'bundler/gem_tasks'
3
3
  require 'rake/testtask'
4
+ require 'rspec/core/rake_task'
5
+ require 'logger'
6
+ require_relative 'test/test_it'
7
+ require_relative 'test/helper_test'
4
8
 
5
9
  desc 'Run test suite'
6
10
  Rake::TestTask.new do |t|
7
- # t.test_files = FileList['test/**/test_*.rb']
11
+ Finix::FinixTest.logger = Logger.new STDOUT
12
+ Finix::FinixTest.logger.level = Logger::ERROR
8
13
  t.pattern ='test/**/test_*.rb'
9
14
  end
15
+
16
+ desc 'Run rspec suite'
17
+ RSpec::Core::RakeTask.new(:spec) do |t|
18
+ t.pattern = Dir.glob('spec/**/*_spec.rb')
19
+ end
20
+
21
+ task testit: [:test, :spec]
data/circle.yml CHANGED
@@ -3,7 +3,7 @@ machine:
3
3
  version: 1.9.3
4
4
 
5
5
  environment:
6
- RUBY_VERSIONS: 1.9.3,2.0.0,2.1.10,2.2.5,2.2.6,2.3.1,2.3.3,2.4.0-preview3
6
+ RUBY_VERSIONS: 1.9.3,2.0.0,2.1.10,2.2.5,2.2.6,2.3.1,2.3.3,2.4.0
7
7
  PROCESSING_URL: https://api-staging.finix.io/
8
8
 
9
9
  dependencies:
@@ -15,7 +15,7 @@ dependencies:
15
15
 
16
16
  test:
17
17
  override:
18
- - rvm $RUBY_VERSIONS --verbose do bundle exec rake test
18
+ - rvm $RUBY_VERSIONS --verbose do bundle exec rake testit
19
19
 
20
20
  post:
21
21
  - cp -Rf .report/ $CIRCLE_ARTIFACTS
@@ -1,6 +1,6 @@
1
- require 'finix/version' unless defined? Finix::VERSION
2
- require 'finix/client'
3
- require 'finix/error'
1
+ require_relative 'finix/version' unless defined? Finix::VERSION
2
+ require_relative 'finix/client'
3
+ require_relative 'finix/errors'
4
4
 
5
5
  module Finix
6
6
 
@@ -8,15 +8,15 @@ module Finix
8
8
  @config = {:root_url => 'https://localhost/processing'}
9
9
  @hypermedia_registry = {}
10
10
  @errors_registry = {
11
- :unknown => Finix::ResourceErrors,
12
- 400 => Finix::BadRequest,
13
- 401 => Finix::Unauthorized,
14
- 402 => Finix::PaymentRequired,
15
- 403 => Finix::Forbidden,
16
- 404 => Finix::NotFound,
17
- 405 => Finix::MethodNotAllowed,
18
- 422 => Finix::UnprocessableEntity,
19
- 500 => Finix::InternalServerError
11
+ :unknown => Errors,
12
+ 400 => BadRequest,
13
+ 401 => Unauthorized,
14
+ 402 => PaymentRequired,
15
+ 403 => Forbidden,
16
+ 404 => NotFound,
17
+ 405 => MethodNotAllowed,
18
+ 422 => UnprocessableEntity,
19
+ 500 => InternalServerError
20
20
  }
21
21
 
22
22
  class << self
@@ -35,7 +35,7 @@ module Finix
35
35
  @config[:user] = @config[:user].strip unless @config[:user].nil?
36
36
  @config[:password] = @config[:password].strip unless @config[:password].nil?
37
37
 
38
- @client = Finix::Client.new @config
38
+ @client = Client.new @config
39
39
  end
40
40
 
41
41
  def split_the_href(href)
@@ -43,13 +43,13 @@ module Finix
43
43
  end
44
44
 
45
45
  def get_href(cls)
46
- href = Finix.hypermedia_registry.key(cls)
46
+ href = hypermedia_registry.key(cls)
47
47
  sps = cls
48
48
  while href.nil?
49
49
  sps = sps.superclass
50
50
  break if sps.nil?
51
- clss = Finix::Utils.eval_class cls, sps.name.split('::').last
52
- href = Finix.hypermedia_registry.key(clss)
51
+ clss = Finix::Utils.eval_class cls, sps
52
+ href = hypermedia_registry.key(clss)
53
53
  end
54
54
  href
55
55
  end
@@ -57,11 +57,16 @@ module Finix
57
57
  def from_hypermedia_registry(href, attributes={})
58
58
  split_uri = split_the_href(href)
59
59
  split_uri.reverse!.each do |resource|
60
- cls = Finix.hypermedia_registry[resource]
61
- cls = cls.send :hypermedia_subtype, attributes if not cls.nil? and cls.respond_to?(:hypermedia_subtype)
60
+ cls = find_resource_cls(resource, attributes)
62
61
  return cls unless cls.nil?
63
62
  end
64
- Finix::Utils.eval_class self, 'UnknownResource'
63
+ Finix::Utils.eval_class self, UnknownResource
64
+ end
65
+
66
+ def find_resource_cls(resource, attributes={})
67
+ cls = hypermedia_registry[resource]
68
+ cls = cls.send :hypermedia_subtype, attributes if not cls.nil? and cls.respond_to?(:hypermedia_subtype)
69
+ cls
65
70
  end
66
71
 
67
72
  def get(*args, &block)
@@ -8,7 +8,7 @@ require 'finix/response/finix_error_middleware'
8
8
  module Finix
9
9
  class Client
10
10
 
11
- DefaultConfig = {
11
+ DEFAULT_CONFIG = {
12
12
  :logging_level => 'WARN',
13
13
  :connection_timeout => 60,
14
14
  :read_timeout => 60,
@@ -21,7 +21,7 @@ module Finix
21
21
  attr_accessor :config
22
22
 
23
23
  def initialize(options={})
24
- @config = DefaultConfig.merge options
24
+ @config = DEFAULT_CONFIG.merge options
25
25
  build_conn
26
26
  end
27
27
 
@@ -0,0 +1,47 @@
1
+ require_relative 'hal_resource'
2
+ module Finix
3
+
4
+ class Errors < ::StandardError
5
+ include Finix::HalResource
6
+ attr_reader :code
7
+ attr_reader :total
8
+
9
+ def initialize(response=nil)
10
+ @code = response[:status].to_i
11
+ @total = response[:body]['total'].to_i
12
+
13
+ load_page_from_response! response
14
+ @attributes['errors'] = @attributes.delete 'items'
15
+ @attributes.delete 'page'
16
+ end
17
+
18
+ def to_s
19
+ "#{@errors}"
20
+ end
21
+
22
+ end
23
+
24
+ class BadRequest < Errors
25
+ end
26
+
27
+ class Unauthorized < Errors
28
+ end
29
+
30
+ class PaymentRequired < Errors
31
+ end
32
+
33
+ class Forbidden < Errors
34
+ end
35
+
36
+ class NotFound < Errors
37
+ end
38
+
39
+ class MethodNotAllowed < Errors
40
+ end
41
+
42
+ class UnprocessableEntity < Errors
43
+ end
44
+
45
+ class InternalServerError < Errors
46
+ end
47
+ end
@@ -5,20 +5,41 @@ module Finix
5
5
  attr_accessor :attributes
6
6
 
7
7
  def method_missing(method, *args, &block)
8
- if @attributes.has_key?(method.to_s)
9
- return @attributes[method.to_s]
8
+ [@attributes, @attributes['page'] || {}].each do |attrs|
9
+ if attrs.has_key?(method.to_s)
10
+ return attrs[method.to_s]
11
+ end
12
+ end
13
+
14
+ if @attributes.empty? or (@attributes.has_key?('page') and not @attributes.has_key?('items'))
15
+ self.refresh if self.respond_to? :refresh
16
+ return self.send :method_missing, method, *args, &block
10
17
  end
11
18
 
12
19
  case method.to_s
13
- when /(.+)=$/
20
+ when /(.+)=$/ # support setting
14
21
  attr = method.to_s.chop
15
- @attributes[attr] = args[0]
22
+ @attributes[attr] = args.slice(0)
16
23
  else
17
- if @hyperlinks.has_key? "#{method}"
18
- value = @hyperlinks["#{method}"]
19
- result = value.call
20
- return result
21
- end
24
+ @hyperlinks.send :method_missing, method, *args, &block
25
+ end
26
+ end
27
+
28
+ def load_page_from_response!(response)
29
+ body = Finix::Utils.indifferent_read_access response.body
30
+
31
+ @hyperlinks = Finix::Utils.eval_class(self, IndifferentHash).new
32
+ links = body.delete('_links')
33
+ links.each { |key, link| @hyperlinks[key.to_sym] = link[:href] } unless links.nil?
34
+
35
+ @attributes = {'items' => [], 'page' => body.delete('page')} # clear attributes
36
+ if body.has_key? '_embedded'
37
+ resource_name, resources = body.delete('_embedded').first
38
+ @resource_class = Finix.from_hypermedia_registry resource_name
39
+ @attributes['items'] = resources.map do |attrs|
40
+ cls = Finix.from_hypermedia_registry resource_name, attrs
41
+ cls.construct_from_response attrs
42
+ end
22
43
  end
23
44
  end
24
45
  end
@@ -0,0 +1,12 @@
1
+ module Finix
2
+ class IndifferentHash < ::Hash
3
+ def method_missing(method, *args, &block)
4
+ if self.has_key? "#{method}"
5
+ value = self["#{method}"]
6
+ result = value.call
7
+ # result.init! args.slice(0) if result.respond_to? :init!
8
+ return result
9
+ end
10
+ end
11
+ end
12
+ end
@@ -1,80 +1,169 @@
1
1
  require 'cgi'
2
+ require 'ostruct'
2
3
  require_relative 'hal_resource'
4
+ require_relative 'indifferent_hash'
3
5
 
4
6
  module Finix
5
7
  class Pagination
6
8
 
7
- include Enumerable
9
+ include ::Enumerable
8
10
  include HalResource
9
11
 
10
12
  attr_accessor :resource_class
13
+ attr_reader :attributes
14
+ attr_reader :hyperlinks
11
15
 
12
- def initialize(href, opts={})
13
- @hyperlinks = {}
14
- @attributes = {}
16
+ def initialize(*args)
17
+ opts = args.slice!(0) || {}
18
+ href = opts.delete(:href)
15
19
 
20
+ @hyperlinks = Finix::Utils.eval_class(self, IndifferentHash).new
16
21
  @hyperlinks[:self] = href
22
+ @attributes = {}
17
23
  @resource_class = nil
24
+ extract_opts opts
25
+ end
26
+
27
+ def init!(*args)
28
+ opts = args.slice(0) || {}
29
+ extract_opts opts
30
+ self
18
31
  end
19
32
 
20
33
  def each
21
34
  return enum_for :each unless block_given?
22
- current
35
+ fetch_first
23
36
  loop do
24
37
  items.each { |item| yield item }
25
38
  fetch :next
26
39
  end
27
40
  end
28
41
 
29
- def current
30
- refresh unless items
31
- items
42
+ def count(*args)
43
+ refresh # always refresh to get last items
44
+ return super *args if block_given?
45
+ self.send :method_missing, :count, args
32
46
  end
33
47
 
34
- def fetch(scope) # :next, :last, :first, :prev, :self
35
- scope = scope.to_s.to_sym
48
+ def fetch(scope = nil) # :next, :last, :first, :prev, :self
49
+ opts = {}
50
+ if scope.is_a? Hash
51
+ opts = Finix::Utils.indifferent_read_access scope
52
+ scope = nil
53
+ end
54
+
55
+ scope = :self if scope.nil?
56
+ scope = scope.to_s.to_sym unless scope.nil?
36
57
  if @hyperlinks[scope]
37
- load_from @hyperlinks[scope]
58
+ load_from @hyperlinks[scope], opts
38
59
  return self.items
39
60
  end
40
61
 
41
62
  raise StopIteration
42
63
  end
43
64
 
65
+ alias retrieve fetch
66
+
44
67
  def refresh
45
- fetch :self
68
+ fetch
69
+ self
46
70
  end
47
71
 
48
72
  def create(attrs={})
49
- attrs = attrs.attributes if attrs.is_a?(Finix::Resource)
73
+ attrs = attrs.attributes if attrs.is_a?(Resource)
50
74
  attrs = Finix::Utils.indifferent_read_access attrs
75
+
51
76
  href = @hyperlinks[:self]
52
77
  @resource_class = Finix.from_hypermedia_registry href, attrs
53
- @resource_class.new(attrs, href).save
78
+
79
+ attrs[:href] = href
80
+ @resource_class.new(attrs).save
54
81
  end
55
82
 
56
83
  private
57
84
 
58
- def load_from(url, params = {})
59
- params ||= {}
85
+ def fetch_first
86
+ @attributes['page']['offset'] = 0 unless @attributes['page']['offset'].nil?
87
+ fetch(@hyperlinks[:first] ? :first : nil)
88
+ end
89
+
90
+ def extract_opts(opts={})
91
+ opts = Finix::Utils.indifferent_read_access opts
92
+ limit = opts.delete('limit')
93
+ offset = opts.delete('offset')
94
+ @attributes['page'] = @attributes['page'] || {}
95
+ @attributes['page']['limit'] = limit unless limit.nil?
96
+ @attributes['page']['offset'] = offset unless offset.nil?
97
+ @attributes.merge! opts unless opts.empty?
98
+
99
+ if not limit.nil? or not offset.nil? # reset @hyperlinks
100
+ @hyperlinks.reject! {|k, v| k.to_s != 'self'}
101
+ parsed_url = URI.parse(@hyperlinks[:self])
102
+ parsed_url.query = nil
103
+ @hyperlinks[:self] = parsed_url.to_s
104
+ end
105
+ end
60
106
 
61
- response = Finix.get url, params
62
- body = Finix::Utils.indifferent_read_access response.body
107
+ def load_from(url, opts = {})
108
+ parsed_url = URI.parse(url)
63
109
 
64
- links = body.delete('_links')
65
- links.each { |key, link| @hyperlinks[key.to_sym] = link[:href] }
110
+ params = {}
111
+ params.merge! @attributes['page'] if @attributes.has_key? 'page'
112
+ params.merge! parse_query(parsed_url.query)
113
+ parsed_url.query = nil # remove query
66
114
 
67
- @attributes = {} # clear attributes
68
- if body.has_key? '_embedded'
69
- resource_name, resources = body.delete('_embedded').first
70
- @resource_class = Finix.from_hypermedia_registry resource_name
71
- @attributes['items'] = resources.map do |attrs|
72
- cls = Finix.from_hypermedia_registry resource_name, attrs
73
- cls.construct_from_response attrs
74
- end
75
- @attributes['page'] = body.delete('page')
115
+ # params page
116
+ opts ||= {}
117
+ page = opts.delete('page')
118
+ unless page.nil?
119
+ page = Finix::Utils.indifferent_read_access page
120
+ params.merge! page unless page.nil?
76
121
  end
122
+
123
+ params.merge! opts unless opts.empty?
124
+ params.delete('count') # remove count from previous query
125
+
126
+ response = Finix.get parsed_url.to_s, params
127
+ load_page_from_response! response
128
+ # body = Finix::Utils.indifferent_read_access response.body
129
+ #
130
+ # @hyperlinks = Finix::Utils.eval_class(self, IndifferentHash).new
131
+ # links = body.delete('_links')
132
+ # links.each { |key, link| @hyperlinks[key.to_sym] = link[:href] }
133
+ #
134
+ # @attributes = {'items' => [], 'page' => body.delete('page')} # clear attributes
135
+ # if body.has_key? '_embedded'
136
+ # resource_name, resources = body.delete('_embedded').first
137
+ # @resource_class = Finix.from_hypermedia_registry resource_name
138
+ # @attributes['items'] = resources.map do |attrs|
139
+ # cls = Finix.from_hypermedia_registry resource_name, attrs
140
+ # cls.construct_from_response attrs
141
+ # end
142
+ # end
77
143
  end
78
144
 
145
+ # Stolen from Mongrel, with some small modifications:
146
+ # Parses a query string by breaking it up at the '&'
147
+ # and ';' characters. You can also use this to parse
148
+ # cookies by changing the characters used in the second
149
+ # parameter (which defaults to '&;').
150
+ def parse_query(qs, d = nil)
151
+ params = {}
152
+ (qs || '').split(d ? /[#{d}] */n : /[&;] */n).each do |p|
153
+ k, v = p.split('=', 2).map { |x| CGI::unescape(x) }
154
+ if (cur = params[k])
155
+ if cur.class == Array
156
+ params[k] << v
157
+ else
158
+ params[k] = [cur, v]
159
+ end
160
+ else
161
+ params[k] = v
162
+ end
163
+ end
164
+
165
+ params
166
+ end
79
167
  end
80
- end
168
+ end
169
+
@@ -18,4 +18,5 @@ require 'finix/resources/verification'
18
18
  require 'finix/resources/dispute'
19
19
  require 'finix/resources/evidence'
20
20
  require 'finix/resources/refund'
21
- require 'finix/resources/settlement'
21
+ require 'finix/resources/settlement'
22
+ require 'finix/resources/error'
@@ -14,6 +14,7 @@ module Finix
14
14
  attrs = attrs.attributes if attrs.is_a?(Finix::Resource)
15
15
  self.processors.create(attrs)
16
16
  end
17
+ alias enable_processor create_processor
17
18
 
18
19
  def create_token(attrs={})
19
20
  attrs = attrs.attributes if attrs.is_a?(Finix::Resource)
@@ -11,6 +11,9 @@ module Finix
11
11
  end
12
12
 
13
13
  def capture(attrs={})
14
+ if attrs['capture_amount'].nil?
15
+ attrs['capture_amount'] = self.amount
16
+ end
14
17
  self.attributes = self.attributes.merge attrs
15
18
  self.save
16
19
  end
@@ -0,0 +1,12 @@
1
+ module Finix
2
+ class Error < ::StandardError
3
+ include Finix::Resource
4
+ include Finix::HypermediaRegistry
5
+
6
+ define_hypermedia_types [:errors]
7
+
8
+ def message
9
+ @attributes['message']
10
+ end
11
+ end
12
+ end
@@ -1,29 +1,39 @@
1
- require File.expand_path('../../pagination', __FILE__)
2
- require File.expand_path('../../utils', __FILE__)
3
- require File.expand_path('../../hal_resource', __FILE__)
4
1
  require 'addressable/template'
5
2
 
3
+ require_relative '../utils'
4
+ require_relative '../hal_resource'
5
+ require_relative '../pagination'
6
+ require_relative '../indifferent_hash'
7
+
6
8
  module Finix
7
9
 
8
10
  module Resource
9
11
 
10
12
  include HalResource
11
13
 
12
- def initialize(attributes = {}, href = nil)
13
- @attributes = Utils.indifferent_read_access attributes
14
+ def initialize(*args)
15
+ opts = args.slice!(0) || {}
16
+ href = opts.delete(:href)
17
+ @attributes = Finix::Utils.indifferent_read_access opts
14
18
 
15
- @hyperlinks = {}
19
+ @hyperlinks = Finix::Utils.eval_class(self, IndifferentHash).new
16
20
  @hyperlinks[:self] = href if href =~ URI::regexp
17
21
  end
18
22
 
23
+ alias links hyperlinks
24
+
19
25
  def hydrate(links)
20
26
  links.each do |key, link|
21
27
  property = key.sub(/.*?\./, '')
22
28
 
29
+ href = link[:href]
23
30
  if property == 'self'
24
- @hyperlinks[:self] = link[:href]
31
+ @hyperlinks[:self] = href
25
32
  else
26
- @hyperlinks[property] = Finix::Utils.callable(Finix::Utils.eval_class(self, 'Pagination').new link[:href], {})
33
+ split_uri = Finix.split_the_href(href).reverse!
34
+ cls = Finix.find_resource_cls split_uri[0]
35
+ cls = cls.nil? ? Finix.from_hypermedia_registry(href) : Finix::Utils.eval_class(self, Pagination)
36
+ @hyperlinks[property] = Finix::Utils.callable(cls.new :href => href)
27
37
  end
28
38
  end
29
39
  end
@@ -96,9 +106,9 @@ module Finix
96
106
 
97
107
  module ClassMethods
98
108
 
109
+ # this is class method, not callable from instance
99
110
  def construct_from_response(payload)
100
111
  payload = Finix::Utils.indifferent_read_access payload
101
-
102
112
  links = payload.delete('_links') || {}
103
113
  instance = self.new payload
104
114
  instance.hydrate(links)
@@ -108,16 +118,20 @@ module Finix
108
118
  def fetch(*arguments)
109
119
  if arguments.nil? or arguments.empty? or arguments[0].nil? or arguments[0].to_s.empty?
110
120
  href = Finix.hypermedia_registry.key(self)
111
- return Finix::Utils.eval_class(self, 'Pagination').new href
121
+ return Finix::Utils.eval_class(self, Pagination).new :href => href
112
122
  end
113
123
 
114
124
  options = arguments.slice!(0) or {}
115
125
  if options.is_a? String and options =~ URI::regexp
116
126
  href = options
117
127
  else
118
- href = Finix.hypermedia_registry.key(self) or Finix.hypermedia_registry.key(self.class)
119
- id = options if options.is_a? String
120
- id = options.delete(:id) if options.is_a? Hash
128
+ href = Finix.get_href(self) or Finix.get_href(self.class)
129
+ if options.is_a? Hash
130
+ options = Finix::Utils.indifferent_read_access options
131
+ id = options.delete('id')
132
+ elsif options.is_a? String
133
+ id = options
134
+ end
121
135
  href = "#{href}/#{id}" unless id.nil?
122
136
  end
123
137
 
@@ -125,6 +139,13 @@ module Finix
125
139
  construct_from_response response.body
126
140
  end
127
141
 
142
+ def pagination(*args)
143
+ href = Finix.hypermedia_registry.key(self)
144
+ opts = args.slice!(0) || {}
145
+ opts[:href] = href
146
+ Finix::Utils.eval_class(self, Pagination).new opts
147
+ end
148
+
128
149
  alias find fetch
129
150
  alias retrieve fetch
130
151
  end
@@ -1,5 +1,5 @@
1
1
  require 'faraday'
2
- require 'finix/error'
2
+ require_relative '../errors'
3
3
 
4
4
  module Faraday
5
5
 
@@ -1,8 +1,9 @@
1
1
  module Finix
2
2
  module Utils
3
- def eval_class(slf, name)
4
- mod = slf.class.name.split("::").first unless slf.kind_of? Class or slf.kind_of? Module
5
- mod = slf.name.split("::").first if mod.nil?
3
+ def eval_class(slf, cls)
4
+ mod = slf.class.name.sub(/::.*/, '') unless slf.kind_of? Class or slf.kind_of? Module
5
+ mod = slf.name.sub(/::.*/, '') if mod.nil?
6
+ name = demodulize cls.name
6
7
  self.instance_eval "#{mod}::#{name}"
7
8
  end
8
9
 
@@ -39,6 +40,10 @@ module Finix
39
40
  indifferent
40
41
  end
41
42
 
43
+ def demodulize(class_name_in_module)
44
+ class_name_in_module.to_s.sub(/^.*::/, '')
45
+ end
46
+
42
47
  extend self
43
48
  end
44
49
  end
@@ -1,3 +1,3 @@
1
1
  module Finix
2
- VERSION = '0.15'
2
+ VERSION = '0.16'
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: finix
3
3
  version: !ruby/object:Gem::Version
4
- version: '0.15'
4
+ version: '0.16'
5
5
  platform: ruby
6
6
  authors:
7
7
  - finix-payments
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-12-13 00:00:00.000000000 Z
11
+ date: 2016-12-27 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: faraday
@@ -88,6 +88,7 @@ extensions: []
88
88
  extra_rdoc_files: []
89
89
  files:
90
90
  - .gitignore
91
+ - .rspec
91
92
  - .ruby-gemset
92
93
  - .ruby-version
93
94
  - Gemfile
@@ -98,14 +99,16 @@ files:
98
99
  - finix.gemspec
99
100
  - lib/finix.rb
100
101
  - lib/finix/client.rb
101
- - lib/finix/error.rb
102
+ - lib/finix/errors.rb
102
103
  - lib/finix/hal_resource.rb
104
+ - lib/finix/indifferent_hash.rb
103
105
  - lib/finix/pagination.rb
104
106
  - lib/finix/resources.rb
105
107
  - lib/finix/resources/application.rb
106
108
  - lib/finix/resources/authorization.rb
107
109
  - lib/finix/resources/bank_account.rb
108
110
  - lib/finix/resources/dispute.rb
111
+ - lib/finix/resources/error.rb
109
112
  - lib/finix/resources/evidence.rb
110
113
  - lib/finix/resources/hypermedia.rb
111
114
  - lib/finix/resources/identity.rb
@@ -1,44 +0,0 @@
1
- module Finix
2
-
3
- class ResourceErrors < ::StandardError
4
- attr_reader :response
5
- attr_reader :errors
6
- attr_reader :code
7
-
8
- def initialize(response=nil)
9
- @response = response
10
- @errors = []
11
- unless response.nil?
12
- @code = response[:status].to_i
13
- @response[:body]['_embedded']['errors'].each { |error| @errors.push Utils.eval_class(self, 'ResourceError').new error}
14
- end
15
- end
16
-
17
- def to_s
18
- "#{@errors}"
19
- end
20
-
21
- end
22
-
23
- class ResourceError < ::StandardError
24
- attr_reader :attributes
25
-
26
- def initialize(error = {})
27
- @attributes = {}
28
- @attributes = @attributes.merge error
29
- end
30
-
31
- def to_s
32
- "#{self.class.name.split('::').last || ''} #{@attributes}"
33
- end
34
- end
35
-
36
- class BadRequest < ResourceErrors; end
37
- class Unauthorized < ResourceErrors; end
38
- class PaymentRequired < ResourceErrors; end
39
- class Forbidden < ResourceErrors; end
40
- class NotFound < ResourceErrors; end
41
- class MethodNotAllowed < ResourceErrors; end
42
- class UnprocessableEntity < ResourceErrors; end
43
- class InternalServerError < ResourceErrors; end
44
- end