money-open-exchange-rates 0.6.1 → 0.7.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 73bc7cbbe69a86d58f6376c4670c53996aa6675d
4
- data.tar.gz: 79425bdbc492a05b9070791f919389a5e2f347b0
3
+ metadata.gz: 2bb0f3d444f2239012893ebb5b147ea7ed833685
4
+ data.tar.gz: 73c55f153751549bda537ea4450fbe1dcf13b955
5
5
  SHA512:
6
- metadata.gz: f8d8f3a886a0dfefc7f1172ebd72c81f5353cd96cfaae5477ba6d1c0e16ec2b2c4f546da7bb9cae06a597b3b8919d30eb0b39a0ac3b065fc0e9ca52c6a15bd3a
7
- data.tar.gz: 1cf0505b5695351d429deff4e42fe762295a259c1d99cd908bfde791bda134867ccf9a662aff0be576d0e9ffd481a32e2d5a9ff2900ebb0884d77913e2315c72
6
+ metadata.gz: e84776814c80e1720bd0bac0014fd30b0bd58028c73d1dae3ab284ab374c25eca032c91ee6aab353e308358b977bd18776940c07908cfc871c577e180f692be6
7
+ data.tar.gz: 8d6cac3341389cf16dd7431b2cd32a1b61d210afd9e650ccdfe6935500a8eba1bcd68de7922a468806ad5ba0a8234cac48fab7ff3034171d45a12d214c9f770d
data/History.md CHANGED
@@ -1,4 +1,20 @@
1
1
 
2
+ v0.7.0 / 2016-10-23
3
+ ===================
4
+
5
+ * Merge pull request #36 from @lautis / drop-json-gem
6
+ * Use bundled JSON instead of gem
7
+ * Better api url tests
8
+ * README https links
9
+ * Fix rubocop offense Style/StringLiterals
10
+ * Improve documentation from inch suggestions
11
+ * Skip integration test when OXR_APP_ID not present
12
+ * Avoid leak of api key in integration tests
13
+ * Update josscrowcroft/open-exchange-rates link
14
+ * More info about OXR
15
+ * Merge pull request #35 from spk/integration-test
16
+ * Better integration test
17
+
2
18
  v0.6.1 / 2016-09-21
3
19
  ===================
4
20
 
@@ -7,7 +23,7 @@ v0.6.1 / 2016-09-21
7
23
  * Add failing test for source_url
8
24
 
9
25
  v0.6.0 / 2016-09-13
10
- ==================
26
+ ===================
11
27
 
12
28
  * Merge pull request #29 from @xsve / pairrates
13
29
  * Implemented rate calculation for any pair of currencies via base currency
data/README.md CHANGED
@@ -3,6 +3,17 @@
3
3
  A gem that calculates the exchange rate using published rates from
