bloom_remit2 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,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 "bloom_remit2"
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,39 @@
1
+ require_relative 'lib/bloom_remit2/version'
2
+
3
+ Gem::Specification.new do |spec|
4
+ spec.name = "bloom_remit2"
5
+ spec.version = BloomRemit2::VERSION
6
+ spec.authors = ["Xavi Ablaza"]
7
+ spec.email = ["xlablaza@gmail.com"]
8
+
9
+ spec.summary = %q{Ruby wrapper for BloomRemit's API}
10
+ spec.homepage = "https://github.com/makisu/bloom_remit2"
11
+ spec.license = "MIT"
12
+ spec.required_ruby_version = Gem::Requirement.new(">= 2.3.0")
13
+
14
+ spec.metadata["homepage_uri"] = spec.homepage
15
+ spec.metadata["source_code_uri"] = "https://github.com/makisu/bloom_remit2"
16
+ spec.metadata["changelog_uri"] = "https://github.com/makisu/bloom_remit2/blob/master/CHANGELOG.md"
17
+
18
+ # Specify which files should be added to the gem when it is released.
19
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
20
+ spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
21
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
22
+ end
23
+ spec.bindir = "exe"
24
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
25
+ spec.require_paths = ["lib"]
26
+
27
+ spec.add_dependency 'activesupport'
28
+ spec.add_dependency 'gem_config'
29
+ spec.add_dependency 'httparty'
30
+
31
+ spec.add_development_dependency "bundler", "~> 2.0"
32
+ spec.add_development_dependency "rake", "~> 13.0"
33
+ spec.add_development_dependency "rspec", "~> 3.0"
34
+ spec.add_development_dependency "rspec_junit_formatter"
35
+ spec.add_development_dependency "pry-byebug"
36
+ spec.add_development_dependency "vcr"
37
+ spec.add_development_dependency "webmock"
38
+ spec.add_development_dependency "prettier"
39
+ end
@@ -0,0 +1,19 @@
1
+ require 'gem_config'
2
+ require 'bloom_remit2/version'
3
+ require 'bloom_remit2/client'
4
+ require 'bloom_remit2/partner'
5
+ require 'bloom_remit2/agent'
6
+ require 'bloom_remit2/credit'
7
+ require 'bloom_remit2/rate'
8
+ require 'bloom_remit2/sender'
9
+ require 'bloom_remit2/recipient'
10
+ require 'bloom_remit2/remittance'
11
+
12
+ module BloomRemit2
13
+ include GemConfig::Base
14
+
15
+ with_configuration do
16
+ has :api_token, classes: String
17
+ has :api_secret_key, classes: String
18
+ end
19
+ end
@@ -0,0 +1,84 @@
1
+ module BloomRemit2
2
+ class Agent
3
+ class << self
4
+ # Show a list of agents belonging to this partner
5
+ def list
6
+ agents = Client.get(path)
7
+ agents.map do |agent|
8
+ agent = agent.with_indifferent_access
9
+ new(
10
+ agent[:id],
11
+ agent[:name],
12
+ agent[:deleted_at]
13
+ )
14
+ end
15
+ end
16
+
17
+ # Show an agent belonging to this partner
18
+ # @param id [String] agent uuid to retrieve details for
19
+ def retrieve(id)
20
+ agent = Client.get("#{path}/#{id}").with_indifferent_access
21
+ new(
22
+ agent[:id],
23
+ agent[:name]
24
+ )
25
+ end
26
+
27
+ # Create a new agent under this partner
28
+ # @param name [String] agent name
29
+ def create(name)
30
+ agent = Client.post(path, { agent: { name: name }}).with_indifferent_access
31
+ new(
32
+ agent[:id],
33
+ agent[:name]
34
+ )
35
+ end
36
+
37
+ # TODO: Doesn't work at the moment
38
+ # Update the attributes of an agent belonging to this partner
39
+ # @param agent_id [String] agent uuid to update details for
40
+ # @param name [String]
41
+ # def update(agent_id, name:)
42
+ # result = Client.put(path, { agent: { name: name }})
43
+ # agent = JSON.parse(result).with_indifferent_access
44
+ # new(
45
+ # agent[:id],
46
+ # agent[:name]
47
+ # )
48
+ # end
49
+
50
+ # Delete an agent belonging to this partner
51
+ # @param agent_id [String] agent uuid to delete
52
+ def delete(agent_id)
53
+ message = Client.delete("#{path}/#{agent_id}").with_indifferent_access
54
+ if message[:success] == "We've successfully deleted that agent."
55
+ new(
56
+ agent_id,
57
+ nil,
58
+ deleted: true
59
+ )
60
+ end
61
+ end
62
+
63
+ private
64
+
65
+ def path
66
+ "api/v1/partners/#{BloomRemit2.configuration.api_token}/agents"
67
+ end
68
+ end
69
+
70
+ attr_reader :id, :name, :deleted_at, :deleted
71
+
72
+ def initialize(
73
+ id,
74
+ name,
75
+ deleted_at=nil,
76
+ deleted: false
77
+ )
78
+ @id = id
79
+ @name = name
80
+ @deleted_at = deleted_at
81
+ @deleted = deleted
82
+ end
83
+ end
84
+ end
@@ -0,0 +1,59 @@
1
+ require 'httparty'
2
+
3
+ module BloomRemit2
4
+ class Client
5
+ class << self
6
+ def get(path, headers = default_get_headers, query: {})
7
+ HTTParty.get(url(path), { query: query, headers: headers })
8
+ end
9
+
10
+ def post(path, body = {}, headers = default_post_headers)
11
+ HTTParty.post(url(path), { body: body.to_json, headers: headers })
12
+ end
13
+
14
+ def put(path, body = [], headers = default_put_headers)
15
+ HTTParty.put(url(path), { body: URI.encode_www_form(body), headers: headers })
16
+ end
17
+
18
+ def delete(path, headers = default_get_headers)
19
+ HTTParty.delete(url(path), { headers: headers })
20
+ end
21
+
22
+ private
23
+
24
+ def base_url
25
+ 'https://www.bloomremit.net'
26
+ end
27
+
28
+ def url(path)
29
+ "#{base_url}/#{path}"
30
+ end
31
+
32
+ def api_secret_key
33
+ BloomRemit2.configuration.api_secret_key
34
+ end
35
+
36
+ def default_get_headers
37
+ {
38
+ 'x-api-secret' => api_secret_key,
39
+ 'cache-control' => 'no-cache'
40
+ }
41
+ end
42
+
43
+ def default_put_headers
44
+ {
45
+ 'x-api-secret' => api_secret_key,
46
+ 'content-type' => 'application/x-www-form-urlencoded'
47
+ }
48
+ end
49
+
50
+ def default_post_headers
51
+ default_get_headers.merge!(
52
+ {
53
+ 'content-type' => 'application/json'
54
+ }
55
+ )
56
+ end
57
+ end
58
+ end
59
+ end
@@ -0,0 +1,69 @@
1
+ module BloomRemit2
2
+ class Credit
3
+ class << self
4
+ # Show a list of credit transactions
5
+ #
6
+ # Grabs a list of the most recent 100 credit transactions associated with this vendor account.
7
+ # Transactions that add to your credit will have the boolean 'consumed' set to 'false',
8
+ # and transactions that use up your credit (i.e., to pay for remittances) will have it set to 'true'.
9
+ def history
10
+ credits = Client.get("#{path}/history")
11
+ credits.map do |credit|
12
+ new(
13
+ credit['amount_in_php'],
14
+ credit['balance'],
15
+ credit['consumed'],
16
+ credit['created_at'],
17
+ credit['id'],
18
+ credit['item_id'],
19
+ credit['item_type'],
20
+ credit['rate'],
21
+ credit['source_amount'],
22
+ credit['source_currency'],
23
+ credit['target_amount'],
24
+ credit['target_currency'],
25
+ credit['updated_at']
26
+ )
27
+ end
28
+ end
29
+
30
+ private
31
+
32
+ def path
33
+ "api/v1/partners/#{BloomRemit2.configuration.api_token}/credits"
34
+ end
35
+ end
36
+
37
+ attr_reader :amount_in_php, :balance, :consumed, :created_at, :id, :item_id, :item_type, :rate, :source_amount, :source_currency, :target_amount, :target_currency, :updated_at
38
+
39
+ def initialize(
40
+ amount_in_php,
41
+ balance,
42
+ consumed,
43
+ created_at,
44
+ id,
45
+ item_id,
46
+ item_type,
47
+ rate,
48
+ source_amount,
49
+ source_currency,
50
+ target_amount,
51
+ target_currency,
52
+ updated_at
53
+ )
54
+ @amount_in_php = amount_in_php
55
+ @balance = balance
56
+ @consumed = consumed
57
+ @created_at = created_at
58
+ @id = id
59
+ @item_id = item_id
60
+ @item_type = item_type
61
+ @rate = rate
62
+ @source_amount = source_amount
63
+ @source_currency = source_currency
64
+ @target_amount = target_amount
65
+ @target_currency = target_currency
66
+ @updated_at = updated_at
67
+ end
68
+ end
69
+ end
@@ -0,0 +1,80 @@
1
+ module BloomRemit2
2
+ class Partner
3
+ class << self
4
+ def retrieve
5
+ response = Client.get(path).with_indifferent_access
6
+ new(
7
+ response[:id],
8
+ response[:name],
9
+ response[:slug],
10
+ response[:domain],
11
+ response[:active],
12
+ response[:orig_currency],
13
+ response[:dest_currency],
14
+ response[:min_amount_in_orig_currency],
15
+ response[:max_amount_in_orig_currency],
16
+ response[:website],
17
+ response[:zip_code],
18
+ response[:country],
19
+ response[:phone],
20
+ response[:email],
21
+ response[:flat_fee_in_orig_currency],
22
+ response[:forex_margin],
23
+ response[:agent_ids],
24
+ response[:credit_in_php],
25
+ response[:credit_in_vnd]
26
+ )
27
+ end
28
+
29
+ private
30
+
31
+ def path
32
+ "api/v1/partners/#{BloomRemit2.configuration.api_token}"
33
+ end
34
+ end
35
+
36
+ attr_reader :id, :name, :slug, :domain, :active, :orig_currency, :dest_currency, :min_amount_in_orig_currency, :max_amount_in_orig_currency, :website, :zip_code, :country, :phone, :email, :flat_fee_in_orig_currency, :forex_margin, :agent_ids, :credit_in_php, :credit_in_vnd
37
+
38
+ def initialize(
39
+ id,
40
+ name,
41
+ slug,
42
+ domain,
43
+ active,
44
+ orig_currency,
45
+ dest_currency,
46
+ min_amount_in_orig_currency,
47
+ max_amount_in_orig_currency,
48
+ website,
49
+ zip_code,
50
+ country,
51
+ phone,
52
+ email,
53
+ flat_fee_in_orig_currency,
54
+ forex_margin,
55
+ agent_ids,
56
+ credit_in_php,
57
+ credit_in_vnd
58
+ )
59
+ @id = id
60
+ @name = name
61
+ @slug = slug
62
+ @domain = domain
63
+ @active = active
64
+ @orig_currency = orig_currency
65
+ @dest_currency = dest_currency
66
+ @min_amount_in_orig_currency = min_amount_in_orig_currency
67
+ @max_amount_in_orig_currency = max_amount_in_orig_currency
68
+ @website = website
69
+ @zip_code = zip_code
70
+ @country = country
71
+ @phone = phone
72
+ @email = email
73
+ @flat_fee_in_orig_currency = flat_fee_in_orig_currency
74
+ @forex_margin = forex_margin
75
+ @agent_ids = agent_ids
76
+ @credit_in_php = credit_in_php
77
+ @credit_in_vnd = credit_in_vnd
78
+ end
79
+ end
80
+ end
@@ -0,0 +1,31 @@
1
+ module BloomRemit2
2
+ class Rate
3
+ class << self
4
+ # Returns a real-time hash of currency exchange rates that update every minute
5
+ #
6
+ # Supported currencies include: AED, AUD, BCH, BTC, CAD, CNY, DASH, ETH, EUR, GBP, HKD,
7
+ # IDR, INR, JPY, KRW, LINK, LTC, MYR, NPR, PHP, SGD, THB, USD, USDT, VND, XLM, XRP, ZAR.
8
+ # By default, all rates provided are relative to USD.
9
+ #
10
+ # @return currency_exchange_rates [Hash] where key is 6-letter currency pair and value is price
11
+ def list
12
+ rates = Client.get(path)
13
+ JSON.parse(rates.body)
14
+ end
15
+
16
+ # Retrieves one currency exchange rate
17
+ # @param currency_pair [String] 6-letter currency pair (e.g. 'USDBTC')
18
+ # @return rate [BloomRemit2::Rate]
19
+ def retrieve(currency_pair)
20
+ price = Client.get("#{path}&currency=#{currency_pair}").body
21
+ { currency_pair => price.to_f }
22
+ end
23
+
24
+ private
25
+
26
+ def path
27
+ "api/v1/rates?partner_id=#{BloomRemit2.configuration.api_token}"
28
+ end
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,94 @@
1
+ module BloomRemit2
2
+ class Recipient
3
+ class << self
4
+ def create(sender_id, recipient_hash)
5
+ recipient = Client.post(path(sender_id), recipient_hash)
6
+ initialize_from_hash(recipient['recipient'])
7
+ end
8
+
9
+ def list(sender_id)
10
+ recipients = Client.get(path(sender_id))
11
+ recipients.map { |recipient| initialize_from_hash(recipient) }
12
+ end
13
+
14
+ def retrieve(sender_id, recipient_id)
15
+ recipient = Client.get("#{path(sender_id)}/#{recipient_id}")
16
+ initialize_from_hash(recipient['recipient'], recipient['remittance_ids'])
17
+ end
18
+
19
+ def update(sender_id, recipient_id, recipient_hash)
20
+ body = []
21
+ recipient_hash.each do |k, v|
22
+ body << ["recipient[#{k.to_s.downcase}]", v]
23
+ end
24
+ recipient = Client.put("#{path(sender_id)}/#{recipient_id}", body)
25
+ initialize_from_hash(recipient['recipient'])
26
+ end
27
+
28
+ def delete(sender_id, recipient_id)
29
+ message = Client.delete("#{path(sender_id)}/#{recipient_id}").with_indifferent_access
30
+ if message[:success] == "We've successfully deleted that recipient."
31
+ new(
32
+ recipient_id,
33
+ sender_id,
34
+ deleted: true
35
+ )
36
+ end
37
+ end
38
+
39
+ private
40
+
41
+ def path(sender_id)
42
+ "api/v1/partners/#{BloomRemit2.configuration.api_token}/senders/#{sender_id}/recipients"
43
+ end
44
+
45
+ def initialize_from_hash(recipient, remittance_ids=nil)
46
+ recipient = recipient.with_indifferent_access
47
+ new(
48
+ recipient[:id],
49
+ recipient[:sender_id],
50
+ recipient[:first_name],
51
+ recipient[:last_name],
52
+ recipient[:email],
53
+ recipient[:mobile],
54
+ recipient[:address],
55
+ recipient[:city],
56
+ recipient[:state],
57
+ recipient[:country],
58
+ remittance_ids
59
+ )
60
+ end
61
+ end
62
+
63
+ attr_reader :id, :sender_id, :first_name, :last_name, :email, :mobile, :address, :city, :province, :country, :remittance_ids, :deleted
64
+ alias :state :province
65
+
66
+ def initialize(
67
+ id,
68
+ sender_id,
69
+ first_name=nil,
70
+ last_name=nil,
71
+ email=nil,
72
+ mobile=nil,
73
+ address=nil,
74
+ city=nil,
75
+ province=nil,
76
+ country=nil,
77
+ remittance_ids=nil,
78
+ deleted: false
79
+ )
80
+ @id = id
81
+ @sender_id = sender_id
82
+ @first_name = first_name
83
+ @last_name = last_name
84
+ @email = email
85
+ @mobile = mobile
86
+ @address = address
87
+ @city = city
88
+ @province = province
89
+ @country = country
90
+ @remittance_ids = remittance_ids
91
+ @deleted = deleted
92
+ end
93
+ end
94
+ end