money-currencylayer-bank 0.5.0 → 0.5.2

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: f30609c06313fb218edbad89627da0d1bb7bc66d
4
- data.tar.gz: 8a1ffe4d046a32e3942b67cf89977ef3d8c2c0f5
3
+ metadata.gz: 34fe15fb5a511798ab01fd6bb0d230505211d985
4
+ data.tar.gz: 4f9e805357eafdc38f1fca9d4d53164c8f002228
5
5
  SHA512:
6
- metadata.gz: 546c3541ae100259af247726d04e17fdb8e86a011e19d3f8d378f8eef8d373f11f3479a703f68876d38a82bfdbf8a26784e25009d7b51178dfea0b07b14e8418
7
- data.tar.gz: 11ccf7440dbf5d076578cce0e6cc591c70217ec5481c97caa2f1a32bc4ab03cdffe35fcbbf7a8193151f2687ca512c83e683b44deee1eb0200b36b53be6d4f66
6
+ metadata.gz: c2ed1a8ec702c34866295ad35622aa56b469f9a8f8cd13cae5e9e86a43d869b2304f1654639f142b6d15639ed4a17c02a1ff12e08ddefbefba92aab19dfc70b7
7
+ data.tar.gz: e8f8317e5932cadb8bee5669c978f53604af9fab0ad300558ac6a69b1ce076e2533214148976bd0b90e644842a8bb21d6a5de9a3c4c61086f606c2c1a85a9b67
data/Gemfile CHANGED
@@ -1,3 +1,9 @@
1
1
  source 'https://rubygems.org'
2
2
 
3
3
  gemspec
