finix 0.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (43) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +4 -0
  3. data/.ruby-gemset +1 -0
  4. data/.ruby-version +1 -0
  5. data/Gemfile +14 -0
  6. data/LICENSE.txt +21 -0
  7. data/README.md +6 -0
  8. data/Rakefile +8 -0
  9. data/bin/console +14 -0
  10. data/bin/setup +8 -0
  11. data/circle.yml +18 -0
  12. data/finix.gemspec +31 -0
  13. data/lib/finix.rb +66 -0
  14. data/lib/finix/client.rb +88 -0
  15. data/lib/finix/error.rb +33 -0
  16. data/lib/finix/hal_resource.rb +39 -0
  17. data/lib/finix/pagination.rb +78 -0
  18. data/lib/finix/resources.rb +20 -0
  19. data/lib/finix/resources/application.rb +23 -0
  20. data/lib/finix/resources/authorization.rb +18 -0
  21. data/lib/finix/resources/bank_account.rb +9 -0
  22. data/lib/finix/resources/dispute.rb +14 -0
  23. data/lib/finix/resources/evidence.rb +8 -0
  24. data/lib/finix/resources/hypermedia.rb +26 -0
  25. data/lib/finix/resources/identity.rb +36 -0
  26. data/lib/finix/resources/merchant.rb +8 -0
  27. data/lib/finix/resources/payment_card.rb +9 -0
  28. data/lib/finix/resources/payment_instrument.rb +8 -0
  29. data/lib/finix/resources/processor.rb +8 -0
  30. data/lib/finix/resources/refund.rb +8 -0
  31. data/lib/finix/resources/resource.rb +148 -0
  32. data/lib/finix/resources/settlement.rb +8 -0
  33. data/lib/finix/resources/token.rb +8 -0
  34. data/lib/finix/resources/transfer.rb +12 -0
  35. data/lib/finix/resources/unknown_resource.rb +5 -0
  36. data/lib/finix/resources/user.rb +13 -0
  37. data/lib/finix/resources/verifiable.rb +12 -0
  38. data/lib/finix/resources/verification.rb +8 -0
  39. data/lib/finix/resources/webhook.rb +8 -0
  40. data/lib/finix/response/finix_error_middleware.rb +26 -0
  41. data/lib/finix/utils.rb +108 -0
  42. data/lib/finix/version.rb +3 -0
  43. metadata +177 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: b0e8ae111ff460e0564223a26c61921e2bd835d6
