money-currencylayer-bank 0.5.4 → 0.5.5

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: cc97450bce8ac6e02fc906a21ca0c10860428557
4
- data.tar.gz: 371c75e3794e3a7c2be6babd222845eda7cb1ac6
3
+ metadata.gz: bcc2d3fa0693e5f08c0368924439dfad9e36e744
4
+ data.tar.gz: 8d0d077b6c485208c35f49740e93054b7a5c8102
5
5
  SHA512:
6
- metadata.gz: f69464174caabd8f18da9077f579d252f77b5b751bf4032117fbc575efd04a4740f84cf1be1fdb39e1f42791dc1540d5911b168324938108a339facef6bcdce0
7
- data.tar.gz: e81c35792af291b86f106ed2c2f1efacc78ffcf035ffa01f70d4e030bb9de77de6f41983ccd0052dc61bf105cf4fde668256c1b5b87566bea6cb7e0017ec122e
6
+ metadata.gz: 85105563870f9b83c96bc6e2e0a47ea8b6c3328d0ac7d7bfc709fe7f598996cd2cbf5eda05f2e96b34dad0520d793c42d862fc71f591f4f76ea2ab96989e52f8
7
+ data.tar.gz: 60f70f3a63f6c96883e9e34bc274bc96d343f0458eaeeb59e8fb07f441041f424e1734c87ba2d7d86d49ba005d67606021c08fdf8cff08f6845ddf06226e2696
data/LICENSE CHANGED
@@ -1,6 +1,6 @@
1
1
  The MIT License (MIT)
2
2
 
3
- Copyright (c) 2016 Phlegx Systems OG
3
+ Copyright (c) 2017 Phlegx Systems OG
4
4
 
5
5
  Permission is hereby granted, free of charge, to any person obtaining a copy
6
6
  of this software and associated documentation files (the "Software"), to deal
data/README.md CHANGED
@@ -40,6 +40,7 @@ See more about Currencylayer product plans on https://currencylayer.com/product.
40
40
  * average response time < 20ms
41
41
  * supports caching currency rates
42
42
  * calculates every pair rate calculating inverse rate or using base currency rate
43
+ * supports multi threading
43
44
 
44
45
  ## Installation
45
46
 
@@ -89,7 +90,7 @@ mclb.secure_connection = true
89
90
  # Define cache (string or pathname)
90
91
  mclb.cache = 'path/to/file/cache'
91
92
 
92
- # Set money default bank to currencylayer bank
93
+ # Set money default bank to Currencylayer bank
93
94
  Money.default_bank = mclb
94
95
  ~~~
95
96
 
@@ -104,7 +105,7 @@ mclb.source
104
105
  # Expires rates if the expiration time is reached.
105
106
  mclb.expire_rates!
106
107
 
107
- # Return true if the expiration time is reached.
108
+ # Returns true if the expiration time is reached.
108
109
  mclb.expired?
109
110
 
110
111
  # Get the API source url.
@@ -112,6 +113,9 @@ mclb.source_url
112
113
 
113
114
  # Get the rates timestamp of the last API request.
114
115
  mclb.rates_timestamp
116
+
117
+ # Get the rates timestamp of loaded rates in memory.
118
+ moxb.rates_mem_timestamp
115
119
  ~~~
116
120
 
117
121
  ### How to exchange
@@ -184,6 +188,7 @@ bundle exec rake
184
188
  ## Other Implementations
185
189
 
