money-currencylayer-bank 0.5.2 → 0.5.3
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 +4 -4
- data/LICENSE +1 -1
- data/README.md +17 -0
- data/lib/money/bank/currencylayer_bank.rb +41 -47
- data/test/currencylayer_bank_test.rb +9 -7
- metadata +14 -14
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 2954dfb52185c910cfed6a3f3b1def920c80c847
|
4
|
+
data.tar.gz: 408ba3963b1c258ce322e69237c951f34988ba1e
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: f1696528b4ac735817a8fc476b1b5a3e87512ce4e167baf36601a794ef6de6a521c0496a05f943e0ca9f643ab2f3a1361f4e5a663d32a4a684df27f1894a62fa
|
7
|
+
data.tar.gz: a58368adeb7bbc501e5ef1e13f55e9deff90084b7b45f9021c1f49b9bb4241b4acfee44303e28f7002efdfc56c230547b98b149c59b3c4bcf34633a597c2e0cc
|
data/LICENSE
CHANGED
data/README.md
CHANGED
@@ -1,6 +1,7 @@
|
|
1
1
|
# Money Currencylayer Bank
|
2
2
|
|
3
3
|
[](https://rubygems.org/gems/money-currencylayer-bank)
|
4
|
+
[](https://rubygems.org/gems/money-currencylayer-bank)
|
4
5
|
[](https://travis-ci.org/phlegx/money-currencylayer-bank)
|
5
6
|
[](https://codeclimate.com/github/phlegx/money-currencylayer-bank)
|
6
7
|
[](http://inch-ci.org/github/phlegx/money-currencylayer-bank)
|
@@ -40,6 +41,22 @@ See more about Currencylayer product plans on https://currencylayer.com/product.
|
|
40
41
|
* supports caching currency rates
|
41
42
|
* calculates every pair rate calculating inverse rate or using base currency rate
|
42
43
|
|
44
|
+
## Installation
|
45
|
+
|
46
|
+
Add this line to your application's Gemfile:
|
47
|
+
|
48
|
+
```ruby
|
49
|
+
gem 'money-currencylayer-bank'
|
50
|
+
```
|
51
|
+
|
52
|
+
And then execute:
|
53
|
+
|
54
|
+
$ bundle
|
55
|
+
|
56
|
+
Or install it yourself as:
|
57
|
+
|
58
|
+
$ gem install money-currencylayer-bank
|
59
|
+
|
43
60
|
## Usage
|
44
61
|
|
45
62
|
~~~ ruby
|
@@ -26,22 +26,39 @@ class Money
|
|
26
26
|
# Use https to fetch rates from CurrencylayerBank
|
27
27
|
# CurrencylayerBank only allows http as connection
|
28
28
|
# for the free plan users.
|
29
|
+
#
|
30
|
+
# @param value [Boolean] true for secure connection
|
31
|
+
#
|
32
|
+
# @return [Boolean] chosen secure connection
|
29
33
|
attr_accessor :secure_connection
|
30
34
|
|
31
35
|
# API must have a valid access_key
|
36
|
+
#
|
37
|
+
# @param value [String] API access key
|
38
|
+
#
|
39
|
+
# @return [String] chosen API access key
|
32
40
|
attr_accessor :access_key
|
33
41
|
|
34
42
|
# Cache accessor, can be a String or a Proc
|
43
|
+
#
|
44
|
+
# @param value [String,Pathname,Proc] cache system
|
45
|
+
#
|
46
|
+
# @return [String,Pathname,Proc] chosen cache system
|
35
47
|
attr_accessor :cache
|
36
48
|
|
37
|
-
# Rates expiration Time
|
38
|
-
attr_reader :rates_expiration
|
39
|
-
|
40
49
|
# Parsed CurrencylayerBank result as Hash
|
41
50
|
attr_reader :rates
|
42
51
|
|
43
|
-
#
|
44
|
-
|
52
|
+
# Set the seconds after than the current rates are automatically expired
|
53
|
+
# by default, they never expire.
|
54
|
+
#
|
55
|
+
# @example
|
56
|
+
# ttl_in_seconds = 86400 # will expire the rates in one day
|
57
|
+
#
|
58
|
+
# @param value [Integer] time to live in seconds
|
59
|
+
#
|
60
|
+
# @return [Integer] chosen time to live in seconds
|
61
|
+
attr_writer :ttl_in_seconds
|
45
62
|
|
46
63
|
# Set the base currency for all rates. By default, USD is used.
|
47
64
|
# CurrencylayerBank only allows USD as base currency
|
@@ -63,19 +80,11 @@ class Money
|
|
63
80
|
@source ||= CL_SOURCE
|
64
81
|
end
|
65
82
|
|
66
|
-
#
|
83
|
+
# Get the seconds after than the current rates are automatically expired
|
67
84
|
# by default, they never expire.
|
68
|
-
#
|
69
|
-
# @example
|
70
|
-
# ttl_in_seconds = 86400 # will expire the rates in one day
|
71
|
-
#
|
72
|
-
# @param value [Integer] time to live in seconds
|
73
|
-
#
|
74
85
|
# @return [Integer] chosen time to live in seconds
|
75
|
-
def ttl_in_seconds
|
76
|
-
@ttl_in_seconds
|
77
|
-
refresh_rates_expiration!
|
78
|
-
@ttl_in_seconds
|
86
|
+
def ttl_in_seconds
|
87
|
+
@ttl_in_seconds ||= 0
|
79
88
|
end
|
80
89
|
|
81
90
|
# Update all rates from CurrencylayerBank JSON
|
@@ -140,7 +149,7 @@ class Money
|
|
140
149
|
# Check if rates are expired
|
141
150
|
# @return [Boolean] true if rates are expired
|
142
151
|
def expired?
|
143
|
-
|
152
|
+
Time.now > rates_expiration
|
144
153
|
end
|
145
154
|
|
146
155
|
# Source url of CurrencylayerBank
|
@@ -153,27 +162,21 @@ class Money
|
|
153
162
|
"#{cl_url}?source=#{source}&access_key=#{access_key}"
|
154
163
|
end
|
155
164
|
|
165
|
+
# Get rates expiration time based on ttl
|
166
|
+
# @return [Time] rates expiration time
|
167
|
+
def rates_expiration
|
168
|
+
rates_timestamp + ttl_in_seconds
|
169
|
+
end
|
170
|
+
|
156
171
|
# Get the timestamp of rates
|
157
172
|
# @return [Time] time object or nil
|
158
173
|
def rates_timestamp
|
159
|
-
|
174
|
+
raw = raw_rates_careful
|
175
|
+
raw.key?('timestamp') ? Time.at(raw['timestamp']) : Time.at(0)
|
160
176
|
end
|
161
177
|
|
162
178
|
protected
|
163
179
|
|
164
|
-
# Sets the rates timestamp from parsed JSON content
|
165
|
-
#
|
166
|
-
# @example
|
167
|
-
# set_rates_timestamp("{\"timestamp\": 1441049528,
|
168
|
-
# \"quotes\": {\"USDAED\": 3.67304}}")
|
169
|
-
#
|
170
|
-
# @param raw_rates [String] parsed JSON content, default is nil
|
171
|
-
# @return [Time] time object with rates timestamp
|
172
|
-
def init_rates_timestamp(raw_rates = nil)
|
173
|
-
raw = raw_rates || raw_rates_careful
|
174
|
-
@rates_timestamp = Time.at(raw['timestamp']) if raw.key?('timestamp')
|
175
|
-
end
|
176
|
-
|
177
180
|
# Store the provided text data by calling the proc method provided
|
178
181
|
# for the cache, or write to the cache file.
|
179
182
|
#
|
@@ -216,10 +219,7 @@ class Money
|
|
216
219
|
# @return [String] unparsed JSON content
|
217
220
|
def read_from_url
|
218
221
|
text = open_url
|
219
|
-
if valid_rates?(text)
|
220
|
-
refresh_rates_expiration!
|
221
|
-
store_in_cache(text) if cache
|
222
|
-
end
|
222
|
+
store_in_cache(text) if valid_rates?(text) && cache
|
223
223
|
text
|
224
224
|
end
|
225
225
|
|
@@ -227,6 +227,8 @@ class Money
|
|
227
227
|
# @return [String] unparsed JSON content
|
228
228
|
def open_url
|
229
229
|
open(source_url).read
|
230
|
+
rescue OpenURI::HTTPError
|
231
|
+
''
|
230
232
|
end
|
231
233
|
|
232
234
|
# Check validity of rates response only for store in cache
|
@@ -261,26 +263,18 @@ class Money
|
|
261
263
|
|
262
264
|
# Get raw exchange rates from cache and then from url
|
263
265
|
# @return [String] JSON content
|
264
|
-
def raw_rates_careful
|
266
|
+
def raw_rates_careful(rescue_straight = true)
|
265
267
|
JSON.parse(read_from_cache.to_s)
|
266
268
|
rescue JSON::ParserError
|
267
|
-
raw_rates_straight
|
269
|
+
rescue_straight ? raw_rates_straight : { 'quotes' => {} }
|
268
270
|
end
|
269
271
|
|
270
272
|
# Get raw exchange rates from url
|
271
273
|
# @return [String] JSON content
|
272
274
|
def raw_rates_straight
|
273
|
-
|
274
|
-
init_rates_timestamp(raw_rates)
|
275
|
-
raw_rates
|
275
|
+
JSON.parse(read_from_url)
|
276
276
|
rescue JSON::ParserError
|
277
|
-
|
278
|
-
end
|
279
|
-
|
280
|
-
# Refresh expiration from now
|
281
|
-
# return [Time] new expiration time
|
282
|
-
def refresh_rates_expiration!
|
283
|
-
@rates_expiration = Time.now + ttl_in_seconds unless ttl_in_seconds.nil?
|
277
|
+
raw_rates_careful(false)
|
284
278
|
end
|
285
279
|
end
|
286
280
|
end
|
@@ -230,7 +230,7 @@ describe Money::Bank::CurrencylayerBank do
|
|
230
230
|
subject.access_key = TEST_ACCESS_KEY
|
231
231
|
subject.ttl_in_seconds = 1000
|
232
232
|
@old_usd_eur_rate = 0.655
|
233
|
-
# see test/
|
233
|
+
# see test/live.json +54
|
234
234
|
@new_usd_eur_rate = 0.886584
|
235
235
|
subject.add_rate('USD', 'EUR', @old_usd_eur_rate)
|
236
236
|
subject.cache = temp_cache_path
|
@@ -243,16 +243,18 @@ describe Money::Bank::CurrencylayerBank do
|
|
243
243
|
|
244
244
|
describe 'when the ttl has expired' do
|
245
245
|
it 'should update the rates' do
|
246
|
-
subject.
|
247
|
-
|
246
|
+
Timecop.freeze(subject.rates_timestamp + 1000) do
|
247
|
+
subject.get_rate('USD', 'EUR').must_equal @old_usd_eur_rate
|
248
|
+
end
|
249
|
+
Timecop.freeze(subject.rates_timestamp + 1001) do
|
248
250
|
subject.get_rate('USD', 'EUR').wont_equal @old_usd_eur_rate
|
249
251
|
subject.get_rate('USD', 'EUR').must_equal @new_usd_eur_rate
|
250
252
|
end
|
251
253
|
end
|
252
254
|
|
253
255
|
it 'updates the next expiration time' do
|
254
|
-
Timecop.freeze(
|
255
|
-
exp_time =
|
256
|
+
Timecop.freeze(subject.rates_timestamp + 1001) do
|
257
|
+
exp_time = subject.rates_timestamp + 1000
|
256
258
|
subject.expire_rates!
|
257
259
|
subject.rates_expiration.must_equal exp_time
|
258
260
|
end
|
@@ -280,10 +282,10 @@ describe Money::Bank::CurrencylayerBank do
|
|
280
282
|
File.delete(temp_cache_path) if File.exist?(temp_cache_path)
|
281
283
|
end
|
282
284
|
|
283
|
-
it 'should return
|
285
|
+
it 'should return 1970-01-01 datetime if no rates' do
|
284
286
|
stub(subject).open_url { '' }
|
285
287
|
subject.update_rates
|
286
|
-
subject.rates_timestamp.
|
288
|
+
subject.rates_timestamp.must_equal Time.at(0)
|
287
289
|
end
|
288
290
|
|
289
291
|
it 'should return a Time object' do
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: money-currencylayer-bank
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.5.
|
4
|
+
version: 0.5.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Egon Zemmer
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2016-
|
11
|
+
date: 2016-06-17 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: money
|
@@ -98,30 +98,30 @@ dependencies:
|
|
98
98
|
name: rake
|
99
99
|
requirement: !ruby/object:Gem::Requirement
|
100
100
|
requirements:
|
101
|
-
- - "
|
101
|
+
- - "~>"
|
102
102
|
- !ruby/object:Gem::Version
|
103
|
-
version: '
|
103
|
+
version: '10.4'
|
104
104
|
type: :development
|
105
105
|
prerelease: false
|
106
106
|
version_requirements: !ruby/object:Gem::Requirement
|
107
107
|
requirements:
|
108
|
-
- - "
|
108
|
+
- - "~>"
|
109
109
|
- !ruby/object:Gem::Version
|
110
|
-
version: '
|
110
|
+
version: '10.4'
|
111
111
|
- !ruby/object:Gem::Dependency
|
112
112
|
name: timecop
|
113
113
|
requirement: !ruby/object:Gem::Requirement
|
114
114
|
requirements:
|
115
|
-
- - "
|
115
|
+
- - "~>"
|
116
116
|
- !ruby/object:Gem::Version
|
117
|
-
version:
|
117
|
+
version: 0.8.1
|
118
118
|
type: :development
|
119
119
|
prerelease: false
|
120
120
|
version_requirements: !ruby/object:Gem::Requirement
|
121
121
|
requirements:
|
122
|
-
- - "
|
122
|
+
- - "~>"
|
123
123
|
- !ruby/object:Gem::Version
|
124
|
-
version:
|
124
|
+
version: 0.8.1
|
125
125
|
- !ruby/object:Gem::Dependency
|
126
126
|
name: rubocop
|
127
127
|
requirement: !ruby/object:Gem::Requirement
|
@@ -140,16 +140,16 @@ dependencies:
|
|
140
140
|
name: inch
|
141
141
|
requirement: !ruby/object:Gem::Requirement
|
142
142
|
requirements:
|
143
|
-
- - "
|
143
|
+
- - "~>"
|
144
144
|
- !ruby/object:Gem::Version
|
145
|
-
version:
|
145
|
+
version: 0.7.1
|
146
146
|
type: :development
|
147
147
|
prerelease: false
|
148
148
|
version_requirements: !ruby/object:Gem::Requirement
|
149
149
|
requirements:
|
150
|
-
- - "
|
150
|
+
- - "~>"
|
151
151
|
- !ruby/object:Gem::Version
|
152
|
-
version:
|
152
|
+
version: 0.7.1
|
153
153
|
description: A gem that calculates the exchange rate using published rates from currencylayer.com.
|
154
154
|
Compatible with the money gem.
|
155
155
|
email: office@phlegx.com
|