mambu 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.
data/.travis.yml ADDED
@@ -0,0 +1,4 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.2.2
4
+ before_install: gem install bundler -v 1.10.3
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in mambu.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2015 netguru.co
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,96 @@
1
+ # Mambu
2
+
3
+ Wrapper for Mambu Cloud Banking Platform REST API
4
+
5
+ https://developer.mambu.com/
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'mambu'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install mambu
22
+
23
+ ## Usage
24
+ # create api connection
25
+ connection = Mambu::Connection.new('username', 'password', 'tenant.sandbox')
26
+ ### API models
27
+ ##### #find_all
28
+ Mambu::LoanProduct.find_all(connection)
29
+ # or
30
+ connection.loan_product.find_all
31
+ ##### #find
32
+ Mambu::LoanProduct.find('product_id', connection)
33
+ # or
34
+ connection.loan_product.find('product_id')
35
+
36
+ #### Implemented models:
37
+ Mambu::LoanProduct https://developer.mambu.com/customer/portal/articles/1616164-loan-products-api
38
+
39
+
40
+ ## Development
41
+
42
+ #### Gem development
43
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake rspec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
44
+
45
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
46
+
47
+ #### Mambu::ApiModel
48
+ Adding new api models is extremely easy. Model fields (accessors) are set dynamically based on json content. To add User model you have to:
49
+
50
+ # create user class (lib/mambu/user.rb) that inherits from Mambu::ApiModel
51
+
52
+ module Mambu
53
+ class User < ApiModel
54
+ end
55
+ end
56
+
57
+ # require this class in lib/mambu.rb
58
+ require 'mambu/user'
59
+
60
+ Api endpoint is created from connection and model class name. For Mambu::User is is users. You can override this by setting class method api_uri
61
+
62
+ module Mambu
63
+ class User < ApiModel
64
+ def self.api_uri
65
+ 'endpoint'
66
+ end
67
+ end
68
+ end
69
+
70
+ You can also override accessor if you want to change how json data is converted into api model. For example:
71
+
72
+ module Mambu
73
+ class LoanProduct < ApiModel
74
+ attr_accessor :loan_fees
75
+
76
+ def loan_fees=(data)
77
+ @loan_fees = data.map { |hash_fee| Mambu::LoanFee.new(hash_fee) }
78
+ end
79
+ end
80
+ end
81
+
82
+ #### Mambu::Response
83
+ Every response is wrapped in Mambu::Response. This class converts json body to ruby hash with symbolized, snake_cased keys. It also reads mambu #error_code and #error_status from failed response body.
84
+
85
+ #### Mambu::Error
86
+ Mambu error has message, code and status. It can be generated from failed Mambu::Response by calling #error method. Message is humanized mambu error status.
87
+
88
+
89
+ ## Contributing
90
+
91
+ Bug reports and pull requests are welcome on GitHub at https://github.com/netguru/mambu-api-ruby.
92
+
93
+
94
+ ## License
95
+
96
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -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
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "mambu"
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,7 @@
1
+ #!/bin/bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+
5
+ bundle install
6
+
7
+ # Do any other automated setup that you need to do here
data/circle.yml ADDED
@@ -0,0 +1,8 @@
1
+ dependencies:
2
+ pre:
3
+ - gem install bundler --pre
4
+ deployment:
5
+ master:
6
+ branch: master
7
+ commands:
8
+ - curl -L https://raw.githubusercontent.com/netguru/review/master/check.rb | ruby
data/lib/mambu.rb ADDED
@@ -0,0 +1,22 @@
1
+ require "active_support/all"
2
+ require "faraday"
3
+ require "json"
4
+ require "mambu/version"
5
+ require "mambu/finders"
6
+ require "mambu/error"
7
+ require "mambu/response"
8
+ require "mambu/connection"
9
+ require "mambu/api_model"
10
+ require "mambu/loan_product"
11
+ require "mambu/loan_fee"
12
+ require "mambu/loan_schedule"
13
+ require "mambu/repayment"
14
+ begin
15
+ require "pry"
16
+ rescue LoadError
17
+ end
18
+
19
+
20
+ module Mambu
21
+ # Your code goes here...
22
+ end
@@ -0,0 +1,18 @@
1
+ module Mambu
2
+ class ApiModel
3
+ def initialize(data)
4
+ data.each do |method_name, value|
5
+ self.class.send(:attr_accessor, method_name) unless self.class.method_defined?(method_name)
6
+ send("#{method_name}=", value)
7
+ end
8
+ end
9
+
10
+ def self.endpoint(connection)
11
+ "#{connection.api_url}/#{api_uri}"
12
+ end
13
+
14
+ def self.api_uri
15
+ name.demodulize.downcase.pluralize
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,44 @@
1
+ module Mambu
2
+ class Connection
3
+ def initialize(username, password, tenant)
4
+ @username = username
5
+ @password = password
6
+ @tenant = tenant
7
+ fail Mambu::Error, 'Insufficient credentials' unless valid?
8
+ end
9
+
10
+ def get(url, options = {})
11
+ request(:get, url, options)
12
+ end
13
+
14
+ def post(url, options = {})
15
+ request(:post, url, options)
16
+ end
17
+
18
+ def request(method, url, options)
19
+ Mambu::Response.new(connection.send(method, url, options))
20
+ end
21
+
22
+ def connection
23
+ conn = Faraday.new
24
+ conn.basic_auth(@username, @password)
25
+ conn
26
+ end
27
+
28
+ def api_url
29
+ "https://#{@tenant}.mambu.com/api"
30
+ end
31
+
32
+ def valid?
33
+ @username.present? && @password.present? && @tenant.present?
34
+ end
35
+
36
+ def loan_product(id)
37
+ Mambu::LoanProduct.find(id, self)
38
+ end
39
+
40
+ def loan_products
41
+ Mambu::LoanProduct.find_all(self)
42
+ end
43
+ end
44
+ end
@@ -0,0 +1,11 @@
1
+ module Mambu
2
+ class Error < StandardError
3
+ attr_reader :code, :status
4
+
5
+ def initialize(message, code = nil, status = nil)
6
+ super(message)
7
+ @code = code
8
+ @status = status
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,15 @@
1
+ module Mambu
2
+ module Finders
3
+ def find(id, connection)
4
+ response = connection.get("#{endpoint(connection)}/#{id}")
5
+ fail response.error unless response.success?
6
+ new(response.body)
7
+ end
8
+
9
+ def find_all(connection)
10
+ response = connection.get(endpoint(connection))
11
+ fail response.error unless response.success?
12
+ response.body.map { |data| new(data) }
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,4 @@
1
+ module Mambu
2
+ class LoanFee < ApiModel
3
+ end
4
+ end
@@ -0,0 +1,10 @@
1
+ module Mambu
2
+ class LoanProduct < ApiModel
3
+ extend Mambu::Finders
4
+ attr_accessor :loan_fees
5
+
6
+ def loan_fees=(data)
7
+ @loan_fees = data.map { |hash_fee| Mambu::LoanFee.new(hash_fee) }
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,37 @@
1
+ module Mambu
2
+ class LoanSchedule < ApiModel
3
+ attr_accessor :repayments
4
+
5
+ def repayments=(data)
6
+ @repayments = data.map { |hash_fee| Mambu::Repayment.new(hash_fee) }
7
+ end
8
+
9
+ def self.find(loan_product, options, connection)
10
+ response = connection.get(
11
+ "#{endpoint(connection)}/#{loan_product.id}/schedule",
12
+ camelize_hash(options)
13
+ )
14
+ handle_error(response)
15
+ new(response.body)
16
+ end
17
+
18
+ def self.api_uri
19
+ Mambu::LoanProduct.api_uri
20
+ end
21
+
22
+ def self.handle_error(response)
23
+ return if response.success?
24
+ error = response.error
25
+ if error.status == 'INTERNAL_SERVER_ERROR'
26
+ error = Mambu::Error.new(
27
+ 'Known mambu issue. Please grant administrator permissions to api user.'
28
+ )
29
+ end
30
+ fail error
31
+ end
32
+
33
+ def self.camelize_hash(options)
34
+ Hash[options.map { |k, v| [k.to_s.camelize(:lower).to_sym, v] }]
35
+ end
36
+ end
37
+ end
@@ -0,0 +1,4 @@
1
+ module Mambu
2
+ class Repayment < ApiModel
3
+ end
4
+ end
@@ -0,0 +1,52 @@
1
+ module Mambu
2
+ class Response
3
+ attr_reader :http_status, :body, :faraday_response, :error_code, :error_status
4
+
5
+ def initialize(faraday_response)
6
+ @faraday_response = faraday_response
7
+ @http_status = faraday_response.status
8
+ @body = parse_body(faraday_response.body)
9
+ return self if success?
10
+ @error_code = @body[:return_code]
11
+ @error_status = @body[:return_status]
12
+ end
13
+
14
+ def success?
15
+ @faraday_response.success?
16
+ end
17
+
18
+ def error?
19
+ !success?
20
+ end
21
+
22
+ def error_message
23
+ error_status.humanize
24
+ end
25
+
26
+ def error
27
+ return nil if success?
28
+ Mambu::Error.new(error_message, error_code, error_status)
29
+ end
30
+
31
+ private
32
+
33
+ def parse_body(json_body)
34
+ begin
35
+ convert_hash_keys(JSON.parse(json_body))
36
+ rescue JSON::ParserError
37
+ { return_code: -1, return_status: 'INTERNAL_SERVER_ERROR' }
38
+ end
39
+ end
40
+
41
+ def convert_hash_keys(value)
42
+ case value
43
+ when Array
44
+ value.map(&method(:convert_hash_keys))
45
+ when Hash
46
+ Hash[value.map { |k, v| [k.to_s.underscore.to_sym, convert_hash_keys(v)] }]
47
+ else
48
+ value
49
+ end
50
+ end
51
+ end
52
+ end
@@ -0,0 +1,3 @@
1
+ module Mambu
2
+ VERSION = "0.1.0"
3
+ end
data/mambu.gemspec ADDED
@@ -0,0 +1,32 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'mambu/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "mambu"
8
+ spec.version = Mambu::VERSION
9
+ spec.authors = ["Netguru"]
10
+ spec.email = ["netguru@netguru.co"]
11
+
12
+ spec.summary = "mambu.com REST API wrapper"
13
+ spec.description = "mambu.com REST API wrapper"
14
+ spec.homepage = "http://github.com/netguru/mambu-api-ruby"
15
+ spec.license = "MIT"
16
+
17
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
18
+ spec.bindir = "exe"
19
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
20
+ spec.require_paths = ["lib"]
21
+
22
+ spec.add_development_dependency "bundler", "~> 1.10"
23
+ spec.add_development_dependency "rake", "~> 10.0"
24
+ spec.add_development_dependency "rspec"
25
+ spec.add_development_dependency "vcr"
26
+ spec.add_development_dependency "webmock"
27
+ spec.add_development_dependency "awesome_print"
28
+ spec.add_development_dependency "pry"
29
+
30
+ spec.add_dependency "activesupport"
31
+ spec.add_dependency "faraday"
32
+ end