186
190
  * Gem [currencylayer](https://github.com/askuratovsky/currencylayer)
191
+ * Gem [money-openexchangerates-bank](https://github.com/phlegx/money-openexchangerates-bank)
187
192
  * Gem [money-open-exchange-rates](https://github.com/spk/money-open-exchange-rates)
188
193
  * Gem [money-historical-bank](https://github.com/atwam/money-historical-bank)
189
194
  * Gem [eu_central_bank](https://github.com/RubyMoney/eu_central_bank)
@@ -207,4 +212,4 @@ bundle exec rake
207
212
 
208
213
  The MIT License
209
214
 
210
- Copyright (c) 2015 Phlegx Systems OG
215
+ Copyright (c) 2017 Phlegx Systems OG
@@ -13,7 +13,6 @@ class Money
13
13
  class Memory
14
14
  # Add method to reset the build in memory store
15
15
  # @param [Hash] rt Optional initial exchange rate data.
16
- #
17
16
  # @return [Object] store.
18
17
  def reset!(rt = {})
19
18
  transaction { @index = rt }
@@ -32,32 +31,29 @@ class Money
32
31
  # CurrencylayerBank base class
33
32
  class CurrencylayerBank < Money::Bank::VariableExchange
34
33
  # CurrencylayerBank url
35
- CL_URL = 'http://apilayer.net/api/live'
34
+ CL_URL = 'http://apilayer.net/api/live'.freeze
36
35
  # CurrencylayerBank secure url
37
- CL_SECURE_URL = CL_URL.gsub('http:', 'https:')
36
+ CL_SECURE_URL = CL_URL.sub('http:', 'https:')
38
37
  # Default base currency
39
- CL_SOURCE = 'USD'
38
+ CL_SOURCE = 'USD'.freeze
40
39
 
41
40
  # Use https to fetch rates from CurrencylayerBank
42
41
  # CurrencylayerBank only allows http as connection
43
42
  # for the free plan users.
44
43
  #
45
44
  # @param value [Boolean] true for secure connection
46
- #
47
45
  # @return [Boolean] chosen secure connection
48
46
  attr_accessor :secure_connection
49
47
 
50
48
  # API must have a valid access_key
51
49
  #
52
50
  # @param value [String] API access key
53
- #
54
51
  # @return [String] chosen API access key
55
52
  attr_accessor :access_key
56
53
 
57
54
  # Cache accessor, can be a String or a Proc
58
55
  #
59
56
  # @param value [String,Pathname,Proc] cache system
60
- #
61
57
  # @return [String,Pathname,Proc] chosen cache system
62
58
  attr_accessor :cache
63
59
 
@@ -75,7 +71,6 @@ class Money
75
71
  # ttl_in_seconds = 86400 # will expire the rates in one day
76
72
  #
77
73
  # @param value [Integer] time to live in seconds
78
- #
79
74
  # @return [Integer] chosen time to live in seconds
80
75
  attr_writer :ttl_in_seconds
81
76
 
@@ -87,7 +82,6 @@ class Money
87
82
  # source = 'USD'
88
83
  #
89
84
  # @param value [String] Currency code, ISO 3166-1 alpha-3
90
- #
91
85
  # @return [String] chosen base currency
92
86
  def source=(value)
93
87
  @source = Money::Currency.find(value.to_s).try(:iso_code) || CL_SOURCE
@@ -124,47 +118,23 @@ class Money
124
118
  # @param [String] from_currency Currency ISO code. ex. 'USD'
125
119
  # @param [String] to_currency Currency ISO code. ex. 'CAD'
126
120
  # @param [Numeric] rate Rate to use when exchanging currencies.
127
- #
128
121
  # @return [Numeric] rate.
129
122
  def add_rate(from_currency, to_currency, rate)
130
123
  super
131
124
  end
132
125
 
126
+ # Alias super method
127
+ alias super_get_rate get_rate
128
+
133
129
  # Override Money `get_rate` method for caching
134
130
  # @param [String] from_currency Currency ISO code. ex. 'USD'
135
131
  # @param [String] to_currency Currency ISO code. ex. 'CAD'
136
132
  # @param [Hash] opts Options hash to set special parameters.
137
- #
138
133
  # @return [Numeric] rate.
139
- def get_rate(from_currency, to_currency, opts = {}) # rubocop:disable all
134
+ def get_rate(from_currency, to_currency, opts = {})
140
135
  expire_rates!
141
- rate = super
142
- unless rate
143
- # Tries to calculate an inverse rate
144
- inverse_rate = super(to_currency, from_currency, opts)
145
- if inverse_rate
146
- rate = 1.0 / inverse_rate
147
- add_rate(from_currency, to_currency, rate)
148
- end
149
- end
150
- unless rate
151
- # Tries to calculate a pair rate using base currency rate
152
- from_base_rate = super(source, from_currency, opts)
153
- unless from_base_rate
154
- from_inverse_rate = super(from_currency, source, opts)
155
- from_base_rate = 1.0 / from_inverse_rate if from_inverse_rate
156
- end
157
- to_base_rate = super(source, to_currency, opts)
158
- unless to_base_rate
159
- to_inverse_rate = super(to_currency, source, opts)
160
- to_base_rate = 1.0 / to_inverse_rate if to_inverse_rate
161
- end
162
- if to_base_rate && from_base_rate
163
- rate = to_base_rate / from_base_rate
164
- add_rate(from_currency, to_currency, rate)
165
- end
166
- end
167
- rate
136
+ rate = get_rate_or_calc_inverse(from_currency, to_currency, opts)
137
+ rate || calc_pair_rate_using_base(from_currency, to_currency, opts)
168
138
  end
169
139
 
170
140
  # Fetch new rates if cached rates are expired or stale
@@ -305,6 +275,7 @@ class Money
305
275
  end
306
276
 
307
277
  # Get raw exchange rates from cache and then from url
278
+ # @param rescue_straight [Boolean] true for rescue straight, default true
308
279
  # @return [String] JSON content
309
280
  def raw_rates_careful(rescue_straight = true)
310
281
  JSON.parse(read_from_cache.to_s)
@@ -319,6 +290,38 @@ class Money
319
290
  rescue JSON::ParserError
320
291
  raw_rates_careful(false)
321
292
  end
293
+
294
+ # Get rate or calculate it as inverse rate
295
+ # @param [String] from_currency Currency ISO code. ex. 'USD'
296
+ # @param [String] to_currency Currency ISO code. ex. 'CAD'
297
+ # @return [Numeric] rate or rate calculated as inverse rate.
298
+ def get_rate_or_calc_inverse(from_currency, to_currency, opts = {})
299
+ rate = super_get_rate(from_currency, to_currency, opts)
300
+ unless rate
301
+ # Tries to calculate an inverse rate
302
+ inverse_rate = super_get_rate(to_currency, from_currency, opts)
303
+ if inverse_rate
304
+ rate = 1.0 / inverse_rate
305
+ add_rate(from_currency, to_currency, rate)
306
+ end
307
+ end
308
+ rate
309
+ end
310
+
311
+ # Tries to calculate a pair rate using base currency rate
312
+ # @param [String] from_currency Currency ISO code. ex. 'USD'
313
+ # @param [String] to_currency Currency ISO code. ex. 'CAD'
314
+ # @return [Numeric] rate or nil if cannot calculate rate.
315
+ def calc_pair_rate_using_base(from_currency, to_currency, opts = {})
316
+ from_base_rate = get_rate_or_calc_inverse(source, from_currency, opts)
317
+ to_base_rate = get_rate_or_calc_inverse(source, to_currency, opts)
318
+ if to_base_rate && from_base_rate
319
+ rate = to_base_rate / from_base_rate
320
+ add_rate(from_currency, to_currency, rate)
321
+ return rate
322
+ end
323
+ nil
324
+ end
322
325
  end
323
326
  end
324
327
  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.4
4
+ version: 0.5.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Egon Zemmer
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2017-05-08 00:00:00.000000000 Z
11
+ date: 2017-05-25 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: money