4
+ data.tar.gz: 3a5b69d952123b1b18bceeb8b5a2079b1807d47b
5
+ SHA512:
6
+ metadata.gz: 117ba294a0bd85a1ba0e21f608950be0601bbf7260d020b16a7bfec473dc4b4e143df522433a2451166ca19bd0f6d9c2e55bad1fcdd889154f4541c5dc6f9671
7
+ data.tar.gz: 2d021614c589cc51e93998bc2cfb6109a7d2bd18d165d3a57f92b4eca0cdaadf0483e5bf8d70c28bd37acdff3408141a8189ee318e35308a529720328d8fb74b
data/.gitignore ADDED
@@ -0,0 +1,4 @@
1
+ Gemfile.lock
2
+ .idea/
3
+ *.gem
4
+ .html/
data/.ruby-gemset ADDED
@@ -0,0 +1 @@
1
+ finix
data/.ruby-version ADDED
@@ -0,0 +1 @@
1
+ ruby-2.1.5
data/Gemfile ADDED
@@ -0,0 +1,14 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in processing-ruby.gemspec
4
+ gemspec
5
+
6
+ gem 'faraday'
7
+ gem 'faraday_middleware'
8
+ gem 'addressable'
9
+ gem 'json'
10
+
11
+ group :test do
12
+ gem 'minitest'
13
+ gem 'minitest-reporters'
14
+ end
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2016 Finix Payments, Inc.
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,6 @@
1
+ # processing-ruby
2
+
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
+
5
+ ## Getting Started
6
+ `gem install finix`
data/Rakefile ADDED
@@ -0,0 +1,8 @@
1
+ require 'rake'
2
+ require 'bundler/gem_tasks'
3
+ require 'rake/testtask'
4
+
5
+ desc 'Run test suite'
6
+ Rake::TestTask.new do |t|
7
+ t.test_files = FileList['test/**/test_*.rb']
8
+ end
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'bundler/setup'
4
+ require 'finix'
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require 'irb'
14
+ IRB.start
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
data/circle.yml ADDED
@@ -0,0 +1,18 @@
1
+ machine:
2
+ ruby:
3
+ version: 2.1.5
4
+
5
+ environment:
6
+ PROCESSING_URL: https://api-staging.finix.io/
7
+
8
+ dependencies:
9
+ override:
10
+ - gem install bundler
11
+ - bundle install
12
+
13
+ test:
14
+ override:
15
+ - rake test
16
+
17
+ post:
18
+ - cp -Rf .report $CIRCLE_ARTIFACTS
data/finix.gemspec ADDED
@@ -0,0 +1,31 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'finix/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = 'finix'
8
+ spec.version = Finix::VERSION
9
+ spec.authors = ['finix-payments']
10
+ spec.email = ['support@finixpayments.com']
11
+
12
+ spec.summary = %q{Finix Ruby Client}
13
+ spec.description = %q{Finix Ruby Client}
14
+ spec.homepage = 'https://www.finixpayments.com'
15
+ spec.license = 'MIT'
16
+
17
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
18
+ f.match(%r{^(test|spec|features)/})
19
+ end
20
+ spec.bindir = 'bin'
21
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
22
+ spec.require_paths = ['lib']
23
+
24
+ spec.add_development_dependency 'bundler', '~> 1.13'
25
+ spec.add_development_dependency 'rake', '~> 10.0'
26
+ spec.add_development_dependency 'rspec', '~> 3.0'
27
+
28
+ spec.add_dependency('faraday', ['>= 0.8.6', '<= 0.9.0'])
29
+ spec.add_dependency('faraday_middleware', '~> 0.9.0')
30
+ spec.add_dependency('addressable', '~> 2.3.5')
31
+ end
data/lib/finix.rb ADDED
@@ -0,0 +1,66 @@
1
+ require 'finix/version' unless defined? Finix::VERSION
2
+
3
+ module Finix
4
+
5
+ @client = nil
6
+ @config = {:root_url=>'https://localhost/processing'}
7
+ @hypermedia_registry = {}
8
+
9
+ class << self
10
+
11
+ attr_accessor :client
12
+ attr_accessor :config
13
+ attr_accessor :hypermedia_registry
14
+
15
+ def configure(options={})
16
+ unless options[:root_url].nil? # reset us/pass as well if user update :root_url
17
+ @config = {}
18
+ end
19
+
20
+ @config = @config.merge(options)
21
+ @config[:user] = @config[:user].strip unless @config[:user].nil?
22
+ @config[:password] = @config[:password].strip unless @config[:password].nil?
23
+
24
+ @client = Finix::Client.new @config
25
+ end
26
+
27
+ def split_the_href(href)
28
+ URI.parse(href).path.sub(/\/$/, '').split('/')
29
+ end
30
+
31
+ def from_hypermedia_registry(resource_name)
32
+ cls = Finix.hypermedia_registry[resource_name]
33
+ if cls.nil?
34
+ return Finix::UnknownResource
35
+ end
36
+ cls
37
+ end
38
+
39
+ def from_href(href)
40
+ split_uri = split_the_href(href)
41
+ split_uri.reverse!.each do |resource|
42
+ cls = Finix.hypermedia_registry[resource]
43
+ return cls unless cls.nil?
44
+ end
45
+ end
46
+
47
+ def is_collection(href)
48
+ split_uri = split_the_href(href)
49
+ Finix.hypermedia_registry.has_key?(split_uri.last)
50
+ end
51
+
52
+ def get(*args, &block)
53
+ self.client.get *args
54
+ end
55
+
56
+ def post(*args, &block)
57
+ self.client.post *args
58
+ end
59
+
60
+ def put(*args, &block)
61
+ self.client.put *args
62
+ end
63
+ end
64
+ end
65
+
66
+ require 'finix/resources'
@@ -0,0 +1,88 @@
1
+ require 'logger'
2
+ require 'uri'
3
+ require 'faraday'
4
+ require 'faraday_middleware'
5
+ require 'finix/response/finix_error_middleware'
6
+
7
+
8
+ module Finix
9
+ class Client
10
+
11
+ DEFAULTS = {
12
+ :logging_level => 'WARN',
13
+ :connection_timeout => 60,
14
+ :read_timeout => 60,
15
+ :logger => nil,
16
+ :ssl_verify => true,
17
+ :faraday_adapter => Faraday.default_adapter,
18
+ }
19
+
20
+ attr_reader :conn
21
+ attr_accessor :config
22
+
23
+ def initialize(options={})
24
+ @config = DEFAULTS.merge options
25
+ build_conn
26
+ end
27
+
28
+ def build_conn
29
+ if config[:logger]
30
+ logger = config[:logger]
31
+ else
32
+ logger = Logger.new(STDOUT)
33
+ logger.level = Logger.const_get(config[:logging_level].to_s)
34
+ end
35
+
36
+ Faraday::Response.register_middleware :handle_api_errors => lambda { Faraday::Response::RaiseApiError }
37
+
38
+ options = {
39
+ :request => {
40
+ :open_timeout => config[:connection_timeout],
41
+ :timeout => config[:read_timeout]
42
+ },
43
+ :ssl => {
44
+ :verify => @config[:ssl_verify] # Only set this to false for testing
45
+ }
46
+ }
47
+ @conn = Faraday.new(@config[:url], options) do |cxn|
48
+ cxn.request :multipart
49
+ cxn.request :url_encoded
50
+ cxn.request :json
51
+ cxn.response :logger, logger
52
+ cxn.response :handle_api_errors
53
+ cxn.response :json
54
+ cxn.adapter config[:faraday_adapter]
55
+ end
56
+ conn.headers['User-Agent'] = "finix-ruby/#{Finix::VERSION}"
57
+ end
58
+
59
+ def method_missing(method, *args, &block)
60
+ href_or_uri = args[0]
61
+ args[0] = "#{@config[:root_url]}/#{href_or_uri}" unless href_or_uri =~ /\A#{URI::regexp(%w(http https))}\z/
62
+ if is_http_method? method
63
+ # TODO use auth property
64
+ conn.basic_auth(@config[:user], @config[:password]) unless @config[:user].nil? and @config[:password].nil?
65
+ conn.send method, *args do |req|
66
+ req.headers['Content-Type'] = 'application/json'
67
+ req.headers['Content-Type'] = 'multipart/form-data' if ((not req.body.nil?) and req.body.key?('file'))
68
+ end
69
+ else
70
+ super method, *args, &block
71
+ end
72
+ end
73
+
74
+ private
75
+
76
+ def is_http_method? method
77
+ [:get, :post, :put, :delete].include? method
78
+ end
79
+
80
+ def respond_to?(method, include_private = false)
81
+ if is_http_method? method
82
+ true
83
+ else
84
+ super method, include_private
85
+ end
86
+ end
87
+ end
88
+ end
@@ -0,0 +1,33 @@
1
+ module Finix
2
+
3
+ class ResourceErrors < ::StandardError
4
+ attr_reader :response
5
+ attr_reader :errors
6
+
7
+ def initialize(response=nil)
8
+ @response = response
9
+ @errors = []
10
+ @response[:body]['_embedded']['errors'].each { |error| @errors.push Finix::ResourceError.new error}
11
+ end
12
+
13
+ def to_s
14
+ "#{@errors}"
15
+ end
16
+ end
17
+
18
+ class ResourceError
19
+ attr_reader :attributes
20
+
21
+ def initialize(error = {})
22
+ @attributes = {}
23
+ @attributes = @attributes.merge error
24
+ end
25
+ end
26
+
27
+ class BadRequest < ResourceErrors; end
28
+ class Unauthorized < ResourceErrors; end
29
+ class Forbidden < ResourceErrors; end
30
+ class NotFound < ResourceErrors; end
31
+ class MethodNotAllowed < ResourceErrors; end
32
+ class UnprocessableEntity < ResourceErrors; end
33
+ end
@@ -0,0 +1,39 @@
1
+ module Finix
2
+ module HalResource
3
+
4
+ attr_accessor :hyperlinks
5
+ attr_accessor :attributes
6
+
7
+ def method_missing(method, *args, &block)
8
+ if @attributes.has_key?(method.to_s)
9
+ return @attributes[method.to_s]
10
+ end
11
+
12
+ case method.to_s
13
+ when /(.+)=$/
14
+ attr = method.to_s.chop
15
+ @attributes[attr] = args[0]
16
+ else
17
+ if @hyperlinks.has_key? "#{method}"
18
+ value = @hyperlinks["#{method}"]
19
+ result = value.call
20
+ return result
21
+ end
22
+ end
23
+ end
24
+
25
+ if RUBY_VERSION < '1.9'
26
+ def respond_to?(method_name, include_private=false)
27
+ does_resource_respond_to?(method_name) || super
28
+ end
29
+ else
30
+ def respond_to_missing?(method_name, include_private=false)
31
+ does_resource_respond_to?(method_name) || super
32
+ end
33
+ end
34
+
35
+ def does_resource_respond_to?(method_name)
36
+ @attributes.has_key?(method_name.to_s) or @hyperlinks.has_key?(method_name.to_s)
37
+ end
38
+ end
39
+ end
@@ -0,0 +1,78 @@
1
+ require 'cgi'
2
+ require_relative 'hal_resource'
3
+
4
+ module Finix
5
+ class Pagination
6
+
7
+ include Enumerable
8
+ include HalResource
9
+
10
+ attr_accessor :resource_class
11
+
12
+ def initialize(href, opts={})
13
+ @hyperlinks = {}
14
+ @attributes = {}
15
+
16
+ @hyperlinks[:self] = href
17
+ @resource_class = nil
18
+ end
19
+
20
+ def each
21
+ return enum_for :each unless block_given?
22
+ current
23
+ loop do
24
+ items.each { |item| yield item }
25
+ fetch :next
26
+ end
27
+ end
28
+
29
+ def current
30
+ refresh unless items
31
+ items
32
+ end
33
+
34
+ def fetch(scope) # :next, :last, :first, :prev, :self
35
+ scope = scope.to_s.to_sym
36
+ if @hyperlinks[scope]
37
+ load_from @hyperlinks[scope]
38
+ return self.items
39
+ end
40
+
41
+ raise StopIteration
42
+ end
43
+
44
+ def refresh
45
+ fetch :self
46
+ end
47
+
48
+ def create(attrs={})
49
+ attrs = attrs.attributes if attrs.is_a?(Finix::Resource)
50
+ attrs = Finix::Utils.indifferent_read_access attrs
51
+ href = @hyperlinks[:self]
52
+ if @resource_class.nil?
53
+ @resource_class = Finix.from_href(href)
54
+ end
55
+ @resource_class.new(attrs, href).save
56
+ end
57
+
58
+ private
59
+
60
+ def load_from(url, params = {})
61
+ params ||= {}
62
+
63
+ response = Finix.get url, params
64
+ body = Finix::Utils.indifferent_read_access response.body
65
+
66
+ links = body.delete('_links')
67
+ links.each { |key, link| @hyperlinks[key.to_sym] = link[:href] }
68
+
69
+ @attributes = {} # clear attributes
70
+ if body.has_key? '_embedded'
71
+ resource_name, resources = body.delete('_embedded').first
72
+ @resource_class = Finix.from_hypermedia_registry resource_name
73
+ @attributes['items'] = resources.map { |item| @resource_class.construct_from_response item }
74
+ end
75
+ end
76
+
77
+ end
78
+ end
@@ -0,0 +1,20 @@
1
+ require 'finix/resources/verifiable'
2
+ require 'finix/resources/resource'
3
+ require 'finix/resources/unknown_resource'
4
+ require 'finix/resources/hypermedia'
5
+ require 'finix/resources/user'
6
+ require 'finix/resources/application'
7
+ require 'finix/resources/identity'
8
+ require 'finix/resources/processor'
9
+ require 'finix/resources/token'
10
+ require 'finix/resources/merchant'
11
+ require 'finix/resources/payment_instrument'
12
+ require 'finix/resources/payment_card'
13
+ require 'finix/resources/bank_account'
14
+ require 'finix/resources/transfer'
15
+ require 'finix/resources/authorization'
16
+ require 'finix/resources/webhook'
17
+ require 'finix/resources/verification'
18
+ require 'finix/resources/dispute'
19
+ require 'finix/resources/evidence'
20
+ require 'finix/resources/refund'
@@ -0,0 +1,23 @@
1
+ module Finix
2
+ class Application
3
+ include Finix::Resource
4
+ include Finix::HypermediaRegistry
5
+
6
+ define_hypermedia_types [:applications]
7
+
8
+ def create_partner_user(attrs={})
9
+ attrs = attrs.attributes if attrs.is_a?(Finix::Resource)
10
+ self.users.create(attrs)
11
+ end
12
+
13
+ def create_processor(attrs={})
14
+ attrs = attrs.attributes if attrs.is_a?(Finix::Resource)
15
+ self.processors.create(attrs)
16
+ end
17
+
18
+ def create_token(attrs={})
19
+ attrs = attrs.attributes if attrs.is_a?(Finix::Resource)
20
+ self.tokens.create(attrs)
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,18 @@
1
+ module Finix
2
+ class Authorization
3
+ include Finix::Resource
4
+ include Finix::HypermediaRegistry
5
+
6
+ define_hypermedia_types [:authorizations]
7
+
8
+ def void
9
+ self['void_me'] = true
10
+ self.save
11
+ end
12
+
13
+ def capture(attrs={})
14
+ self.attributes = self.attributes.merge attrs
15
+ self.save
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,9 @@
1
+ module Finix
2
+ class BankAccount < PaymentInstrument
3
+
4
+ def initialize(attributes = {}, self_href = nil)
5
+ super(attributes, self_href)
6
+ self.type = 'BANK_ACCOUNT'
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,14 @@
1
+ module Finix
2
+ class Dispute
3
+ include Finix::Resource
4
+ include Finix::HypermediaRegistry
5
+
6
+ define_hypermedia_types [:disputes]
7
+
8
+ def upload_evidence(evidence_file_path)
9
+ # self.evidence.file = Faraday::UploadIO.new(evidence_file_path, 'application/octet-stream')
10
+ # self.evidence.file = Faraday::UploadIO.new(evidence_file_path, 'application/octet-stream')
11
+ self.evidence.create :file => Faraday::UploadIO.new(evidence_file_path, 'application/octet-stream')
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,8 @@
1
+ module Finix
2
+ class Evidence
3
+ include Finix::Resource
4
+ include Finix::HypermediaRegistry
5
+
6
+ define_hypermedia_types [:evidence]
7
+ end
8
+ end
@@ -0,0 +1,26 @@
1
+ module Finix
2
+
3
+ module HypermediaRegistry
4
+
5
+ def self.included(base) # :nodoc:
6
+ base.extend ClassMethods
7
+ end
8
+
9
+ module ClassMethods
10
+
11
+ def define_hypermedia_types(types)
12
+ @hypermedia_types = types.map! do |t|
13
+ t.to_s
14
+ end.sort!.freeze
15
+
16
+ @hypermedia_types.each do |type|
17
+ Finix.hypermedia_registry[type] = self
18
+ end
19
+ end
20
+
21
+ attr_reader :hypermedia_types
22
+ end
23
+
24
+ end
25
+
26
+ end
@@ -0,0 +1,36 @@
1
+ module Finix
2
+ class Identity
3
+ include Finix::Resource
4
+ include Finix::HypermediaRegistry
5
+ include Finix::Verifiable
6
+
7
+ define_hypermedia_types [:identities]
8
+
9
+ def provision_merchant_on(attrs={})
10
+ self.merchants.create(attrs)
11
+ end
12
+
13
+ def create_payment_instrument(attrs={})
14
+ if attrs.is_a?(Finix::Resource)
15
+ attrs.identity = self.id
16
+ attrs = attrs.attributes
17
+ else
18
+ attrs['identity'] = self.id # TODO might be error here
19
+ end
20
+
21
+ if attrs['type'] == 'PAYMENT_CARD'
22
+ self.payment_instruments.resource_class = Finix::PaymentCard
23
+ elsif attrs['type'] == 'BANK_ACCOUNT'
24
+ self.payment_instruments.resource_class = Finix::BankAccount
25
+ end
26
+
27
+ self.payment_instruments.create(attrs)
28
+ end
29
+
30
+ def create_settlement(attrs={})
31
+ attrs = attrs.attributes if attrs.is_a?(Finix::Resource)
32
+ self.settlements.create(attrs)
33
+ end
34
+
35
+ end
36
+ end
@@ -0,0 +1,8 @@
1
+ module Finix
2
+ class Merchant
3
+ include Finix::Resource
4
+ include Finix::HypermediaRegistry
5
+
6
+ define_hypermedia_types [:merchants]
7
+ end
8
+ end
@@ -0,0 +1,9 @@
1
+ module Finix
2
+ class PaymentCard < PaymentInstrument
3
+
4
+ def initialize(attributes = {}, self_href = nil)
5
+ super(attributes, self_href)
6
+ self.type = 'PAYMENT_CARD'
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,8 @@
1
+ module Finix
2
+ class PaymentInstrument
3
+ include Finix::Resource
4
+ include Finix::HypermediaRegistry
5
+
6
+ define_hypermedia_types [:payment_instruments]
7
+ end
8
+ end
@@ -0,0 +1,8 @@
1
+ module Finix
2
+ class Processor
3
+ include Finix::Resource
4
+ include Finix::HypermediaRegistry
5
+
6
+ define_hypermedia_types [:processors]
7
+ end
8
+ end
@@ -0,0 +1,8 @@
1
+ module Finix
2
+ class Refund
3
+ include Finix::Resource
4
+ include Finix::HypermediaRegistry
5
+
6
+ define_hypermedia_types [:reversals]
7
+ end
8
+ end
@@ -0,0 +1,148 @@
1
+ require File.expand_path('../../pagination', __FILE__)
2
+ require File.expand_path('../../utils', __FILE__)
3
+ require File.expand_path('../../hal_resource', __FILE__)
4
+ require 'addressable/template'
5
+
6
+ module Finix
7
+
8
+ module Resource
9
+
10
+ include HalResource
11
+
12
+ def initialize(attributes = {}, href = nil)
13
+ @attributes = Utils.indifferent_read_access attributes
14
+
15
+ @hyperlinks = {}
16
+ @hyperlinks[:self] = href if href =~ URI::regexp
17
+ end
18
+
19
+ def hydrate(links)
20
+ links.each do |key, link|
21
+ property = key.sub(/.*?\./, '')
22
+
23
+ if property == 'self'
24
+ @hyperlinks[:self] = link[:href]
25
+ # @attributes['href'] = link[:href]
26
+ else
27
+ @hyperlinks[property] = Finix::Utils.callable(Finix::Pagination.new(link[:href], {}))
28
+ # @hyperlinks[property] = Finix.from_hypermedia_registry(property).new 'href' => link[:href]
29
+ end
30
+ end
31
+ end
32
+
33
+ def fetch(*arguments)
34
+ self.class.find *arguments
35
+ end
36
+
37
+ alias find fetch
38
+
39
+ def save(options = {}, href = nil)
40
+ options = options.is_a?(Finix::Resource) ? options.attributes : options
41
+ @attributes = @attributes.merge options
42
+ href ||= @hyperlinks[:self]
43
+ # href = @attributes.delete('href')
44
+ method = :post
45
+ if href.nil?
46
+ href = Finix.hypermedia_registry.key(self.class)
47
+ elsif not @attributes[:id].nil?
48
+ method = :put
49
+ end
50
+
51
+ attributes_to_submit = self.sanitize
52
+ begin
53
+ @response = Finix.send(method, href, attributes_to_submit)
54
+ rescue Exception
55
+ raise
56
+ end
57
+
58
+ refresh @response
59
+ end
60
+
61
+ def sanitize
62
+ to_submit = {}
63
+ @attributes.each do |key, value|
64
+ if not value.is_a? Finix::Resource
65
+ to_submit[key] = value
66
+ end
67
+ end
68
+ to_submit
69
+ end
70
+
71
+ def response
72
+ @response
73
+ end
74
+
75
+ private :response
76
+
77
+ def refresh(the_response = nil)
78
+ if the_response
79
+ return if the_response.body.to_s.length.zero?
80
+ fresh = self.class.construct_from_response the_response.body
81
+ else
82
+ fresh = self.find(@hyperlinks[:self])
83
+ # fresh = self.find(@attributes[:href])
84
+ end
85
+ fresh and copy_from fresh
86
+ self
87
+ end
88
+
89
+ # alias refresh load!
90
+
91
+ def copy_from(other)
92
+ other.instance_variables.each do |ivar|
93
+ instance_variable_set ivar, other.instance_variable_get(ivar)
94
+ end
95
+ end
96
+
97
+ def self.included(base)
98
+ base.extend ClassMethods
99
+ end
100
+
101
+ module ClassMethods
102
+
103
+ def construct_from_response(payload)
104
+ payload = Finix::Utils.indifferent_read_access payload
105
+
106
+ links = payload.delete('_links') || {}
107
+ instance = self.new payload
108
+ instance.hydrate(links)
109
+ instance
110
+ end
111
+
112
+ def fetch(*arguments)
113
+ if arguments.nil? or arguments.empty? or arguments[0].nil? or arguments[0].to_s.empty? # no symbol.empty? in 1.8.7
114
+ raise Finix::NotFound
115
+ end
116
+
117
+ options = arguments.slice!(0) or {}
118
+ if options.is_a? String
119
+ href = options
120
+ else
121
+ href = Finix.hypermedia_registry.key(self) or Finix.hypermedia_registry.key(self.class)
122
+ id = options.delete(:id)
123
+ href = "#{href}/#{id}" unless id.nil?
124
+ end
125
+
126
+ response = Finix.get href
127
+ construct_from_response response.body
128
+
129
+ end
130
+
131
+ alias find fetch
132
+
133
+ # def paginate(options = {})
134
+ # Finix::Pagination.new href, options
135
+ # end
136
+
137
+ # alias scoped paginate
138
+ # alias where paginate
139
+
140
+ # def all(options = {})
141
+ # pager = paginate(options)
142
+ # pager.to_a
143
+ # end
144
+
145
+ end
146
+
147
+ end
148
+ end
@@ -0,0 +1,8 @@
1
+ module Finix
2
+ class Settlement
3
+ include Finix::Resource
4
+ include Finix::HypermediaRegistry
5
+
6
+ define_hypermedia_types [:settlements]
7
+ end
8
+ end
@@ -0,0 +1,8 @@
1
+ module Finix
2
+ class Token
3
+ include Finix::Resource
4
+ include Finix::HypermediaRegistry
5
+
6
+ define_hypermedia_types [:tokens]
7
+ end
8
+ end
@@ -0,0 +1,12 @@
1
+ module Finix
2
+ class Transfer
3
+ include Finix::Resource
4
+ include Finix::HypermediaRegistry
5
+
6
+ define_hypermedia_types [:transfers]
7
+
8
+ def reverse(refund_amount=0)
9
+ self.reversals.create :refund_amount => refund_amount
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,5 @@
1
+ module Finix
2
+ class UnknownResource
3
+ include Finix::Resource
4
+ end
5
+ end
@@ -0,0 +1,13 @@
1
+ module Finix
2
+ class User
3
+ include Finix::Resource
4
+ include Finix::HypermediaRegistry
5
+
6
+ define_hypermedia_types [:users]
7
+
8
+ def create_application(attrs = {})
9
+ attrs = attrs.attributes if attrs.is_a?(Finix::Resource)
10
+ self.applications.create(attrs)
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,12 @@
1
+ module Finix
2
+ module Verifiable
3
+ def verify_on(attrs={})
4
+ attrs = attrs.attributes if attrs.is_a?(Finix::Resource)
5
+ unless self.verifications.nil?
6
+ self.verifications.create(attrs)
7
+ else
8
+ raise 'Unsupported Method'
9
+ end
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,8 @@
1
+ module Finix
2
+ class Verification
3
+ include Finix::Resource
4
+ include Finix::HypermediaRegistry
5
+
6
+ define_hypermedia_types [:verifications]
7
+ end
8
+ end
@@ -0,0 +1,8 @@
1
+ module Finix
2
+ class Webhook
3
+ include Finix::Resource
4
+ include Finix::HypermediaRegistry
5
+
6
+ define_hypermedia_types [:webhooks]
7
+ end
8
+ end
@@ -0,0 +1,26 @@
1
+ require 'faraday'
2
+ require 'finix/error'
3
+
4
+ module Faraday
5
+
6
+ class Response::RaiseApiError < Response::Middleware
7
+
8
+ HTTP_STATUS_CODES = {
9
+ 400 => Finix::BadRequest,
10
+ 401 => Finix::Unauthorized,
11
+ 403 => Finix::Forbidden,
12
+ 404 => Finix::NotFound,
13
+ 405 => Finix::MethodNotAllowed,
14
+ 422 => Finix::UnprocessableEntity
15
+ }
16
+
17
+ def on_complete(response)
18
+ status_code = response[:status].to_i
19
+ error_class = HTTP_STATUS_CODES[status_code]
20
+ raise Finix::ResourceErrors.new(response) if error_class.nil? and status_code >= 400
21
+ raise error_class.new(response) if error_class
22
+ end
23
+
24
+ end
25
+
26
+ end
@@ -0,0 +1,108 @@
1
+ module Finix
2
+
3
+ module Utils
4
+
5
+ def callable(callable_or_not)
6
+ callable_or_not.respond_to?(:call) ? callable_or_not : lambda { callable_or_not }
7
+ end
8
+
9
+ def camelize(underscored_word)
10
+ underscored_word.to_s.gsub(/(?:^|_)(.)/) { $1.upcase }
11
+ end
12
+
13
+ def classify(table_name)
14
+ camelize singularize(table_name.to_s.sub(/.*\./, ''))
15
+ end
16
+
17
+ def demodulize(class_name_in_module)
18
+ class_name_in_module.to_s.sub(/^.*::/, '')
19
+ end
20
+
21
+ def pluralize(word)
22
+ word.to_s.sub(/([^s])$/, '\1s')
23
+ end
24
+
25
+ def singularize(word)
26
+ word.to_s.sub(/s$/, '').sub(/ie$/, 'y')
27
+ end
28
+
29
+ def underscore(camel_cased_word)
30
+ word = camel_cased_word.to_s.dup
31
+ word.gsub!(/::/, '/')
32
+ word.gsub!(/([A-Z]+)([A-Z][a-z])/, '\1_\2')
33
+ word.gsub!(/([a-z\d])([A-Z])/, '\1_\2')
34
+ word.tr! '-', '_'
35
+ word.downcase!
36
+ word
37
+ end
38
+
39
+ # def extract_href_from_object(object)
40
+ # object.respond_to?(:href) ? object.href : object
41
+ # end
42
+
43
+ def indifferent_read_access(base = {})
44
+ indifferent = Hash.new do |hash, key|
45
+ hash[key.to_s] if key.is_a? Symbol
46
+ end
47
+ base.each_pair do |key, value|
48
+ if value.is_a? Hash
49
+ value = indifferent_read_access value
50
+ elsif value.respond_to? :each
51
+ if value.respond_to? :map!
52
+ value.map! do |v|
53
+ if v.is_a? Hash
54
+ v = indifferent_read_access v
55
+ end
56
+ v
57
+ end
58
+ else
59
+ value.map do |v|
60
+ if v.is_a? Hash
61
+ v = indifferent_read_access v
62
+ end
63
+ v
64
+ end
65
+ end
66
+ end
67
+ indifferent[key.to_s] = value
68
+ end
69
+ indifferent
70
+ end
71
+
72
+ # def stringify_keys!(hash)
73
+ # hash.keys.each do |key|
74
+ # stringify_keys! hash[key] if hash[key].is_a? Hash
75
+ # hash[key.to_s] = hash.delete key if key.is_a? Symbol
76
+ # end
77
+ # end
78
+
79
+ # Validate all keys in a hash match *valid keys, raising ArgumentError on a mismatch.
80
+ # Note that keys are NOT treated indifferently, meaning if you use strings for keys but assert symbols
81
+ # as keys, this will fail.
82
+ #
83
+ # ==== Examples
84
+ # { :name => "Rob", :years => "28" }.assert_valid_keys(:name, :age) # => raises "ArgumentError: Unknown key(s): years"
85
+ # { :name => "Rob", :age => "28" }.assert_valid_keys("name", "age") # => raises "ArgumentError: Unknown key(s): name, age"
86
+ # { :name => "Rob", :age => "28" }.assert_valid_keys(:name, :age) # => passes, raises nothing
87
+ # def assert_valid_keys(hash, *valid_keys)
88
+ # unknown_keys = hash.keys - [valid_keys].flatten
89
+ # raise(ArgumentError, "Unknown key(s): #{unknown_keys.join(', ')}") unless unknown_keys.empty?
90
+ # end
91
+
92
+ # http://pablomanrubia.com/2011/03/extending-ruby-to-validate-required-keys-in-a-hash-table/
93
+ #def assert_required_keys(hash, params)
94
+ # params[:required] ||= []
95
+ # params[:optional] ||= []
96
+ # assert_valid_keys(hash, params[:required] + params[:optional])
97
+ # pending_keys = params[:required] - hash.keys
98
+ # raise(ArgumentError, "Required key(s) not present: #{pending_keys.join(', ')}") unless pending_keys.empty?
99
+ #end
100
+
101
+ # def assert_required_keys(hash, params)
102
+ # params[:required] ||= []
103
+ # pending_keys = params[:required] - hash.keys
104
+ # raise(ArgumentError, "Required key(s) not present: #{pending_keys.join(', ')}") unless pending_keys.empty?
105
+ # end
106
+ extend self
107
+ end
108
+ end
@@ -0,0 +1,3 @@
1
+ module Finix
2
+ VERSION = '0.2'
3
+ end
metadata ADDED
@@ -0,0 +1,177 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: finix
3
+ version: !ruby/object:Gem::Version
4
+ version: '0.2'
5
+ platform: ruby
6
+ authors:
7
+ - finix-payments
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2016-12-06 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.13'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.13'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '3.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '3.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: faraday
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: 0.8.6
62
+ - - "<="
63
+ - !ruby/object:Gem::Version
64
+ version: 0.9.0
65
+ type: :runtime
66
+ prerelease: false
67
+ version_requirements: !ruby/object:Gem::Requirement
68
+ requirements:
69
+ - - ">="
70
+ - !ruby/object:Gem::Version
71
+ version: 0.8.6
72
+ - - "<="
73
+ - !ruby/object:Gem::Version
74
+ version: 0.9.0
75
+ - !ruby/object:Gem::Dependency
76
+ name: faraday_middleware
77
+ requirement: !ruby/object:Gem::Requirement
78
+ requirements:
79
+ - - "~>"
80
+ - !ruby/object:Gem::Version
81
+ version: 0.9.0
82
+ type: :runtime
83
+ prerelease: false
84
+ version_requirements: !ruby/object:Gem::Requirement
85
+ requirements:
86
+ - - "~>"
87
+ - !ruby/object:Gem::Version
88
+ version: 0.9.0
89
+ - !ruby/object:Gem::Dependency
90
+ name: addressable
91
+ requirement: !ruby/object:Gem::Requirement
92
+ requirements:
93
+ - - "~>"
94
+ - !ruby/object:Gem::Version
95
+ version: 2.3.5
96
+ type: :runtime
97
+ prerelease: false
98
+ version_requirements: !ruby/object:Gem::Requirement
99
+ requirements:
100
+ - - "~>"
101
+ - !ruby/object:Gem::Version
102
+ version: 2.3.5
103
+ description: Finix Ruby Client
104
+ email:
105
+ - support@finixpayments.com
106
+ executables:
107
+ - console
108
+ - setup
109
+ extensions: []
110
+ extra_rdoc_files: []
111
+ files:
112
+ - ".gitignore"
113
+ - ".ruby-gemset"
114
+ - ".ruby-version"
115
+ - Gemfile
116
+ - LICENSE.txt
117
+ - README.md
118
+ - Rakefile
119
+ - bin/console
120
+ - bin/setup
121
+ - circle.yml
122
+ - finix.gemspec
123
+ - lib/finix.rb
124
+ - lib/finix/client.rb
125
+ - lib/finix/error.rb
126
+ - lib/finix/hal_resource.rb
127
+ - lib/finix/pagination.rb
128
+ - lib/finix/resources.rb
129
+ - lib/finix/resources/application.rb
130
+ - lib/finix/resources/authorization.rb
131
+ - lib/finix/resources/bank_account.rb
132
+ - lib/finix/resources/dispute.rb
133
+ - lib/finix/resources/evidence.rb
134
+ - lib/finix/resources/hypermedia.rb
135
+ - lib/finix/resources/identity.rb
136
+ - lib/finix/resources/merchant.rb
137
+ - lib/finix/resources/payment_card.rb
138
+ - lib/finix/resources/payment_instrument.rb
139
+ - lib/finix/resources/processor.rb
140
+ - lib/finix/resources/refund.rb
141
+ - lib/finix/resources/resource.rb
142
+ - lib/finix/resources/settlement.rb
143
+ - lib/finix/resources/token.rb
144
+ - lib/finix/resources/transfer.rb
145
+ - lib/finix/resources/unknown_resource.rb
146
+ - lib/finix/resources/user.rb
147
+ - lib/finix/resources/verifiable.rb
148
+ - lib/finix/resources/verification.rb
149
+ - lib/finix/resources/webhook.rb
150
+ - lib/finix/response/finix_error_middleware.rb
151
+ - lib/finix/utils.rb
152
+ - lib/finix/version.rb
153
+ homepage: https://www.finixpayments.com
154
+ licenses:
155
+ - MIT
156
+ metadata: {}
157
+ post_install_message:
158
+ rdoc_options: []
159
+ require_paths:
160
+ - lib
161
+ required_ruby_version: !ruby/object:Gem::Requirement
162
+ requirements:
163
+ - - ">="
164
+ - !ruby/object:Gem::Version
165
+ version: '0'
166
+ required_rubygems_version: !ruby/object:Gem::Requirement
167
+ requirements:
168
+ - - ">="
169
+ - !ruby/object:Gem::Version
170
+ version: '0'
171
+ requirements: []
172
+ rubyforge_project:
173
+ rubygems_version: 2.4.8
174
+ signing_key:
175
+ specification_version: 4
176
+ summary: Finix Ruby Client
177
+ test_files: []