ofx-rb 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 5fbec719c8f878143842e7ce5a3d5b49901458be06deab41adaa832799b13e90
4
+ data.tar.gz: 790aaddf260e8c6a306f3496fdaeb6d81cca8bc4fbba684235c1543ec653dff5
5
+ SHA512:
6
+ metadata.gz: 16133b8b23709e0734d46c9a685e9ecb2dfd21dd8feaf1d8373218a411b01cdf5729936a8d7b4cd4d632b8834c0cf96b07df151dcc5f8dd0a12083854e979251
7
+ data.tar.gz: 821b68bae5262d4b1922b22ff9e7e3ed2927e36092f36ceed876485881385c4d83812b43733c96b48888ba05f5dd5051cc358e834f85c43a31b1803a3e345973
@@ -0,0 +1,11 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /.byebug_history/
9
+ /tmp/
10
+ # rspec failure tracking
11
+ .rspec_status
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "https://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in ofx.gemspec
4
+ gemspec
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2019 Milaap Social Ventures
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.
@@ -0,0 +1,60 @@
1
+ # OFX
2
+
3
+ Welcome to OFX ruby gem! The OFX Ruby gem provides a small SDK for convenient access to the [OFX APIs][ofx/api] from applications written in the Ruby language. It provides a pre-defined set of classes for API resources that initialize themselves dynamically from API responses which allows the bindings to tolerate a number of different versions of the API.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'ofx-rb'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle install
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install ofx-rb
20
+
21
+ ## Usage
22
+
23
+ The library needs to be configured with environment mode "test" or "live"
24
+ ```ruby
25
+ For live mode
26
+ Ofx.mode = "live"
27
+
28
+ For test mode
29
+ Ofx.mode = "test"
30
+ ```
31
+ # Development
32
+ ## Authentication
33
+ OFX resource APIs require access token to be sent as Authorization header.
34
+ You can get the access token as following:
35
+
36
+ ```ruby
37
+ Ofx::Authentication.new("your client_id", "your client_secret").get_access_token
38
+ ```
39
+ ## Quotes API
40
+ https://payments.developer.ofx.com/specs/quotes/create-quote
41
+ Create a quote
42
+
43
+ ```ruby
44
+ quote_params = {
45
+ "buyCurrency": "USD",
46
+ "buyAmount": 0,
47
+ "sellCurrency": "INR",
48
+ "sellAmount": 50,
49
+ "beneficiaries": 1
50
+ }
51
+ Ofx::Quote.create(quote_params, {"access_token" => "api access token"})
52
+ ```
53
+ Get a already created quote
54
+ https://payments.developer.ofx.com/specs/quotes/get-quote
55
+
56
+ ```ruby
57
+ Ofx::Quote.get("<existing quote id>", {"access_token" => "api access token"})
58
+ ```
59
+
60
+ [ofx/api]: https://payments.developer.ofx.com/
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "ofx"
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(__FILE__)
@@ -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
@@ -0,0 +1,10 @@
1
+ class String
2
+ def to_underscore!
3
+ gsub!(/(.)([A-Z])/,'\1_\2')
4
+ downcase!
5
+ end
6
+
7
+ def to_underscore
8
+ dup.tap { |s| s.to_underscore! }
9
+ end
10
+ end
@@ -0,0 +1,24 @@
1
+ require "ofx/version"
2
+
3
+ require "rest-client"
4
+ require "json"
5
+
6
+ require "extentions/string"
7
+ require "ofx/ofx_object"
8
+ require "ofx/authentication"
9
+ require "ofx/client"
10
+ require "ofx/api_resource"
11
+ require "ofx/beneficiary"
12
+ require "ofx/quote"
13
+ require "ofx/deal"
14
+ require "ofx/ofx_error"
15
+
16
+ module Ofx
17
+ class << self
18
+ attr_accessor :mode
19
+ attr_accessor :access_token
20
+ def api_base
21
+ @api_base ||= mode == 'live' ? 'https://live.api.ofx.com' : 'https://sandbox.api.ofx.com/v1'
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,35 @@
1
+ module Ofx
2
+ class APIResource
3
+ include Ofx::OfxObject
4
+
5
+ def self.class_name
6
+ self.name.split('::')[-1]
7
+ end
8
+
9
+ def self.resource_url(resource_id)
10
+ "#{collection_url}/#{resource_id}"
11
+ end
12
+
13
+ def self.collection_url(resource_id = nil)
14
+ if self == APIResource
15
+ raise NotImplementedError.new('APIResource is an abstract class. You should perform actions on its subclasses (Account, Transfer, etc.)')
16
+ end
17
+ "/payments/#{CGI.escape(class_name.downcase)}s"
18
+ end
19
+
20
+ def self.create(params = {}, opts = {})
21
+ response = Ofx::Client.request(:post, collection_url, params, opts)
22
+ convert_to_ofx_object(response)
23
+ end
24
+
25
+ def self.list(headers = {}, filters = {}, resource_id: nil)
26
+ response = Ofx::Client.request(:get, collection_url(resource_id), filters, headers)
27
+ convert_to_ofx_object(response)
28
+ end
29
+
30
+ def self.get(resource_id, headers = {})
31
+ response = Ofx::Client.request(:get, resource_url(resource_id), {}, headers)
32
+ convert_to_ofx_object(response)
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,32 @@
1
+ module Ofx
2
+ class Authentication
3
+ attr_accessor :access_token
4
+
5
+ def initialize(client_id, client_secret)
6
+ @client_id, @client_secret = client_id, client_secret
7
+ end
8
+
9
+ def get_access_token
10
+ @access_token ||= fetch_auth_token["access_token"]
11
+ end
12
+
13
+ private
14
+
15
+ def auth_request
16
+ {
17
+ url: "#{Ofx.api_base}/oauth/token",
18
+ method: "post",
19
+ payload: "grant_type=client_credentials&client_id=#{@client_id}&client_secret=#{@client_secret}&scope=payments",
20
+ headers:{
21
+ 'Content-Type' => 'application/x-www-form-urlencoded'
22
+ }
23
+ }
24
+ end
25
+
26
+ def fetch_auth_token
27
+ response = RestClient::Request.execute(auth_request)
28
+ JSON.parse(response)
29
+ end
30
+
31
+ end
32
+ end
@@ -0,0 +1,7 @@
1
+ module Ofx
2
+ class Beneficiary < APIResource
3
+ def self.collection_url(resource_id = nil)
4
+ "/payments/beneficiaries"
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,107 @@
1
+ module Ofx
2
+ class Client
3
+ def self.api_url(url = '')
4
+ Ofx.api_base + url
5
+ end
6
+
7
+ def self.request(method, url, params={}, headers={})
8
+ url = api_url(url)
9
+ access_token = headers.delete("access_token") || Ofx.access_token
10
+ request_opts = {
11
+ url: url,
12
+ method: method,
13
+ payload: params.to_json,
14
+ headers: request_headers(access_token).update(headers)
15
+ }
16
+ response = execute_request(request_opts)
17
+ parse(response)
18
+ end
19
+
20
+ private
21
+
22
+ def self.request_headers(access_token)
23
+ {
24
+ 'Authorization' => "Bearer #{access_token}",
25
+ 'Content-Type' => 'application/json'
26
+ }
27
+ end
28
+
29
+ def self.execute_request(request_opts)
30
+ begin
31
+ response = RestClient::Request.execute(request_opts)
32
+ rescue => e
33
+ if e.is_a?(RestClient::Exception)
34
+ response = handle_error(e, request_opts)
35
+ else
36
+ raise
37
+ end
38
+ end
39
+ response
40
+ end
41
+
42
+
43
+ def self.handle_error(e, request_opts)
44
+ if e.is_a?(RestClient::ExceptionWithResponse) && e.response
45
+ handle_api_error(e.response)
46
+ else
47
+ handle_restclient_error(e, request_opts)
48
+ end
49
+ end
50
+
51
+ def self.handle_api_error(resp)
52
+ error_obj = parse(resp) rescue {}
53
+ unless error_obj["errors"].nil?
54
+ error_message = error_obj["errors"].map{|e| e["message"]}.join(', ')
55
+ end
56
+
57
+ if Ofx::STATUS_CLASS_MAPPING.include?(resp.code)
58
+ raise "Ofx::#{Ofx::STATUS_CLASS_MAPPING[resp.code]}".constantize.new(error_params(error_message, resp, error_obj))
59
+ else
60
+ raise Ofx::OfxError.new(error_params(error_message, resp, error_obj))
61
+ end
62
+ end
63
+
64
+ def self.parse(response)
65
+ begin
66
+ response = JSON.parse(response.body)
67
+ rescue JSON::ParserError
68
+ raise handle_parse_error(response.code, response.body)
69
+ end
70
+ response
71
+ end
72
+
73
+ def self.handle_parse_error(rcode, rbody)
74
+ Ofx::ParseError.new({
75
+ message: "Unable to parse API response: #{rbody.inspect} (HTTP response code was #{rcode})",
76
+ http_status: rcode,
77
+ http_body: rbody
78
+ })
79
+ end
80
+
81
+ def self.handle_restclient_error(e, request_opts)
82
+ connection_message = "Please check your internet connection and try again. "
83
+
84
+ case e
85
+ when RestClient::RequestTimeout
86
+ message = "Could not connect to Ofx (#{request_opts[:url]}). #{connection_message}"
87
+ when RestClient::ServerBrokeConnection
88
+ message = "The connection to the server (#{request_opts[:url]}) broke before the " \
89
+ "request completed. #{connection_message}"
90
+ else
91
+ message = "Unexpected error communicating with Ofx. "
92
+ end
93
+
94
+ raise Ofx::APIConnectionError.new({message: "#{message} \n\n (Error: #{e.message})"})
95
+ end
96
+
97
+ def self.error_params(error, resp, error_obj)
98
+ {
99
+ message: error,
100
+ http_status: resp.code,
101
+ http_body: resp.body,
102
+ json_body: error_obj,
103
+ http_headers: resp.headers
104
+ }
105
+ end
106
+ end
107
+ end
@@ -0,0 +1,4 @@
1
+ module Ofx
2
+ class Deal < APIResource
3
+ end
4
+ end
@@ -0,0 +1,39 @@
1
+ module Ofx
2
+
3
+ STATUS_CLASS_MAPPING = {
4
+ 400 => "InvalidRequestError",
5
+ 404 => "InvalidRequestError",
6
+ 401 => "AuthenticationError"
7
+ }
8
+
9
+ class OfxError < StandardError
10
+ attr_reader :message, :http_status, :http_body, :http_headers, :json_body
11
+
12
+ def initialize(params = {})
13
+ params.each do |key, value|
14
+ instance_variable_set("@#{key}", value)
15
+ end
16
+ end
17
+
18
+ def to_s
19
+ status_string = @http_status.nil? ? "" : "(Status #{@http_status}) "
20
+ "#{status_string}#{@message}"
21
+ end
22
+ end
23
+
24
+ class APIConnectionError < OfxError
25
+ end
26
+
27
+ class APIError < OfxError
28
+ end
29
+
30
+ class AuthenticationError < OfxError
31
+ end
32
+
33
+ class InvalidRequestError < OfxError
34
+ end
35
+
36
+ class ParseError < OfxError
37
+ end
38
+
39
+ end
@@ -0,0 +1,46 @@
1
+ module Ofx
2
+ module OfxObject
3
+
4
+ def self.included(base)
5
+ base.extend ClassMethods
6
+ end
7
+
8
+ def initialize()
9
+ @values = {}
10
+ end
11
+
12
+ def initialize_from(values)
13
+ add_methods(values.keys)
14
+ update_attributes(values)
15
+ self
16
+ end
17
+
18
+ def add_methods(keys)
19
+ self.instance_eval do
20
+ keys.each do |k|
21
+ self.class.send(:define_method, k.to_underscore) { @values[k] }
22
+ end
23
+ end
24
+ end
25
+
26
+ def update_attributes(values)
27
+ values.each do |k, v|
28
+ @values[k] = v
29
+ end
30
+ end
31
+
32
+ module ClassMethods
33
+ def convert_to_ofx_object(resp)
34
+ case resp
35
+ when Array
36
+ resp.map { |i| convert_to_ofx_object(i) }
37
+ when Hash
38
+ self.new.initialize_from(resp)
39
+ else
40
+ resp
41
+ end
42
+ end
43
+ end
44
+
45
+ end
46
+ end
@@ -0,0 +1,4 @@
1
+ module Ofx
2
+ class Quote < APIResource
3
+ end
4
+ end
@@ -0,0 +1,3 @@
1
+ module Ofx
2
+ VERSION = "0.1.0"
3
+ end
Binary file
@@ -0,0 +1,33 @@
1
+ lib = File.expand_path("lib", __dir__)
2
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
3
+ require "ofx/version"
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = "ofx-rb"
7
+ spec.version = Ofx::VERSION
8
+ spec.authors = ["Imran Ahmad"]
9
+ spec.email = ["imran@milaap.org"]
10
+
11
+ spec.summary = %q{Ruby gem for OFX APIs}
12
+ spec.description = %q{Ruby gem for OFX APIs}
13
+ spec.homepage = "https://github.com/Milaap/ofx-rb"
14
+
15
+ spec.metadata["allowed_push_host"] = "https://rubygems.org"
16
+
17
+ spec.metadata["homepage_uri"] = spec.homepage
18
+
19
+ # Specify which files should be added to the gem when it is released.
20
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
21
+ spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
22
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
23
+ end
24
+ spec.bindir = "exe"
25
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
26
+ spec.require_paths = ["lib"]
27
+
28
+ spec.add_runtime_dependency("rest-client", "~> 2.1")
29
+
30
+ spec.add_development_dependency "bundler", "~> 2.0"
31
+ spec.add_development_dependency "rake", "~> 10.0"
32
+ spec.add_development_dependency "rspec", "~> 3.0"
33
+ end
metadata ADDED
@@ -0,0 +1,121 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: ofx-rb
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Imran Ahmad
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2019-12-03 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rest-client
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '2.1'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '2.1'
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '2.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '2.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '10.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '10.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rspec
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '3.0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '3.0'
69
+ description: Ruby gem for OFX APIs
70
+ email:
71
+ - imran@milaap.org
72
+ executables: []
73
+ extensions: []
74
+ extra_rdoc_files: []
75
+ files:
76
+ - ".gitignore"
77
+ - Gemfile
78
+ - LICENSE.txt
79
+ - README.md
80
+ - Rakefile
81
+ - bin/console
82
+ - bin/setup
83
+ - lib/extentions/string.rb
84
+ - lib/ofx.rb
85
+ - lib/ofx/api_resource.rb
86
+ - lib/ofx/authentication.rb
87
+ - lib/ofx/beneficiary.rb
88
+ - lib/ofx/client.rb
89
+ - lib/ofx/deal.rb
90
+ - lib/ofx/ofx_error.rb
91
+ - lib/ofx/ofx_object.rb
92
+ - lib/ofx/quote.rb
93
+ - lib/ofx/version.rb
94
+ - ofx-0.1.0.gem
95
+ - ofx.gemspec
96
+ homepage: https://github.com/Milaap/ofx-rb
97
+ licenses: []
98
+ metadata:
99
+ allowed_push_host: https://rubygems.org
100
+ homepage_uri: https://github.com/Milaap/ofx-rb
101
+ post_install_message:
102
+ rdoc_options: []
103
+ require_paths:
104
+ - lib
105
+ required_ruby_version: !ruby/object:Gem::Requirement
106
+ requirements:
107
+ - - ">="
108
+ - !ruby/object:Gem::Version
109
+ version: '0'
110
+ required_rubygems_version: !ruby/object:Gem::Requirement
111
+ requirements:
112
+ - - ">="
113
+ - !ruby/object:Gem::Version
114
+ version: '0'
115
+ requirements: []
116
+ rubyforge_project:
117
+ rubygems_version: 2.7.8
118
+ signing_key:
119
+ specification_version: 4
120
+ summary: Ruby gem for OFX APIs
121
+ test_files: []