bank_exchange_api 1.0.0 → 1.0.1

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 2829bbc1fdddd9b6dca0279223786e8d608ff722
4
- data.tar.gz: f0da7b52da3ee20fba8c00147fb6ae737d5d63f0
3
+ metadata.gz: ddbe43ca58c823d4d48a69fa48a4110fe92aa742
4
+ data.tar.gz: 45fcb1be71365bb05dd71e4f4db62615a0c7ff38
5
5
  SHA512:
6
- metadata.gz: 75004c2f7ecc4d768f96e71382059f4e784276fdc26b1121c569798698861cef4500ca5d6128e221e29d851af01b1dc614fae128ec3261236fc0f7e9fcde13ef
7
- data.tar.gz: 2887f461fb642f7ff34e0dd0773a4e76024a28dca7e35ffd686de60dc7191e932b3fabb331f7e514ee24b0ad02c3c721147a2af5df056d7b6177d30bb862eb7a
6
+ metadata.gz: 49f85a0d17821e66ece907e32ea9c17f55ede337b8c28a8f346ca115588b21b6aa7d7d76e9db5038e950ba06d6dcc75ee41f09fbe8345095da318403312ddbb1
7
+ data.tar.gz: 22d2a030ad788101ce06fa2eb4687c4f45c8b24f18e899f587d8d9e63d5375d4d38ee44cdb26b1c545bd6aaf3d4bedeaef781dcef5e4bb3180bf8a805f353501
@@ -0,0 +1,8 @@
1
+ ## 1.0.1
2
+
3
+ * REMOVALS
4
+ * Outdated param `countries` was renamed to `iso_to` in `@cli.bank`
5
+ * BUG FIXES
6
+ * Fix `instance variable is not initialized` warning
7
+ * ENHANCEMENTS
8
+ * Add pagination to some endpoints. Example: `@cli.rates{ |response| puts response.data }`
data/README.md CHANGED
@@ -2,10 +2,16 @@
2
2
 
3
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
4
 
5
- > STATUS: WIP
5
+ ## RUBY EXCHANGE RATES API
6
6
 
7
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
8
 
9
+ ## Installation
10
+
11
+ ```bash
12
+ gem install bank_exchange_api
13
+ ```
14
+
9
15
  ## Configuration
10
16
 
11
17
  ```ruby
@@ -28,7 +34,22 @@ This is the Ruby API client for the BankExchange service. Please read the offici
28
34
  @cli.ping #=> true
29
35
  ```
30
36
 
31
- ## Banks list [response methods overview]
37
+ ## Response methods
38
+ ```
39
+ request = @cli.banks(countries: ['US'])
40
+ response = request.json
41
+
42
+ response.data # root-data of the endpoint
43
+ response.body # whole response data
44
+ reponse.params # request params in response
45
+ response.pagination # pagination params
46
+ response.next_page_url # next page URL if resource is paginatable
47
+ response.paginatable? # if resource is paginatable and next page exists
48
+ response.not_paginatable?
49
+ response.success? #=> true if response status code is 2xx
50
+ ```
51
+
52
+ ## Banks list
32
53
 
33
54
  ```ruby
34
55
  # @option countries [Array]
@@ -39,10 +60,6 @@ This is the Ruby API client for the BankExchange service. Please read the offici
39
60
  response = request.json
40
61
  ```
41
62
 
42
- ```ruby
43
- response.success? #=> true
44
- ```
45
-
46
63
  ```ruby
47
64
  # @return [Hash]
48
65
  response.params
@@ -65,15 +82,15 @@ This is the Ruby API client for the BankExchange service. Please read the offici
65
82
 
66
83
  ```ruby
67
84
  # @param swift [String]
68
- # @option currencies [Array]
85
+ # @option iso_to [Array]
69
86
  # @option date [Date,String]
70
87
  # @option fallback_days [Integer]
71
- response = @cli.bank("XXXXXXXX", date: Date.today, currencies: ['EUR']).json
88
+ response = @cli.bank("XXXXXXXX", date: Date.today, iso_to: ['EUR']).json
72
89
  ```
73
90
 
74
91
  ```ruby