4
4
  [open-exchange-rates](https://openexchangerates.org/)
5
5
 
6
+ Check [api documentation](https://docs.openexchangerates.org/)
7
+
8
+ * [Live](https://docs.openexchangerates.org/docs/latest-json) and
9
+ [historical](https://docs.openexchangerates.org/docs/historical-json)
10
+ exchange rates for
11
+ [180 currencies](https://docs.openexchangerates.org/docs/supported-currencies).
12
+ * [Free plan](https://openexchangerates.org/signup) hourly updates, with USD
13
+ base and up to 1,000 requests/month.
14
+ * Currency caching.
15
+ * Calculate pair rates.
16
+
6
17
  ## Installation
7
18
 
8
19
  Add this line to your application's Gemfile:
@@ -27,41 +38,43 @@ gem install money-open-exchange-rates
27
38
 
28
39
  ~~~ ruby
29
40
  require 'money/bank/open_exchange_rates_bank'
30
- moe = Money::Bank::OpenExchangeRatesBank.new
31
- moe.cache = 'path/to/file/cache'
32
- moe.app_id = 'your app id from https://openexchangerates.org/signup'
33
- moe.update_rates
41
+ oxr = Money::Bank::OpenExchangeRatesBank.new
42
+ oxr.cache = 'path/to/file/cache.json'
43
+ oxr.app_id = 'your app id from https://openexchangerates.org/signup'
44
+ oxr.update_rates
34
45
 
35
46
  # (optional)
36
47
  # set the seconds after than the current rates are automatically expired
37
48
  # by default, they never expire, in this example 1 day.
38
- moe.ttl_in_seconds = 86400
49
+ oxr.ttl_in_seconds = 86400
39
50
  # (optional)
40
51
  # use https to fetch rates from Open Exchange Rates
41
52
  # disabled by default to support free-tier users
42
53
  # see https://openexchangerates.org/documentation#https
43
- moe.secure_connection = true
54
+ oxr.secure_connection = true
44
55
  # (optional)
45
56
  # set historical date of the rate
46
57
  # see https://openexchangerates.org/documentation#historical-data
47
- moe.date = '2015-01-01'
58
+ oxr.date = '2015-01-01'
48
59
  # (optional)
49
60
  # Set the base currency for all rates. By default, USD is used.
50
61
  # OpenExchangeRates only allows USD as base currency
51
62
  # for the free plan users.
52
- moe.source = 'USD'
63
+ oxr.source = 'USD'
53
64
 
54
65
  # Store in cache
55
- moe.save_rates
66
+ oxr.save_rates
67
+
68
+ Money.default_bank = oxr
56
69
 
57
- Money.default_bank = moe
70
+ Money.default_bank.get_rate('USD', 'CAD')
58
71
  ~~~
59
72
 
60
73
  You can also provide a Proc as a cache to provide your own caching mechanism
61
74
  perhaps with Redis or just a thread safe `Hash` (global). For example:
62
75
 
63
76
  ~~~ ruby
64
- moe.cache = Proc.new do |v|
77
+ oxr.cache = Proc.new do |v|
65
78
  key = 'money:exchange_rates'
66
79
  if v
67
80
  Thread.current[key] = v
@@ -82,7 +95,7 @@ bundle exec rake
82
95
 
83
96
  ## Refs
84
97
 
85
- * <https://github.com/currencybot/open-exchange-rates>
98
+ * <https://github.com/josscrowcroft/open-exchange-rates>
86
99
  * <https://github.com/RubyMoney/money>
87
100
  * <https://github.com/RubyMoney/eu_central_bank>
88
101
  * <https://github.com/RubyMoney/google_currency>
@@ -102,5 +115,5 @@ Copyright © 2011-2016 Laurent Arnoud <laurent@spkdev.net>
102
115
  [![Version](https://img.shields.io/gem/v/money-open-exchange-rates.svg)](https://rubygems.org/gems/money-open-exchange-rates)
103
116
  [![Documentation](https://img.shields.io/badge/doc-rubydoc-blue.svg)](http://www.rubydoc.info/gems/money-open-exchange-rates)
104
117
  [![License](https://img.shields.io/badge/license-MIT-blue.svg)](http://opensource.org/licenses/MIT "MIT")
105
- [![Code Climate](http://img.shields.io/codeclimate/github/spk/money-open-exchange-rates.svg)](https://codeclimate.com/github/spk/money-open-exchange-rates)
106
- [![Inline docs](http://inch-ci.org/github/spk/money-open-exchange-rates.svg?branch=master)](http://inch-ci.org/github/spk/money-open-exchange-rates)
118
+ [![Code Climate](https://img.shields.io/codeclimate/github/spk/money-open-exchange-rates.svg)](https://codeclimate.com/github/spk/money-open-exchange-rates)
119
+ [![Inline docs](https://inch-ci.org/github/spk/money-open-exchange-rates.svg?branch=master)](http://inch-ci.org/github/spk/money-open-exchange-rates)
@@ -34,26 +34,57 @@ class Money
34
34
 
35
35
  # use https to fetch rates from Open Exchange Rates
36
36
  # disabled by default to support free-tier users
37
+ #
38
+ # @example
39
+ # oxr.secure_connection = true
40
+ #
41
+ # @param [Boolean] true for https, false for http
42
+ # @return [Boolean] true for https, false for http
37
43
  attr_accessor :secure_connection
38
44
 
39
45
  # As of the end of August 2012 all requests to the Open Exchange Rates
40
46
  # API must have a valid app_id
47
+ # see https://docs.openexchangerates.org/docs/authentication
48
+ #
49
+ # @example
50
+ # oxr.app_id = 'YOUR_APP_APP_ID'
51
+ #
52
+ # @param [String] token to access OXR API
53
+ # @return [String] token to access OXR API
41
54
  attr_accessor :app_id
42
55
 
43
- # Cache accessor, can be a String or a Proc
56
+ # Cache accessor
57
+ #
58
+ # @example
59
+ # oxr.cache = 'path/to/file/cache.json'
60
+ #
61
+ # @param [String,Proc] for a String a filepath
62
+ # @return [String,Proc] for a String a filepath
44
63
  attr_accessor :cache
45
64
 
46
65
  # Date for historical api
47
- # see https://openexchangerates.org/documentation#historical-data
66
+ # see https://docs.openexchangerates.org/docs/historical-json
67
+ #
68
+ # @example
69
+ # oxr.date = '2015-01-01'
70
+ #
71
+ # @param [String] The requested date in YYYY-MM-DD format
72
+ # @return [String] The requested date in YYYY-MM-DD format
48
73
  attr_accessor :date
49
74
 
50
75
  # Rates expiration Time
76
+ #
77
+ # @return [Time] expiration time
51
78
  attr_reader :rates_expiration
52
79
 
53
80
  # Parsed OpenExchangeRates result as Hash
81
+ #
82
+ # @return [Hash] All rates as Hash
54
83
  attr_reader :oer_rates
55
84
 
56
85
  # Seconds after than the current rates are automatically expired
86
+ #
87
+ # @return [Integer] Setted time to live in seconds
57
88
  attr_reader :ttl_in_seconds
58
89
 
59
90
  # Set the seconds after than the current rates are automatically expired
@@ -76,7 +107,7 @@ class Money
76
107
  # for the free plan users.
77
108
  #
78
109
  # @example
79
- # source = 'USD'
110
+ # oxr.source = 'USD'
80
111
  #
81
112
  # @param value [String] Currency code, ISO 3166-1 alpha-3
82
113
  #
@@ -88,12 +119,14 @@ class Money
88
119
  end
89
120
 
90
121
  # Get the base currency for all rates. By default, USD is used.
122
+ #
91
123
  # @return [String] base currency
92
124
  def source
93
125
  @source ||= OE_SOURCE
94
126
  end
95
127
 
96
128
  # Update all rates from openexchangerates JSON
129
+ #
97
130
  # @return [Array] Array of exchange rates
98
131
  def update_rates
99
132
  exchange_rates.each do |exchange_rate|
@@ -121,6 +154,7 @@ class Money
121
154
  alias super_get_rate get_rate
122
155
 
123
156
  # Override Money `get_rate` method for caching
157
+ #
124
158
  # @param [String] from_currency Currency ISO code. ex. 'USD'
125
159
  # @param [String] to_currency Currency ISO code. ex. 'CAD'
126
160
  #
@@ -133,6 +167,8 @@ class Money
133
167
  end
134
168
 
135
169
  # Expire rates when expired
170
+ #
171
+ # @return [NilClass, Time] nil if not expired or new expiration time
136
172
  def expire_rates
137
173
  return unless ttl_in_seconds
138
174
  return if rates_expiration > Time.now
@@ -142,6 +178,7 @@ class Money
142
178
 
143
179
  # Source url of openexchangerates
144
180
  # defined with app_id and secure_connection
181
+ #
145
182
  # @return [String] URL
146
183
  def source_url
147
184
  if source == OE_SOURCE
@@ -154,6 +191,7 @@ class Money
154
191
  protected
155
192
 
156
193
  # Latest url if no date given
194
+ #
157
195
  # @return [String] URL
158
196
  def oer_url
159
197
  if date
@@ -164,6 +202,7 @@ class Money
164
202
  end
165
203
 
166
204
  # Historical url generated from `date` attr_accessor
205
+ #
167
206
  # @return [String] URL
168
207
  def historical_url
169
208
  url = OER_HISTORICAL_URL
@@ -172,6 +211,7 @@ class Money
172
211
  end
173
212
 
174
213
  # Latest url
214
+ #
175
215
  # @return [String] URL
176
216
  def latest_url
177
217
  return SECURE_OER_URL if secure_connection
@@ -182,7 +222,7 @@ class Money
182
222
  # for the cache, or write to the cache file.
183
223
  #
184
224
  # @example
185
- # store_in_cache("{\"rates\": {\"AED\": 3.67304}}")
225
+ # oxr.store_in_cache("{\"rates\": {\"AED\": 3.67304}}")
186
226
  #
187
227
  # @param text [String] String to cache
188
228
  # @return [String,Integer]
@@ -197,6 +237,8 @@ class Money
197
237
  end
198
238
 
199
239
  # Read from cache when exist
240
+ #
241
+ # @return [String] Raw string from file or cache proc
200
242
  def read_from_cache
201
243
  if cache.is_a?(Proc)
202
244
  cache.call(nil)
@@ -206,6 +248,7 @@ class Money
206
248
  end
207
249
 
208
250
  # Read from url
251
+ #
209
252
  # @return [String] JSON content
210
253
  def read_from_url
211
254
  raise NoAppId if app_id.nil? || app_id.empty?
@@ -215,7 +258,7 @@ class Money
215
258
  # Check validity of rates response only for store in cache
216
259
  #
217
260
  # @example
218
- # valid_rates?("{\"rates\": {\"AED\": 3.67304}}")
261
+ # oxr.valid_rates?("{\"rates\": {\"AED\": 3.67304}}")
219
262
  #
220
263
  # @param [String] text is JSON content
221
264
  # @return [Boolean] valid or not
@@ -227,6 +270,7 @@ class Money
227
270
  end
228
271
 
229
272
  # Get expire rates, first from cache and then from url
273
+ #
230
274
  # @return [Hash] key is country code (ISO 3166-1 alpha-3) value Float
231
275
  def exchange_rates
232
276
  doc = JSON.parse(read_from_cache || read_from_url)
@@ -234,12 +278,14 @@ class Money
234
278
  end
235
279
 
236
280
  # Refresh expiration from now
237
- # return [Time] new expiration time
281
+ #
282
+ # @return [Time] new expiration time
238
283
  def refresh_rates_expiration
239
284
  @rates_expiration = Time.now + ttl_in_seconds
240
285
  end
241
286
 
242
287
  # Get rate or calculate it as inverse rate
288
+ #
243
289
  # @param [String] from_currency Currency ISO code. ex. 'USD'
244
290
  # @param [String] to_currency Currency ISO code. ex. 'CAD'
245
291
  #
@@ -258,6 +304,7 @@ class Money
258
304
  end
259
305
 
260
306
  # Tries to calculate a pair rate using base currency rate
307
+ #
261
308
  # @param [String] from_currency Currency ISO code. ex. 'USD'
262
309
  # @param [String] to_currency Currency ISO code. ex. 'CAD'
263
310
  #
@@ -2,5 +2,5 @@
2
2
 
3
3
  # Module for version constant
4
4
  module OpenExchangeRatesBank
5
- VERSION = '0.6.1'.freeze
5
+ VERSION = '0.7.0'.freeze
6
6
  end
@@ -1,2 +1,2 @@
1
- gem 'money'
2
- gem 'money-open-exchange-rates'
1
+ gem 'money', '~> 6.7'
2
+ gem 'money-open-exchange-rates', path: '../../'
@@ -1,22 +1,26 @@
1
+ PATH
2
+ remote: ../../
3
+ specs:
4
+ money-open-exchange-rates (0.6.1)
5
+ monetize (~> 1.4)
6
+ money (~> 6.7)
7
+
1
8
  GEM
2
9
  specs:
3
10
  i18n (0.7.0)
4
- json (1.8.3)
5
- monetize (1.3.1)
6
- money (~> 6.6)
7
- money (6.6.1)
11
+ monetize (1.4.0)
12
+ money (~> 6.7)
13
+ money (6.7.1)
8
14
  i18n (>= 0.6.4, <= 0.7.0)
9
- money-open-exchange-rates (0.4.0)
10
- json (~> 1.8)
11
- monetize (~> 1.3)
12
- money (~> 6.6)
15
+ sixarm_ruby_unaccent (>= 1.1.1, < 2)
16
+ sixarm_ruby_unaccent (1.1.1)
13
17
 
14
18
  PLATFORMS
15
19
  ruby
16
20
 
17
21
  DEPENDENCIES
18
- money
19
- money-open-exchange-rates
22
+ money (~> 6.7)
23
+ money-open-exchange-rates!
20
24
 
21
25
  BUNDLED WITH
22
- 1.10.6
26
+ 1.13.1
@@ -0,0 +1,34 @@
1
+ require 'json'
2
+ require 'money/bank/open_exchange_rates_bank'
3
+
4
+ ERROR_MSG = 'Integration test failed!'.freeze
5
+ cache_path = '/tmp/latest.json'
6
+ to_currency = 'CAD'
7
+ app_id = ENV['OXR_APP_ID']
8
+
9
+ if app_id.nil? || app_id.empty?
10
+ puts 'OXR_APP_ID env var not set skipping integration tests'
11
+ exit 0
12
+ end
13
+
14
+ begin
15
+ puts 'OXR version', Money::Bank::OpenExchangeRatesBank::VERSION
16
+
17
+ oxr = Money::Bank::OpenExchangeRatesBank.new
18
+ oxr.cache = cache_path
19
+ oxr.app_id = app_id
20
+ oxr.update_rates
21
+ oxr.save_rates
22
+
23
+ Money.default_bank = oxr
24
+
25
+ cad_rate = Money.default_bank.get_rate('USD', to_currency)
26
+
27
+ json_to_currency = JSON.parse(File.read(cache_path))['rates'][to_currency]
28
+ puts 'JSON to_currency', json_to_currency
29
+ puts 'Money to_currency', cad_rate
30
+ # rubocop:disable Style/AndOr
31
+ json_to_currency == cad_rate or raise ERROR_MSG
32
+ rescue
33
+ raise ERROR_MSG
34
+ end
@@ -176,24 +176,21 @@ describe Money::Bank::OpenExchangeRatesBank do
176
176
  subject.secure_connection = nil
177
177
  subject.source_url.must_equal historical_url
178
178
  subject.source_url.must_include 'http://'
179
- subject.source_url.must_include 'api'
180
- subject.source_url.must_include 'historical'
179
+ subject.source_url.must_include "/api/historical/#{subject.date}.json"
181
180
  end
182
181
 
183
182
  it 'should use the non-secure http url if secure_connection is false' do
184
183
  subject.secure_connection = false
185
184
  subject.source_url.must_equal historical_url
186
185
  subject.source_url.must_include 'http://'
187
- subject.source_url.must_include 'api'
188
- subject.source_url.must_include 'historical'
186
+ subject.source_url.must_include "/api/historical/#{subject.date}.json"
189
187
  end
190
188
 
191
189
  it 'should use the secure https url if secure_connection is true' do
192
190
  subject.secure_connection = true
193
191
  subject.source_url.must_equal historical_secure_url
194
192
  subject.source_url.must_include 'https://'
195
- subject.source_url.must_include 'api'
196
- subject.source_url.must_include 'historical'
193
+ subject.source_url.must_include "/api/historical/#{subject.date}.json"
197
194
  end
198
195
  end
199
196
 
@@ -210,24 +207,21 @@ describe Money::Bank::OpenExchangeRatesBank do
210
207
  subject.secure_connection = nil
211
208
  subject.source_url.must_equal source_url
212
209
  subject.source_url.must_include 'http://'
213
- subject.source_url.must_include 'api'
214
- subject.source_url.must_include 'latest'
210
+ subject.source_url.must_include '/api/latest.json'
215
211
  end
216
212
 
217
213
  it 'should use the non-secure http url if secure_connection is false' do
218
214
  subject.secure_connection = false
219
215
  subject.source_url.must_equal source_url
220
216
  subject.source_url.must_include 'http://'
221
- subject.source_url.must_include 'api'
222
- subject.source_url.must_include 'latest'
217
+ subject.source_url.must_include '/api/latest.json'
223
218
  end
224
219
 
225
220
  it 'should use the secure https url if secure_connection is true' do
226
221
  subject.secure_connection = true
227
222
  subject.source_url.must_equal source_secure_url
228
223
  subject.source_url.must_include 'https://'
229
- subject.source_url.must_include 'api'
230
- subject.source_url.must_include 'latest'
224
+ subject.source_url.must_include '/api/latest.json'
231
225
  end
232
226
  end
233
227
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: money-open-exchange-rates
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.6.1
4
+ version: 0.7.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Laurent Arnoud
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-09-21 00:00:00.000000000 Z
11
+ date: 2016-10-23 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: money
@@ -38,20 +38,6 @@ dependencies:
38
38
  - - "~>"
39
39
  - !ruby/object:Gem::Version
40
40
  version: '1.4'
41
- - !ruby/object:Gem::Dependency
42
- name: json
43
- requirement: !ruby/object:Gem::Requirement
44
- requirements:
45
- - - "~>"
46
- - !ruby/object:Gem::Version
47
- version: '1.8'
48
- type: :runtime
49
- prerelease: false
50
- version_requirements: !ruby/object:Gem::Requirement
51
- requirements:
52
- - - "~>"
53
- - !ruby/object:Gem::Version
54
- version: '1.8'
55
41
  - !ruby/object:Gem::Dependency
56
42
  name: rake
57
43
  requirement: !ruby/object:Gem::Requirement
@@ -154,6 +140,7 @@ files:
154
140
  - test/data/latest.json
155
141
  - test/integration/Gemfile
156
142
  - test/integration/Gemfile.lock
143
+ - test/integration/api.rb
157
144
  - test/open_exchange_rates_bank_test.rb
158
145
  - test/test_helper.rb
159
146
  homepage: http://github.com/spk/money-open-exchange-rates