bank_exchange_api 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.env.example +1 -0
- data/.gitignore +12 -0
- data/.rspec +2 -0
- data/Gemfile +4 -0
- data/README.md +140 -0
- data/Rakefile +6 -0
- data/bank_exchange_api.gemspec +35 -0
- data/bin/console +21 -0
- data/bin/setup +8 -0
- data/lib/bank_exchange_api.rb +61 -0
- data/lib/bank_exchange_api/bm.rb +17 -0
- data/lib/bank_exchange_api/config.rb +38 -0
- data/lib/bank_exchange_api/connection.rb +47 -0
- data/lib/bank_exchange_api/errors.rb +21 -0
- data/lib/bank_exchange_api/param.rb +34 -0
- data/lib/bank_exchange_api/request.rb +10 -0
- data/lib/bank_exchange_api/request/bank.rb +24 -0
- data/lib/bank_exchange_api/request/banks.rb +20 -0
- data/lib/bank_exchange_api/request/base.rb +34 -0
- data/lib/bank_exchange_api/request/ping.rb +8 -0
- data/lib/bank_exchange_api/request/rate.rb +25 -0
- data/lib/bank_exchange_api/request/rates.rb +25 -0
- data/lib/bank_exchange_api/response.rb +6 -0
- data/lib/bank_exchange_api/response/base.rb +36 -0
- data/lib/bank_exchange_api/response/json.rb +26 -0
- data/lib/bank_exchange_api/version.rb +3 -0
- data/log/.keep +0 -0
- metadata +169 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 2829bbc1fdddd9b6dca0279223786e8d608ff722
|
4
|
+
data.tar.gz: f0da7b52da3ee20fba8c00147fb6ae737d5d63f0
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 75004c2f7ecc4d768f96e71382059f4e784276fdc26b1121c569798698861cef4500ca5d6128e221e29d851af01b1dc614fae128ec3261236fc0f7e9fcde13ef
|
7
|
+
data.tar.gz: 2887f461fb642f7ff34e0dd0773a4e76024a28dca7e35ffd686de60dc7191e932b3fabb331f7e514ee24b0ad02c3c721147a2af5df056d7b6177d30bb862eb7a
|
data/.env.example
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
BANK_EXCHANGE_API_TOKEN=bank_exchange_api_token
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/Gemfile
ADDED
data/README.md
ADDED
@@ -0,0 +1,140 @@
|
|
1
|
+
# BankExchangeApi
|
2
|
+
|
3
|
+
[![Build Status](https://semaphoreci.com/api/v1/shlima/bank_exchange_ruby_api/branches/master/badge.svg)](https://semaphoreci.com/shlima/bank_exchange_ruby_api) [![Code Climate](https://codeclimate.com/github/BankExchange/bank_exchange_ruby_api/badges/gpa.svg)](https://codeclimate.com/github/BankExchange/bank_exchange_ruby_api) [![Dependency Status](https://gemnasium.com/BankExchange/bank_exchange_ruby_api.svg)](https://gemnasium.com/BankExchange/bank_exchange_ruby_api)
|
4
|
+
|
5
|
+
> STATUS: WIP
|
6
|
+
|
7
|
+
This is the Ruby API client for the BankExchange service. Please read the official documentation to get further information http://bank.exchange/documentation
|
8
|
+
|
9
|
+
## Configuration
|
10
|
+
|
11
|
+
```ruby
|
12
|
+
BankExchangeApi::Config.api_token = "33f411e7-cebe-4f7e-a8f3-24bfc8ecca6e"
|
13
|
+
BankExchangeApi::Config.logger = Logger.new(STDOUT)
|
14
|
+
```
|
15
|
+
|
16
|
+
## CLI
|
17
|
+
|
18
|
+
```ruby
|
19
|
+
# @option api_token [String]
|
20
|
+
# @option logger [Logger,StringIO]
|
21
|
+
@cli = BankExchangeApi::Cli.new(api_token: '33f411e7-cebe-4f7e-a8f3-24bfc8ecca6e')
|
22
|
+
```
|
23
|
+
|
24
|
+
## Ping Query
|
25
|
+
|
26
|
+
```ruby
|
27
|
+
# @return [Boolean]
|
28
|
+
@cli.ping #=> true
|
29
|
+
```
|
30
|
+
|
31
|
+
## Banks list [response methods overview]
|
32
|
+
|
33
|
+
```ruby
|
34
|
+
# @option countries [Array]
|
35
|
+
# @option countries [Array]
|
36
|
+
request = @cli.banks(countries: ['US'])
|
37
|
+
request.currencies = ['USD']
|
38
|
+
|
39
|
+
response = request.json
|
40
|
+
```
|
41
|
+
|
42
|
+
```ruby
|
43
|
+
response.success? #=> true
|
44
|
+
```
|
45
|
+
|
46
|
+
```ruby
|
47
|
+
# @return [Hash]
|
48
|
+
response.params
|
49
|
+
{"countries"=>["US"], "currencies"=>["USD"]}
|
50
|
+
```
|
51
|
+
|
52
|
+
```ruby
|
53
|
+
# @return [Hash]
|
54
|
+
response.data
|
55
|
+
[{"swift"=>"XXXXXXXX", "name"=>"Board of Governors of the Federal Reserve System", "country"=>"US", "currency"=>"USD", "website"=>"http://www.federalreserve.gov"}]
|
56
|
+
```
|
57
|
+
|
58
|
+
```ruby
|
59
|
+
# @return [Hash]
|
60
|
+
response.body
|
61
|
+
{params: {"countries"=>["US"], "currencies"=>["USD"]}, banks: [{"swift"=>"XXXXXXXX", "name"=>"Board of Governors of the Federal Reserve System", "country"=>"US", "currency"=>"USD", "website"=>"http://www.federalreserve.gov"}]}
|
62
|
+
```
|
63
|
+
|
64
|
+
## Bank overview
|
65
|
+
|
66
|
+
```ruby
|
67
|
+
# @param swift [String]
|
68
|
+
# @option currencies [Array]
|
69
|
+
# @option date [Date,String]
|
70
|
+
# @option fallback_days [Integer]
|
71
|
+
response = @cli.bank("XXXXXXXX", date: Date.today, currencies: ['EUR']).json
|
72
|
+
```
|
73
|
+
|
74
|
+
```ruby
|
75
|
+
response.params
|
76
|
+
{"swift"=>"XXXXXXXX", "currencies"=>["EUR"], "date"=>"2016-03-22", "fallback_days"=>5}
|
77
|
+
```
|
78
|
+
|
79
|
+
```ruby
|
80
|
+
response.data
|
81
|
+
[{"iso_from"=>"USD", "iso_to"=>"EUR", "rate"=>0.885582713425434, "inverse_rate"=>1.1292, "date"=>"2016-03-18"}]
|
82
|
+
```
|
83
|
+
|
84
|
+
```ruby
|
85
|
+
response.body
|
86
|
+
{"params"=>{"swift"=>"XXXXXXXX", "currencies"=>["EUR"], "date"=>"2016-03-22", "fallback_days"=>4}, "bank"=>{"swift"=>"XXXXXXXX", "name"=>"Board of Governors of the Federal Reserve System", "country"=>"US", "currency"=>"USD", "website"=>"http://www.federalreserve.gov"}, "rates"=>[{"iso_from"=>"USD", "iso_to"=>"EUR", "rate"=>0.885582713425434, "inverse_rate"=>1.1292, "date"=>"2016-03-18"}]}
|
87
|
+
```
|
88
|
+
|
89
|
+
## Rates list
|
90
|
+
|
91
|
+
```ruby
|
92
|
+
# @param swift [Array]
|
93
|
+
# @option iso_from [Array]
|
94
|
+
# @option iso_to [Array]
|
95
|
+
# @option date [Date,String]
|
96
|
+
# @option fallback_days [Integer]
|
97
|
+
response = @cli.rates(date: Date.today, iso_from: ['BYR'], iso_to: ['USD']).json
|
98
|
+
```
|
99
|
+
|
100
|
+
```ruby
|
101
|
+
response.params
|
102
|
+
{"date"=>"2016-03-24", "swift"=>[], "iso_from"=>["BYR"], "iso_to"=>["USD"], "fallback_days"=>5}
|
103
|
+
```
|
104
|
+
|
105
|
+
```ruby
|
106
|
+
response.data
|
107
|
+
[{"iso_from"=>"BYR", "iso_to"=>"USD", "rate"=>4.96007142502852e-05, "inverse_rate"=>20161.0, "swift"=>"NBRBBY2X", "date"=>"2016-03-24"}]
|
108
|
+
```
|
109
|
+
|
110
|
+
```ruby
|
111
|
+
response.data
|
112
|
+
{"params"=>{"date"=>"2016-03-24", "swift"=>[], "iso_from"=>["BYR"], "iso_to"=>["USD"], "fallback_days"=>5}, "rates"=>[{"iso_from"=>"BYR", "iso_to"=>"USD", "rate"=>4.96007142502852e-05, "inverse_rate"=>20161.0, "swift"=>"NBRBBY2X", "date"=>"2016-03-24"}]}
|
113
|
+
```
|
114
|
+
|
115
|
+
## Rate overview
|
116
|
+
|
117
|
+
```ruby
|
118
|
+
# @param iso_code [String]
|
119
|
+
# @option iso_from [Array]
|
120
|
+
# @option iso_to [Array]
|
121
|
+
# @option date [Date,String]
|
122
|
+
# @option fallback_days [Integer]
|
123
|
+
response = @cli.rate('BYR', date: Date.today, iso_from: ['BYR'], iso_to: ['RUB']).json
|
124
|
+
```
|
125
|
+
|
126
|
+
```ruby
|
127
|
+
response.params
|
128
|
+
{"iso_code"=>"BYR", "date"=>"2016-03-25", "iso_from"=>["BYR"], "iso_to"=>["RUB"], "fallback_days"=>5}
|
129
|
+
```
|
130
|
+
|
131
|
+
```ruby
|
132
|
+
response.data
|
133
|
+
[{"iso_from"=>"BYR", "iso_to"=>"RUB", "rate"=>0.00340193910529002, "inverse_rate"=>293.95, "swift"=>"NBRBBY2X", "date"=>"2016-03-25"}]
|
134
|
+
```
|
135
|
+
|
136
|
+
```ruby
|
137
|
+
response.body
|
138
|
+
{"params"=>{"iso_code"=>"BYR", "date"=>"2016-03-25", "iso_from"=>["BYR"], "iso_to"=>["RUB"], "fallback_days"=>5}, "rates"=>[{"iso_from"=>"BYR", "iso_to"=>"RUB", "rate"=>0.00340193910529002, "inverse_rate"=>293.95, "swift"=>"NBRBBY2X", "date"=>"2016-03-25"}]}
|
139
|
+
```
|
140
|
+
|
data/Rakefile
ADDED
@@ -0,0 +1,35 @@
|
|
1
|
+
lib = File.expand_path('../lib', __FILE__)
|
2
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
3
|
+
require 'bank_exchange_api/version'
|
4
|
+
|
5
|
+
Gem::Specification.new do |spec|
|
6
|
+
spec.name = 'bank_exchange_api'
|
7
|
+
spec.version = BankExchangeApi::VERSION
|
8
|
+
spec.authors = ['Aliaksandr Shylau']
|
9
|
+
spec.email = ['alex.shilov.by@gmail.com']
|
10
|
+
|
11
|
+
spec.summary = 'BankExchange official API client'
|
12
|
+
spec.description = 'Rates for every world currency from 160+ Central Banks.'
|
13
|
+
spec.homepage = 'https://github.com/BankExchange/bank_exchange_ruby_api'
|
14
|
+
|
15
|
+
# Prevent pushing this gem to RubyGems.org by setting 'allowed_push_host', or
|
16
|
+
# delete this section to allow pushing this gem to any host.
|
17
|
+
if spec.respond_to?(:metadata)
|
18
|
+
spec.metadata['allowed_push_host'] = "https://rubygems.org"
|
19
|
+
else
|
20
|
+
raise 'RubyGems 2.0 or newer is required to protect against public gem pushes.'
|
21
|
+
end
|
22
|
+
|
23
|
+
spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
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_development_dependency 'bundler'
|
29
|
+
spec.add_development_dependency 'rake'
|
30
|
+
spec.add_development_dependency 'rspec'
|
31
|
+
spec.add_development_dependency 'pry'
|
32
|
+
spec.add_development_dependency 'webmock'
|
33
|
+
spec.add_development_dependency 'vcr'
|
34
|
+
spec.add_development_dependency 'dotenv'
|
35
|
+
end
|
data/bin/console
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "bank_exchange_api"
|
5
|
+
require "pry"
|
6
|
+
require "logger"
|
7
|
+
require "dotenv"
|
8
|
+
|
9
|
+
Dotenv.load
|
10
|
+
BankExchangeApi::Config.api_token = ENV.fetch('BANK_EXCHANGE_API_TOKEN')
|
11
|
+
BankExchangeApi::Config.logger = Logger.new(STDOUT)
|
12
|
+
|
13
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
14
|
+
# with your gem easier. You can also use a different console, if you like.
|
15
|
+
|
16
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
17
|
+
# require "pry"
|
18
|
+
# Pry.start
|
19
|
+
|
20
|
+
require "irb"
|
21
|
+
IRB.start
|
data/bin/setup
ADDED
@@ -0,0 +1,61 @@
|
|
1
|
+
# Core
|
2
|
+
require 'bank_exchange_api/version'
|
3
|
+
require 'bank_exchange_api/errors'
|
4
|
+
require 'bank_exchange_api/config'
|
5
|
+
require 'bank_exchange_api/connection'
|
6
|
+
require 'bank_exchange_api/bm'
|
7
|
+
require 'bank_exchange_api/param'
|
8
|
+
require 'bank_exchange_api/response'
|
9
|
+
require 'bank_exchange_api/request'
|
10
|
+
|
11
|
+
module BankExchangeApi
|
12
|
+
class Cli
|
13
|
+
# @param config_params [Hash]
|
14
|
+
def initialize(config_params={})
|
15
|
+
config_params.each{ |key, value| config.public_send("#{key}=", value) }
|
16
|
+
end
|
17
|
+
|
18
|
+
def config
|
19
|
+
@config ||= Config.new
|
20
|
+
end
|
21
|
+
|
22
|
+
def logger
|
23
|
+
@logger ||= config.logger
|
24
|
+
end
|
25
|
+
|
26
|
+
def connection
|
27
|
+
@connection ||= Connection.new(self)
|
28
|
+
end
|
29
|
+
|
30
|
+
def info(value)
|
31
|
+
logger && logger.info(value)
|
32
|
+
end
|
33
|
+
|
34
|
+
def error(value)
|
35
|
+
logger && logger.error(value)
|
36
|
+
end
|
37
|
+
|
38
|
+
# @return [Boolean]
|
39
|
+
def ping
|
40
|
+
Request::Ping.new(self).json.success?
|
41
|
+
rescue BankExchangeApi::UnsuccessfulResponse
|
42
|
+
false
|
43
|
+
end
|
44
|
+
|
45
|
+
def banks(params={})
|
46
|
+
Request::Banks.new(self, params)
|
47
|
+
end
|
48
|
+
|
49
|
+
def bank(swift, params={})
|
50
|
+
Request::Bank.new(self, params.merge(swift: swift))
|
51
|
+
end
|
52
|
+
|
53
|
+
def rates(params={})
|
54
|
+
Request::Rates.new(self, params)
|
55
|
+
end
|
56
|
+
|
57
|
+
def rate(iso_code, params={})
|
58
|
+
Request::Rate.new(self, params.merge(iso_code: iso_code))
|
59
|
+
end
|
60
|
+
end
|
61
|
+
end
|
@@ -0,0 +1,17 @@
|
|
1
|
+
module BankExchangeApi
|
2
|
+
module Bm
|
3
|
+
# @example
|
4
|
+
# Bm.measure(-> (ms) { puts ms }) do
|
5
|
+
# sleep(2)
|
6
|
+
# end
|
7
|
+
def self.measure(cb)
|
8
|
+
t1 = Time.now
|
9
|
+
result = yield
|
10
|
+
ms = (Time.now - t1).to_f * 1000
|
11
|
+
|
12
|
+
cb.call(ms.round)
|
13
|
+
|
14
|
+
result
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
@@ -0,0 +1,38 @@
|
|
1
|
+
module BankExchangeApi
|
2
|
+
class Config
|
3
|
+
# Defines both class & instance methods
|
4
|
+
# Works the same as *cattr_accessor* from *ActiveSupport*
|
5
|
+
# on a class level (globally) and an instance level (locally)
|
6
|
+
def self.conf_accessor(*names)
|
7
|
+
names.each do |name|
|
8
|
+
class_eval <<-METHODS, __FILE__, __LINE__ + 1
|
9
|
+
def #{name}
|
10
|
+
@#{name} || self.class.#{name}
|
11
|
+
end
|
12
|
+
|
13
|
+
def #{name}!
|
14
|
+
self.#{name} || self.class.#{name}!
|
15
|
+
end
|
16
|
+
|
17
|
+
def #{name}=(value)
|
18
|
+
@#{name} = value
|
19
|
+
end
|
20
|
+
|
21
|
+
def self.#{name}
|
22
|
+
@@#{name} if class_variable_defined?(:"@@#{name}")
|
23
|
+
end
|
24
|
+
|
25
|
+
def self.#{name}=(value)
|
26
|
+
@@#{name} = value
|
27
|
+
end
|
28
|
+
|
29
|
+
def self.#{name}!
|
30
|
+
self.#{name} || (raise ConfigurationError, "'#{name}' config param is required")
|
31
|
+
end
|
32
|
+
METHODS
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
conf_accessor :api_token, :logger
|
37
|
+
end
|
38
|
+
end
|
@@ -0,0 +1,47 @@
|
|
1
|
+
module BankExchangeApi
|
2
|
+
class Connection
|
3
|
+
|
4
|
+
HOST = 'api.bank.exchange'.freeze
|
5
|
+
PORT = 80.freeze
|
6
|
+
|
7
|
+
attr_reader :cli
|
8
|
+
|
9
|
+
def initialize(cli)
|
10
|
+
@cli = cli
|
11
|
+
end
|
12
|
+
|
13
|
+
def transport
|
14
|
+
@transport ||= Net::HTTP.new(HOST, PORT)
|
15
|
+
end
|
16
|
+
|
17
|
+
def headers
|
18
|
+
{
|
19
|
+
'Accept' => 'application/json',
|
20
|
+
'X-Api-Token' => cli.config.api_token!,
|
21
|
+
'X-Client' => 'bank_exchange_ruby_api',
|
22
|
+
'X-Client-Version' => VERSION
|
23
|
+
}
|
24
|
+
end
|
25
|
+
|
26
|
+
def get(uri)
|
27
|
+
bm :GET, uri do
|
28
|
+
transport.get(uri, headers).tap do |http|
|
29
|
+
error(uri, http) unless http.is_a?(Net::HTTPOK)
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
private
|
35
|
+
|
36
|
+
def error(uri, http)
|
37
|
+
message = "[#{http.code}] ERROR: #{http.body} while processing #{uri}"
|
38
|
+
cli.error(message)
|
39
|
+
raise UnsuccessfulResponse, message
|
40
|
+
end
|
41
|
+
|
42
|
+
def bm(prefix, message, &block)
|
43
|
+
cb = -> (ms) { cli.info("[#{prefix} #{ms}ms.] #{message}") }
|
44
|
+
Bm.measure(cb, &block)
|
45
|
+
end
|
46
|
+
end
|
47
|
+
end
|
@@ -0,0 +1,21 @@
|
|
1
|
+
module BankExchangeApi
|
2
|
+
# Generic exception class
|
3
|
+
class Error < StandardError
|
4
|
+
end
|
5
|
+
|
6
|
+
# Raise when config is being configured improperly
|
7
|
+
# or configuration param is invalid
|
8
|
+
class ConfigurationError < Error
|
9
|
+
end
|
10
|
+
|
11
|
+
# Raise when class passed into a Param accessor is unsupported
|
12
|
+
class UnsupportedParamClass < Error
|
13
|
+
end
|
14
|
+
|
15
|
+
class MissingRequiredParam < Error
|
16
|
+
end
|
17
|
+
|
18
|
+
# When server responses not 2xx
|
19
|
+
class UnsuccessfulResponse < Error
|
20
|
+
end
|
21
|
+
end
|
@@ -0,0 +1,34 @@
|
|
1
|
+
module BankExchangeApi
|
2
|
+
module Param
|
3
|
+
def param(*names, klass)
|
4
|
+
names.each do |name|
|
5
|
+
class_eval <<-METHODS, __FILE__, __LINE__ + 1
|
6
|
+
def #{name}=(value)
|
7
|
+
@#{name}=value
|
8
|
+
end
|
9
|
+
|
10
|
+
def #{name}
|
11
|
+
@_#{name}_ ||= case #{klass}.name
|
12
|
+
when 'Array'
|
13
|
+
Array(@#{name})
|
14
|
+
when 'String'
|
15
|
+
String(@#{name}) if @#{name}
|
16
|
+
when 'Integer'
|
17
|
+
Integer(@#{name}) if @#{name}
|
18
|
+
when 'Date'
|
19
|
+
if @#{name}
|
20
|
+
@#{name}.is_a?(Date) ? @#{name} : Date.parse(@#{name}.to_s)
|
21
|
+
end
|
22
|
+
else
|
23
|
+
raise UnsupportedParamClass, "Provide #{klass} class processing"
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
def #{name}!
|
28
|
+
self.#{name} || (raise MissingRequiredParam, "Param '#{name}' is required but missing")
|
29
|
+
end
|
30
|
+
METHODS
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
@@ -0,0 +1,10 @@
|
|
1
|
+
module BankExchangeApi
|
2
|
+
module Request
|
3
|
+
autoload :Base , 'bank_exchange_api/request/base'
|
4
|
+
autoload :Ping , 'bank_exchange_api/request/ping'
|
5
|
+
autoload :Banks , 'bank_exchange_api/request/banks'
|
6
|
+
autoload :Bank , 'bank_exchange_api/request/bank'
|
7
|
+
autoload :Rates , 'bank_exchange_api/request/rates'
|
8
|
+
autoload :Rate , 'bank_exchange_api/request/rate'
|
9
|
+
end
|
10
|
+
end
|
@@ -0,0 +1,24 @@
|
|
1
|
+
module BankExchangeApi::Request
|
2
|
+
class Bank < Base
|
3
|
+
param :swift, String
|
4
|
+
param :currencies, Array
|
5
|
+
param :date, Date
|
6
|
+
param :fallback_days, Integer
|
7
|
+
|
8
|
+
def json
|
9
|
+
super(root: :rates)
|
10
|
+
end
|
11
|
+
|
12
|
+
def params
|
13
|
+
{
|
14
|
+
currencies: currencies.join(','),
|
15
|
+
date: date,
|
16
|
+
fallback_days: fallback_days,
|
17
|
+
}
|
18
|
+
end
|
19
|
+
|
20
|
+
def endpoint
|
21
|
+
"/banks/#{swift!}"
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
@@ -0,0 +1,20 @@
|
|
1
|
+
module BankExchangeApi::Request
|
2
|
+
class Banks < Base
|
3
|
+
param :countries, :currencies, Array
|
4
|
+
|
5
|
+
def json
|
6
|
+
super(root: :banks)
|
7
|
+
end
|
8
|
+
|
9
|
+
def params
|
10
|
+
{
|
11
|
+
countries: countries.join(','),
|
12
|
+
currencies: currencies.join(',')
|
13
|
+
}
|
14
|
+
end
|
15
|
+
|
16
|
+
def endpoint
|
17
|
+
'/banks'
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
@@ -0,0 +1,34 @@
|
|
1
|
+
module BankExchangeApi
|
2
|
+
module Request
|
3
|
+
class Base
|
4
|
+
extend Param
|
5
|
+
|
6
|
+
attr_reader :cli
|
7
|
+
|
8
|
+
def initialize(cli, params={})
|
9
|
+
@cli = cli
|
10
|
+
params.each{ |k,v| public_send("#{k}=", v) }
|
11
|
+
end
|
12
|
+
|
13
|
+
def query
|
14
|
+
[endpoint, URI.encode_www_form(params)].join('?')
|
15
|
+
end
|
16
|
+
|
17
|
+
def get(*args)
|
18
|
+
cli.connection.get(*args)
|
19
|
+
end
|
20
|
+
|
21
|
+
def json(root: nil)
|
22
|
+
BankExchangeApi::Response::Json.new(get(query), root: root)
|
23
|
+
end
|
24
|
+
|
25
|
+
def params
|
26
|
+
{}
|
27
|
+
end
|
28
|
+
|
29
|
+
def endpoint
|
30
|
+
raise NotImplementedError, __method__
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
@@ -0,0 +1,25 @@
|
|
1
|
+
module BankExchangeApi::Request
|
2
|
+
class Rate < Base
|
3
|
+
param :iso_code, String
|
4
|
+
param :iso_from, :iso_to, Array
|
5
|
+
param :date, Date
|
6
|
+
param :fallback_days, Integer
|
7
|
+
|
8
|
+
def json
|
9
|
+
super(root: :rates)
|
10
|
+
end
|
11
|
+
|
12
|
+
def params
|
13
|
+
{
|
14
|
+
date: date,
|
15
|
+
iso_from: iso_from.join(','),
|
16
|
+
iso_to: iso_to.join(','),
|
17
|
+
fallback_days: fallback_days
|
18
|
+
}
|
19
|
+
end
|
20
|
+
|
21
|
+
def endpoint
|
22
|
+
"/rates/#{iso_code!}"
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
@@ -0,0 +1,25 @@
|
|
1
|
+
module BankExchangeApi::Request
|
2
|
+
class Rates < Base
|
3
|
+
param :swift, :iso_from, :iso_to, Array
|
4
|
+
param :date, Date
|
5
|
+
param :fallback_days, Integer
|
6
|
+
|
7
|
+
def json
|
8
|
+
super(root: :rates)
|
9
|
+
end
|
10
|
+
|
11
|
+
def params
|
12
|
+
{
|
13
|
+
date: date,
|
14
|
+
swift: swift.join(','),
|
15
|
+
iso_from: iso_from.join(','),
|
16
|
+
iso_to: iso_to.join(','),
|
17
|
+
fallback_days: fallback_days
|
18
|
+
}
|
19
|
+
end
|
20
|
+
|
21
|
+
def endpoint
|
22
|
+
'/rates'
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
@@ -0,0 +1,36 @@
|
|
1
|
+
module BankExchangeApi::Response
|
2
|
+
class Base
|
3
|
+
attr_reader :http, :root
|
4
|
+
|
5
|
+
def initialize(http, root: nil)
|
6
|
+
@http = http
|
7
|
+
@root = root
|
8
|
+
end
|
9
|
+
|
10
|
+
def success?
|
11
|
+
http.code.to_i == 200
|
12
|
+
end
|
13
|
+
|
14
|
+
# Response payload data
|
15
|
+
# @example
|
16
|
+
# rates: [{currency: 'EUR'}, {currency: 'EUR'}]
|
17
|
+
# User *root* param to access the data
|
18
|
+
def data
|
19
|
+
raise NotImplementedError, __method__
|
20
|
+
end
|
21
|
+
|
22
|
+
# Request params in responce
|
23
|
+
# @example
|
24
|
+
# params: {currencies: 'EUR,USD'}
|
25
|
+
def params
|
26
|
+
raise NotImplementedError, __method__
|
27
|
+
end
|
28
|
+
|
29
|
+
# Response body
|
30
|
+
# @example
|
31
|
+
# params: {}, rates: []
|
32
|
+
def body
|
33
|
+
raise NotImplementedError, __method__
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
@@ -0,0 +1,26 @@
|
|
1
|
+
require 'json'
|
2
|
+
|
3
|
+
module BankExchangeApi::Response
|
4
|
+
class Json < Base
|
5
|
+
|
6
|
+
extend Forwardable
|
7
|
+
|
8
|
+
def_delegators :data, :each
|
9
|
+
|
10
|
+
def body
|
11
|
+
@body ||= JSON.parse(http.body)
|
12
|
+
end
|
13
|
+
|
14
|
+
def params
|
15
|
+
body.fetch('params', {})
|
16
|
+
end
|
17
|
+
|
18
|
+
def data
|
19
|
+
root ? body[root.to_s] : body
|
20
|
+
end
|
21
|
+
|
22
|
+
def inspect
|
23
|
+
data
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
data/log/.keep
ADDED
File without changes
|
metadata
ADDED
@@ -0,0 +1,169 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: bank_exchange_api
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Aliaksandr Shylau
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2016-05-18 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: '0'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ">="
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '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: '0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: pry
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - ">="
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - ">="
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: webmock
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - ">="
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '0'
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - ">="
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '0'
|
83
|
+
- !ruby/object:Gem::Dependency
|
84
|
+
name: vcr
|
85
|
+
requirement: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - ">="
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: '0'
|
90
|
+
type: :development
|
91
|
+
prerelease: false
|
92
|
+
version_requirements: !ruby/object:Gem::Requirement
|
93
|
+
requirements:
|
94
|
+
- - ">="
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: '0'
|
97
|
+
- !ruby/object:Gem::Dependency
|
98
|
+
name: dotenv
|
99
|
+
requirement: !ruby/object:Gem::Requirement
|
100
|
+
requirements:
|
101
|
+
- - ">="
|
102
|
+
- !ruby/object:Gem::Version
|
103
|
+
version: '0'
|
104
|
+
type: :development
|
105
|
+
prerelease: false
|
106
|
+
version_requirements: !ruby/object:Gem::Requirement
|
107
|
+
requirements:
|
108
|
+
- - ">="
|
109
|
+
- !ruby/object:Gem::Version
|
110
|
+
version: '0'
|
111
|
+
description: Rates for every world currency from 160+ Central Banks.
|
112
|
+
email:
|
113
|
+
- alex.shilov.by@gmail.com
|
114
|
+
executables: []
|
115
|
+
extensions: []
|
116
|
+
extra_rdoc_files: []
|
117
|
+
files:
|
118
|
+
- ".env.example"
|
119
|
+
- ".gitignore"
|
120
|
+
- ".rspec"
|
121
|
+
- Gemfile
|
122
|
+
- README.md
|
123
|
+
- Rakefile
|
124
|
+
- bank_exchange_api.gemspec
|
125
|
+
- bin/console
|
126
|
+
- bin/setup
|
127
|
+
- lib/bank_exchange_api.rb
|
128
|
+
- lib/bank_exchange_api/bm.rb
|
129
|
+
- lib/bank_exchange_api/config.rb
|
130
|
+
- lib/bank_exchange_api/connection.rb
|
131
|
+
- lib/bank_exchange_api/errors.rb
|
132
|
+
- lib/bank_exchange_api/param.rb
|
133
|
+
- lib/bank_exchange_api/request.rb
|
134
|
+
- lib/bank_exchange_api/request/bank.rb
|
135
|
+
- lib/bank_exchange_api/request/banks.rb
|
136
|
+
- lib/bank_exchange_api/request/base.rb
|
137
|
+
- lib/bank_exchange_api/request/ping.rb
|
138
|
+
- lib/bank_exchange_api/request/rate.rb
|
139
|
+
- lib/bank_exchange_api/request/rates.rb
|
140
|
+
- lib/bank_exchange_api/response.rb
|
141
|
+
- lib/bank_exchange_api/response/base.rb
|
142
|
+
- lib/bank_exchange_api/response/json.rb
|
143
|
+
- lib/bank_exchange_api/version.rb
|
144
|
+
- log/.keep
|
145
|
+
homepage: https://github.com/BankExchange/bank_exchange_ruby_api
|
146
|
+
licenses: []
|
147
|
+
metadata:
|
148
|
+
allowed_push_host: https://rubygems.org
|
149
|
+
post_install_message:
|
150
|
+
rdoc_options: []
|
151
|
+
require_paths:
|
152
|
+
- lib
|
153
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
154
|
+
requirements:
|
155
|
+
- - ">="
|
156
|
+
- !ruby/object:Gem::Version
|
157
|
+
version: '0'
|
158
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
159
|
+
requirements:
|
160
|
+
- - ">="
|
161
|
+
- !ruby/object:Gem::Version
|
162
|
+
version: '0'
|
163
|
+
requirements: []
|
164
|
+
rubyforge_project:
|
165
|
+
rubygems_version: 2.5.1
|
166
|
+
signing_key:
|
167
|
+
specification_version: 4
|
168
|
+
summary: BankExchange official API client
|
169
|
+
test_files: []
|