75
92
  response.params
76
- {"swift"=>"XXXXXXXX", "currencies"=>["EUR"], "date"=>"2016-03-22", "fallback_days"=>5}
93
+ {"swift"=>"XXXXXXXX", "iso_to"=>["EUR"], "date"=>"2016-03-22", "fallback_days"=>5}
77
94
  ```
78
95
 
79
96
  ```ruby
@@ -83,7 +100,19 @@ This is the Ruby API client for the BankExchange service. Please read the offici
83
100
 
84
101
  ```ruby
85
102
  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"}]}
103
+ {"params"=>{"swift"=>"XXXXXXXX", "iso_to"=>["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"}]}
104
+ ```
105
+
106
+ ```ruby
107
+ response.pagination
108
+ {"current_page"=>1, "last_page"=>true, "per_page"=>200, "next_page_url"=>nil}
109
+ ```
110
+
111
+ :warning: Please use pagination by providing a block. Otherwise first page returns.
112
+
113
+ ```ruby
114
+ data = []
115
+ @cli.bank("XXXXXXXX").json{ |response| data.concat(response.data) }
87
116
  ```
88
117
 
89
118
  ## Rates list
@@ -112,6 +141,18 @@ This is the Ruby API client for the BankExchange service. Please read the offici
112
141
  {"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
142
  ```
114
143
 
144
+ ```ruby
145
+ response.pagination
146
+ {"current_page"=>1, "last_page"=>false, "per_page"=>200, "next_page_url"=>"http://api.bank.exchange/rates?iso_from=&iso_to=&page=2&swift="}
147
+ ```
148
+
149
+ :warning: Please use pagination by providing a block. Otherwise first page returns.
150
+
151
+ ```ruby
152
+ data = []
153
+ @cli.rates.json{ |response| data.concat(response.data) }
154
+ ```
155
+
115
156
  ## Rate overview
116
157
 
117
158
  ```ruby
@@ -138,3 +179,14 @@ This is the Ruby API client for the BankExchange service. Please read the offici
138
179
  {"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
180
  ```
140
181
 
182
+ ```ruby
183
+ response.pagination
184
+ {"current_page"=>1, "last_page"=>true, "per_page"=>200, "next_page_url"=>nil}
185
+ ```
186
+
187
+ :warning: Please use pagination by providing a block. Otherwise first page returns.
188
+
189
+ ```ruby
190
+ data = []
191
+ @cli.rate('BYR').json{ |response| data.concat(response.data) }
192
+ ```
@@ -35,6 +35,10 @@ module BankExchangeApi
35
35
  logger && logger.error(value)
36
36
  end
37
37
 
38
+ def warn(value)
39
+ logger && logger.warn(value)
40
+ end
41
+
38
42
  # @return [Boolean]
39
43
  def ping
40
44
  Request::Ping.new(self).json.success?
@@ -7,7 +7,7 @@ module BankExchangeApi
7
7
  names.each do |name|
8
8
  class_eval <<-METHODS, __FILE__, __LINE__ + 1
9
9
  def #{name}
10
- @#{name} || self.class.#{name}
10
+ (defined?(@#{name}) && @#{name}) || self.class.#{name}
11
11
  end
12
12
 
13
13
  def #{name}!
@@ -8,16 +8,18 @@ module BankExchangeApi
8
8
  end
9
9
 
10
10
  def #{name}
11
+ value = @#{name} if defined?(@#{name})
12
+
11
13
  @_#{name}_ ||= case #{klass}.name
12
14
  when 'Array'
13
- Array(@#{name})
15
+ Array(value)
14
16
  when 'String'
15
- String(@#{name}) if @#{name}
17
+ String(value) if value
16
18
  when 'Integer'
17
- Integer(@#{name}) if @#{name}
19
+ Integer(value) if value
18
20
  when 'Date'
19
- if @#{name}
20
- @#{name}.is_a?(Date) ? @#{name} : Date.parse(@#{name}.to_s)
21
+ if value
22
+ value.is_a?(Date) ? value : Date.parse(value.to_s)
21
23
  end
22
24
  else
23
25
  raise UnsupportedParamClass, "Provide #{klass} class processing"
@@ -1,7 +1,7 @@
1
1
  module BankExchangeApi::Request
2
2
  class Bank < Base
3
3
  param :swift, String
4
- param :currencies, Array
4
+ param :iso_to, Array
5
5
  param :date, Date
6
6
  param :fallback_days, Integer
7
7
 
@@ -11,7 +11,7 @@ module BankExchangeApi::Request
11
11
 
12
12
  def params
13
13
  {
14
- currencies: currencies.join(','),
14
+ iso_to: iso_to.join(','),
15
15
  date: date,
16
16
  fallback_days: fallback_days,
17
17
  }
@@ -18,8 +18,14 @@ module BankExchangeApi
18
18
  cli.connection.get(*args)
19
19
  end
20
20
 
21
- def json(root: nil)
22
- BankExchangeApi::Response::Json.new(get(query), root: root)
21
+ def json(root: nil, &block)
22
+ paginator = -> (uri=query) { BankExchangeApi::Response::Json.new(get(uri), root: root) }
23
+
24
+ if block_given?
25
+ paginate(paginator) { |response| yield(response) }
26
+ else
27
+ paginate(paginator)
28
+ end
23
29
  end
24
30
 
25
31
  def params
@@ -29,6 +35,28 @@ module BankExchangeApi
29
35
  def endpoint
30
36
  raise NotImplementedError, __method__
31
37
  end
38
+
39
+ private
40
+
41
+ # @param paginator [Proc] should accept next page uri
42
+ # @block if not given - first response return
43
+ def paginate(paginator)
44
+ response = paginator.call
45
+
46
+ unless block_given?
47
+ if response.paginatable?
48
+ cli.warn('Requested resource contains a pagination, please provide a block to process each page. Initial page returned.')
49
+ end
50
+ return response
51
+ end
52
+
53
+ yield(response)
54
+
55
+ while response.paginatable? do
56
+ response = paginator.call(response.next_page_url)
57
+ yield(response)
58
+ end
59
+ end
32
60
  end
33
61
  end
34
62
  end
@@ -32,5 +32,21 @@ module BankExchangeApi::Response
32
32
  def body
33
33
  raise NotImplementedError, __method__
34
34
  end
35
+
36
+ def pagination
37
+ raise NotImplementedError, __method__
38
+ end
39
+
40
+ def next_page_url
41
+ raise NotImplementedError, __method__
42
+ end
43
+
44
+ def paginatable?
45
+ !next_page_url.nil?
46
+ end
47
+
48
+ def not_paginatable?
49
+ !paginatable?
50
+ end
35
51
  end
36
52
  end
@@ -15,12 +15,20 @@ module BankExchangeApi::Response
15
15
  body.fetch('params', {})
16
16
  end
17
17
 
18
+ def pagination
19
+ body.fetch('pagination', {})
20
+ end
21
+
22
+ def next_page_url
23
+ pagination['next_page_url']
24
+ end
25
+
18
26
  def data
19
27
  root ? body[root.to_s] : body
20
28
  end
21
29
 
22
30
  def inspect
23
- data
31
+ body
24
32
  end
25
33
  end
26
34
  end
@@ -1,3 +1,3 @@
1
1
  module BankExchangeApi
2
- VERSION = '1.0.0'
2
+ VERSION = '1.0.1'
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: bank_exchange_api
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0
4
+ version: 1.0.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Aliaksandr Shylau
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2016-05-18 00:00:00.000000000 Z
11
+ date: 2016-06-01 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -118,6 +118,7 @@ files:
118
118
  - ".env.example"
119
119
  - ".gitignore"
120
120
  - ".rspec"
121
+ - CHANGELOG.md
121
122
  - Gemfile
122
123
  - README.md
123
124
  - Rakefile