4
+
5
+ group :test, :development do
6
+ platforms :ruby_19, :jruby_19 do
7
+ gem 'tins', '~> 1.6.0'
8
+ end
9
+ end
data/README.md CHANGED
@@ -3,7 +3,8 @@
3
3
  [![Gem Version](https://badge.fury.io/rb/money-currencylayer-bank.svg)](https://rubygems.org/gems/money-currencylayer-bank)
4
4
  [![Build Status](https://secure.travis-ci.org/phlegx/money-currencylayer-bank.svg?branch=master)](https://travis-ci.org/phlegx/money-currencylayer-bank)
5
5
  [![Code Climate](http://img.shields.io/codeclimate/github/phlegx/money-currencylayer-bank.svg)](https://codeclimate.com/github/phlegx/money-currencylayer-bank)
6
- [![Inline docs](http://inch-ci.org/github/phlegx/money-currencylayer-bank.svg?branch=master)](http://inch-ci.org/github/phlegx/money-currencylayer-bank)
6
+ [![Inline Docs](http://inch-ci.org/github/phlegx/money-currencylayer-bank.svg?branch=master)](http://inch-ci.org/github/phlegx/money-currencylayer-bank)
7
+ [![Dependency Status](https://gemnasium.com/phlegx/money-currencylayer-bank.svg)](https://gemnasium.com/phlegx/money-currencylayer-bank)
7
8
  [![License](https://img.shields.io/github/license/phlegx/money-currencylayer-bank.svg)](http://opensource.org/licenses/MIT)
8
9
 
9
10
  A gem that calculates the exchange rate using published rates from
@@ -47,11 +48,11 @@ require 'money/bank/currencylayer_bank'
47
48
  mclb = Money::Bank::CurrencylayerBank.new
48
49
  mclb.access_key = 'your access_key from https://currencylayer.com/product'
49
50
 
50
- # Update rates
51
+ # Update rates (get new rates from remote if expired or access rates from cache)
51
52
  mclb.update_rates
52
53
 
53
- # Store in cache
54
- mclb.save_rates
54
+ # Force update rates from remote and store in cache
55
+ # mclb.update_rates(true)
55
56
 
56
57
  # (optional)
57
58
  # Set the base currency for all rates. By default, USD is used.
@@ -68,7 +69,7 @@ mclb.ttl_in_seconds = 86400
68
69
  # CurrencylayerBank only allows http as connection for the free plan users.
69
70
  mclb.secure_connection = true
70
71
 
71
- # Define cache
72
+ # Define cache (string or pathname)
72
73
  mclb.cache = 'path/to/file/cache'
73
74
 
74
75
  # Set money default bank to currencylayer bank
@@ -17,11 +17,11 @@ class Money
17
17
  # rubocop:disable Metrics/ClassLength
18
18
  class CurrencylayerBank < Money::Bank::VariableExchange
19
19
  # CurrencylayerBank url
20
- CL_URL = 'http://apilayer.net/api/live'
20
+ CL_URL = 'http://apilayer.net/api/live'.freeze
21
21
  # CurrencylayerBank secure url
22
- CL_SECURE_URL = CL_URL.gsub('http:', 'https:')
22
+ CL_SECURE_URL = CL_URL.gsub('http:', 'https:').freeze
23
23
  # Default base currency
24
- CL_SOURCE = 'USD'
24
+ CL_SOURCE = 'USD'.freeze
25
25
 
26
26
  # Use https to fetch rates from CurrencylayerBank
27
27
  # CurrencylayerBank only allows http as connection
@@ -74,7 +74,7 @@ class Money
74
74
  # @return [Integer] chosen time to live in seconds
75
75
  def ttl_in_seconds=(value)
76
76
  @ttl_in_seconds = value
77
- refresh_rates_expiration! if ttl_in_seconds
77
+ refresh_rates_expiration!
78
78
  @ttl_in_seconds
79
79
  end
80
80
 
@@ -140,14 +140,14 @@ class Money
140
140
  # Check if rates are expired
141
141
  # @return [Boolean] true if rates are expired
142
142
  def expired?
143
- !ttl_in_seconds.nil? && rates_expiration <= Time.now
143
+ rates_expiration ? rates_expiration <= Time.now : true
144
144
  end
145
145
 
146
146
  # Source url of CurrencylayerBank
147
147
  # defined with access_key and secure_connection
148
148
  # @return [String] the remote API url
149
149
  def source_url
150
- fail NoAccessKey if access_key.nil? || access_key.empty?
150
+ raise NoAccessKey if access_key.nil? || access_key.empty?
151
151
  cl_url = CL_URL
152
152
  cl_url = CL_SECURE_URL if secure_connection
153
153
  "#{cl_url}?source=#{source}&access_key=#{access_key}"
@@ -156,30 +156,42 @@ class Money
156
156
  # Get the timestamp of rates
157
157
  # @return [Time] time object or nil
158
158
  def rates_timestamp
159
- parsed = raw_rates_careful
160
- parsed.key?('timestamp') ? Time.at(parsed['timestamp']) : nil
159
+ @rates_timestamp ||= init_rates_timestamp
161
160
  end
162
161
 
163
162
  protected
164
163
 
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
+
165
177
  # Store the provided text data by calling the proc method provided
166
178
  # for the cache, or write to the cache file.
167
179
  #
168
180
  # @example
169
181
  # store_in_cache("{\"quotes\": {\"USDAED\": 3.67304}}")
170
182
  #
171
- # @param text [String] unparsed JSON content
183
+ # @param text [String] parsed JSON content
172
184
  # @return [String,Integer]
173
185
  def store_in_cache(text)
174
186
  if cache.is_a?(Proc)
175
187
  cache.call(text)
176
- elsif cache.is_a?(String)
188
+ elsif cache.is_a?(String) || cache.is_a?(Pathname)
177
189
  write_to_file(text)
178
190
  end
179
191
  end
180
192
 
181
193
  # Writes content to file cache
182
- # @param text [String] unparsed JSON content
194
+ # @param text [String] parsed JSON content
183
195
  # @return [String,Integer]
184
196
  def write_to_file(text)
185
197
  open(cache, 'w') do |f|
@@ -190,17 +202,18 @@ class Money
190
202
  end
191
203
 
192
204
  # Read from cache when exist
193
- # @return [Proc,String] unparsed JSON content
205
+ # @return [Proc,String] parsed JSON content
194
206
  def read_from_cache
195
207
  if cache.is_a?(Proc)
196
208
  cache.call(nil)
197
- elsif cache.is_a?(String) && File.exist?(cache)
209
+ elsif (cache.is_a?(String) || cache.is_a?(Pathname)) &&
210
+ File.exist?(cache)
198
211
  open(cache).read
199
212
  end
200
213
  end
201
214
 
202
215
  # Get remote content and store in cache
203
- # @return [String] JSON content
216
+ # @return [String] unparsed JSON content
204
217
  def read_from_url
205
218
  text = open_url
206
219
  if valid_rates?(text)
@@ -211,7 +224,7 @@ class Money
211
224
  end
212
225
 
213
226
  # Opens an url and reads the content
214
- # @return [String] JSON content
227
+ # @return [String] unparsed JSON content
215
228
  def open_url
216
229
  open(source_url).read
217
230
  end
@@ -236,14 +249,14 @@ class Money
236
249
  # exchange_rates(true)
237
250
  # exchange_rates
238
251
  #
239
- # @param [Boolean] true for straight, default is careful
252
+ # @param straight [Boolean] true for straight, default is careful
240
253
  # @return [Hash] key is country code (ISO 3166-1 alpha-3) value Float
241
254
  def exchange_rates(straight = false)
242
- if straight
243
- @rates = raw_rates_straight['quotes']
244
- else
245
- @rates = raw_rates_careful['quotes']
246
- end
255
+ @rates = if straight
256
+ raw_rates_straight['quotes']
257
+ else
258
+ raw_rates_careful['quotes']
259
+ end
247
260
  end
248
261
 
249
262
  # Get raw exchange rates from cache and then from url
@@ -257,7 +270,9 @@ class Money
257
270
  # Get raw exchange rates from url
258
271
  # @return [String] JSON content
259
272
  def raw_rates_straight
260
- JSON.parse(read_from_url)
273
+ raw_rates = JSON.parse(read_from_url)
274
+ init_rates_timestamp(raw_rates)
275
+ raw_rates
261
276
  rescue JSON::ParserError
262
277
  { 'quotes' => {} }
263
278
  end
@@ -212,25 +212,6 @@ describe Money::Bank::CurrencylayerBank do
212
212
  subject.add_rate('WTF', 'USD', 2)
213
213
  subject.exchange_with(5000.to_money('WTF'), 'USD').cents.wont_equal 0
214
214
  end
215
-
216
- # in response to #4
217
- it 'should exchange btc' do
218
- btc = {
219
- priority: 1,
220
- iso_code: 'BTC',
221
- name: 'Bitcoin',
222
- symbol: 'BTC',
223
- subunit: 'Cent',
224
- subunit_to_unit: 1000,
225
- separator: '.',
226
- delimiter: ','
227
- }
228
- Money::Currency.register(btc)
229
- rate = 13.7603
230
- subject.add_rate('USD', 'BTC', 1 / 13.7603)
231
- subject.add_rate('BTC', 'USD', rate)
232
- subject.exchange_with(100.to_money('BTC'), 'USD').cents.must_equal 137_603
233
- end
234
215
  end
235
216
 
236
217
  describe '#access_key' do
data/test/test_helper.rb CHANGED
@@ -10,6 +10,6 @@ TEST_ACCESS_KEY_PATH = File.join(File.dirname(__FILE__), 'TEST_ACCESS_KEY')
10
10
  TEST_ACCESS_KEY = ENV['TEST_ACCESS_KEY'] || File.read(TEST_ACCESS_KEY_PATH)
11
11
 
12
12
  if TEST_ACCESS_KEY.nil? || TEST_ACCESS_KEY.empty?
13
- fail "Please add a valid access key to file #{TEST_ACCESS_KEY_PATH} or to " \
14
- ' TEST_TEST_ACCESS_KEY environment'
13
+ raise "Please add a valid access key to file #{TEST_ACCESS_KEY_PATH} or to " \
14
+ ' TEST_ACCESS_KEY environment'
15
15
  end
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.0
4
+ version: 0.5.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Egon Zemmer
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-09-01 00:00:00.000000000 Z
11
+ date: 2016-02-13 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: money
@@ -16,28 +16,28 @@ dependencies:
16
16
  requirements:
17
17
  - - "~>"
18
18
  - !ruby/object:Gem::Version
19
- version: '6.6'
19
+ version: '6.7'
20
20
  type: :runtime
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
24
  - - "~>"
25
25
  - !ruby/object:Gem::Version
26
- version: '6.6'
26
+ version: '6.7'
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: monetize
29
29
  requirement: !ruby/object:Gem::Requirement
30
30
  requirements:
31
31
  - - "~>"
32
32
  - !ruby/object:Gem::Version
33
- version: '1.3'
33
+ version: '1.4'
34
34
  type: :runtime
35
35
  prerelease: false
36
36
  version_requirements: !ruby/object:Gem::Requirement
37
37
  requirements:
38
38
  - - "~>"
39
39
  - !ruby/object:Gem::Version
40
- version: '1.3'
40
+ version: '1.4'
41
41
  - !ruby/object:Gem::Dependency
42
42
  name: json
43
43
  requirement: !ruby/object:Gem::Requirement
@@ -80,20 +80,6 @@ dependencies:
80
80
  - - "~>"
81
81
  - !ruby/object:Gem::Version
82
82
  version: '0.6'
83
- - !ruby/object:Gem::Dependency
84
- name: pry
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
83
  - !ruby/object:Gem::Dependency
98
84
  name: rr
99
85
  requirement: !ruby/object:Gem::Requirement
@@ -112,28 +98,28 @@ dependencies:
112
98
  name: rake
113
99
  requirement: !ruby/object:Gem::Requirement
114
100
  requirements:
115
- - - "~>"
101
+ - - ">="
116
102
  - !ruby/object:Gem::Version
117
103
  version: '0'
118
104
  type: :development
119
105
  prerelease: false
120
106
  version_requirements: !ruby/object:Gem::Requirement
121
107
  requirements:
122
- - - "~>"
108
+ - - ">="
123
109
  - !ruby/object:Gem::Version
124
110
  version: '0'
125
111
  - !ruby/object:Gem::Dependency
126
112
  name: timecop
127
113
  requirement: !ruby/object:Gem::Requirement
128
114
  requirements:
129
- - - "~>"
115
+ - - ">="
130
116
  - !ruby/object:Gem::Version
131
117
  version: '0'
132
118
  type: :development
133
119
  prerelease: false
134
120
  version_requirements: !ruby/object:Gem::Requirement
135
121
  requirements:
136
- - - "~>"
122
+ - - ">="
137
123
  - !ruby/object:Gem::Version
138
124
  version: '0'
139
125
  - !ruby/object:Gem::Dependency
@@ -142,26 +128,26 @@ dependencies:
142
128
  requirements:
143
129
  - - "~>"
144
130
  - !ruby/object:Gem::Version
145
- version: '0'
131
+ version: 0.37.2
146
132
  type: :development
147
133
  prerelease: false
148
134
  version_requirements: !ruby/object:Gem::Requirement
149
135
  requirements:
150
136
  - - "~>"
151
137
  - !ruby/object:Gem::Version
152
- version: '0'
138
+ version: 0.37.2
153
139
  - !ruby/object:Gem::Dependency
154
140
  name: inch
155
141
  requirement: !ruby/object:Gem::Requirement
156
142
  requirements:
157
- - - "~>"
143
+ - - ">="
158
144
  - !ruby/object:Gem::Version
159
145
  version: '0'
160
146
  type: :development
161
147
  prerelease: false
162
148
  version_requirements: !ruby/object:Gem::Requirement
163
149
  requirements:
164
- - - "~>"
150
+ - - ">="
165
151
  - !ruby/object:Gem::Version
166
152
  version: '0'
167
153
  description: A gem that calculates the exchange rate using published rates from currencylayer.com.