money-currencylayer-bank 0.4.2 → 0.5.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +87 -2
- data/lib/money/bank/currencylayer_bank.rb +51 -19
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: f30609c06313fb218edbad89627da0d1bb7bc66d
|
4
|
+
data.tar.gz: 8a1ffe4d046a32e3942b67cf89977ef3d8c2c0f5
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 546c3541ae100259af247726d04e17fdb8e86a011e19d3f8d378f8eef8d373f11f3479a703f68876d38a82bfdbf8a26784e25009d7b51178dfea0b07b14e8418
|
7
|
+
data.tar.gz: 11ccf7440dbf5d076578cce0e6cc591c70217ec5481c97caa2f1a32bc4ab03cdffe35fcbbf7a8193151f2687ca512c83e683b44deee1eb0200b36b53be6d4f66
|
data/README.md
CHANGED
@@ -9,6 +9,36 @@
|
|
9
9
|
A gem that calculates the exchange rate using published rates from
|
10
10
|
[currencylayer.com](https://currencylayer.com/)
|
11
11
|
|
12
|
+
## Currencylayer API
|
13
|
+
|
14
|
+
~~~ json
|
15
|
+
{
|
16
|
+
"timestamp": 1441101909,
|
17
|
+
"source": "USD",
|
18
|
+
"quotes": {
|
19
|
+
/* 168 currencies */
|
20
|
+
"USDAUD": 1.413637,
|
21
|
+
"USDCAD": 1.316495,
|
22
|
+
"USDCHF": 0.96355,
|
23
|
+
"USDEUR": 0.888466,
|
24
|
+
"USDBTC": 0.004322, /* Includes Bitcoin currency! */
|
25
|
+
...
|
26
|
+
}
|
27
|
+
}
|
28
|
+
~~~
|
29
|
+
|
30
|
+
See more about Currencylayer product plans on https://currencylayer.com/product.
|
31
|
+
|
32
|
+
## Features
|
33
|
+
|
34
|
+
* supports 168 currencies
|
35
|
+
* includes [Bitcoin](https://en.wikipedia.org/wiki/Bitcoin) virtual currency
|
36
|
+
* precision of rates up to 6 digits after point
|
37
|
+
* uses fast and reliable json api
|
38
|
+
* average response time < 20ms
|
39
|
+
* supports caching currency rates
|
40
|
+
* calculates every pair rate calculating inverse rate or using base currency rate
|
41
|
+
|
12
42
|
## Usage
|
13
43
|
|
14
44
|
~~~ ruby
|
@@ -45,6 +75,43 @@ mclb.cache = 'path/to/file/cache'
|
|
45
75
|
Money.default_bank = mclb
|
46
76
|
~~~
|
47
77
|
|
78
|
+
### More methods
|
79
|
+
|
80
|
+
~~~ ruby
|
81
|
+
mclb = Money::Bank::CurrencylayerBank.new
|
82
|
+
|
83
|
+
# Returns the base currency set for all rates.
|
84
|
+
mclb.source
|
85
|
+
|
86
|
+
# Expires rates if the expiration time is reached.
|
87
|
+
mclb.expire_rates!
|
88
|
+
|
89
|
+
# Return true if the expiration time is reached.
|
90
|
+
mclb.expired?
|
91
|
+
|
92
|
+
# Get the API source url.
|
93
|
+
mclb.source_url
|
94
|
+
|
95
|
+
# Get the rates timestamp of the last API request.
|
96
|
+
mclb.rates_timestamp
|
97
|
+
~~~
|
98
|
+
|
99
|
+
### How to exchange
|
100
|
+
|
101
|
+
~~~ ruby
|
102
|
+
# Exchange 1000 cents (10.0 USD) to EUR
|
103
|
+
Money.new(1000, 'USD').exchange_to('EUR') # => #<Money fractional:89 currency:EUR>
|
104
|
+
Money.new(1000, 'USD').exchange_to('EUR').to_f # => 8.9
|
105
|
+
|
106
|
+
# Format
|
107
|
+
Money.new(1000, 'USD').exchange_to('EUR').format # => €8.90
|
108
|
+
|
109
|
+
# Get the rate
|
110
|
+
Money.default_bank.get_rate('USD', 'CAD') # => 0.9
|
111
|
+
~~~
|
112
|
+
|
113
|
+
See more on https://github.com/RubyMoney/money.
|
114
|
+
|
48
115
|
### Using gem money-rails
|
49
116
|
|
50
117
|
You can also use it in Rails with the gem [money-rails](https://github.com/RubyMoney/money-rails).
|
@@ -92,14 +159,32 @@ bundle exec rake
|
|
92
159
|
|
93
160
|
## Refs
|
94
161
|
|
95
|
-
*
|
96
|
-
*
|
162
|
+
* Gem [money](https://github.com/RubyMoney/money)
|
163
|
+
* Gem [money-open-exchange-rates](https://github.com/spk/money-open-exchange-rates)
|
164
|
+
* Gem [money-historical-bank](https://github.com/atwam/money-historical-bank)
|
165
|
+
|
166
|
+
## Other Implementations
|
167
|
+
|
168
|
+
* Gem [currencylayer](https://github.com/askuratovsky/currencylayer)
|
169
|
+
* Gem [money-open-exchange-rates](https://github.com/spk/money-open-exchange-rates)
|
170
|
+
* Gem [money-historical-bank](https://github.com/atwam/money-historical-bank)
|
171
|
+
* Gem [eu_central_bank](https://github.com/RubyMoney/eu_central_bank)
|
172
|
+
* Gem [nordea](https://github.com/matiaskorhonen/nordea)
|
173
|
+
* Gem [google_currency](https://github.com/RubyMoney/google_currency)
|
97
174
|
|
98
175
|
## Contributors
|
99
176
|
|
100
177
|
* See [github.com/phlegx/money-currencylayer-bank](https://github.com/phlegx/money-currencylayer-bank/graphs/contributors).
|
101
178
|
* Inspired by [github.com/spk/money-open-exchange-rates](https://github.com/spk/money-open-exchange-rates/graphs/contributors).
|
102
179
|
|
180
|
+
## Contributing
|
181
|
+
|
182
|
+
1. Fork it ( https://github.com/[your-username]/money-currencylayer-bank/fork )
|
183
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
184
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
185
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
186
|
+
5. Create a new Pull Request
|
187
|
+
|
103
188
|
## License
|
104
189
|
|
105
190
|
The MIT License
|
@@ -52,13 +52,13 @@ class Money
|
|
52
52
|
#
|
53
53
|
# @param value [String] Currency code, ISO 3166-1 alpha-3
|
54
54
|
#
|
55
|
-
# @return [String]
|
55
|
+
# @return [String] chosen base currency
|
56
56
|
def source=(value)
|
57
57
|
@source = Money::Currency.find(value.to_s).try(:iso_code) || CL_SOURCE
|
58
58
|
end
|
59
59
|
|
60
60
|
# Get the base currency for all rates. By default, USD is used.
|
61
|
-
# @return [String]
|
61
|
+
# @return [String] base currency
|
62
62
|
def source
|
63
63
|
@source ||= CL_SOURCE
|
64
64
|
end
|
@@ -69,9 +69,9 @@ class Money
|
|
69
69
|
# @example
|
70
70
|
# ttl_in_seconds = 86400 # will expire the rates in one day
|
71
71
|
#
|
72
|
-
# @param value [Integer]
|
72
|
+
# @param value [Integer] time to live in seconds
|
73
73
|
#
|
74
|
-
# @return [Integer]
|
74
|
+
# @return [Integer] chosen time to live in seconds
|
75
75
|
def ttl_in_seconds=(value)
|
76
76
|
@ttl_in_seconds = value
|
77
77
|
refresh_rates_expiration! if ttl_in_seconds
|
@@ -79,14 +79,14 @@ class Money
|
|
79
79
|
end
|
80
80
|
|
81
81
|
# Update all rates from CurrencylayerBank JSON
|
82
|
-
# @return [Array]
|
82
|
+
# @return [Array] array of exchange rates
|
83
83
|
def update_rates(straight = false)
|
84
84
|
exchange_rates(straight).each do |exchange_rate|
|
85
85
|
currency = exchange_rate.first[3..-1]
|
86
86
|
rate = exchange_rate.last
|
87
87
|
next unless Money::Currency.find(currency)
|
88
|
-
|
89
|
-
|
88
|
+
add_rate(source, currency, rate)
|
89
|
+
add_rate(currency, source, 1.0 / rate)
|
90
90
|
end
|
91
91
|
end
|
92
92
|
|
@@ -95,25 +95,57 @@ class Money
|
|
95
95
|
# @param [String] to_currency Currency ISO code. ex. 'CAD'
|
96
96
|
#
|
97
97
|
# @return [Numeric] rate.
|
98
|
-
def get_rate(from_currency, to_currency, opts = {})
|
98
|
+
def get_rate(from_currency, to_currency, opts = {}) # rubocop:disable all
|
99
99
|
expire_rates!
|
100
|
-
super
|
100
|
+
rate = super
|
101
|
+
unless rate
|
102
|
+
# Tries to calculate an inverse rate
|
103
|
+
inverse_rate = super(to_currency, from_currency, opts)
|
104
|
+
if inverse_rate
|
105
|
+
rate = 1.0 / inverse_rate
|
106
|
+
add_rate(from_currency, to_currency, rate)
|
107
|
+
end
|
108
|
+
end
|
109
|
+
unless rate
|
110
|
+
# Tries to calculate a pair rate using base currency rate
|
111
|
+
from_base_rate = super(source, from_currency, opts)
|
112
|
+
unless from_base_rate
|
113
|
+
from_inverse_rate = super(from_currency, source, opts)
|
114
|
+
from_base_rate = 1.0 / from_inverse_rate if from_inverse_rate
|
115
|
+
end
|
116
|
+
to_base_rate = super(source, to_currency, opts)
|
117
|
+
unless to_base_rate
|
118
|
+
to_inverse_rate = super(to_currency, source, opts)
|
119
|
+
to_base_rate = 1.0 / to_inverse_rate if to_inverse_rate
|
120
|
+
end
|
121
|
+
if to_base_rate && from_base_rate
|
122
|
+
rate = to_base_rate / from_base_rate
|
123
|
+
add_rate(from_currency, to_currency, rate)
|
124
|
+
end
|
125
|
+
end
|
126
|
+
rate
|
101
127
|
end
|
102
128
|
|
103
|
-
#
|
129
|
+
# Fetch new rates if cached rates are expired
|
130
|
+
# @return [Boolean] true if rates are expired and updated from remote
|
104
131
|
def expire_rates!
|
105
|
-
|
106
|
-
|
132
|
+
if expired?
|
133
|
+
update_rates(true)
|
134
|
+
true
|
135
|
+
else
|
136
|
+
false
|
137
|
+
end
|
107
138
|
end
|
108
139
|
|
109
|
-
# Check if
|
110
|
-
# @return [Boolean]
|
140
|
+
# Check if rates are expired
|
141
|
+
# @return [Boolean] true if rates are expired
|
111
142
|
def expired?
|
112
|
-
!ttl_in_seconds.nil? && rates_expiration
|
143
|
+
!ttl_in_seconds.nil? && rates_expiration <= Time.now
|
113
144
|
end
|
114
145
|
|
115
146
|
# Source url of CurrencylayerBank
|
116
147
|
# defined with access_key and secure_connection
|
148
|
+
# @return [String] the remote API url
|
117
149
|
def source_url
|
118
150
|
fail NoAccessKey if access_key.nil? || access_key.empty?
|
119
151
|
cl_url = CL_URL
|
@@ -122,7 +154,7 @@ class Money
|
|
122
154
|
end
|
123
155
|
|
124
156
|
# Get the timestamp of rates
|
125
|
-
# @return [Time]
|
157
|
+
# @return [Time] time object or nil
|
126
158
|
def rates_timestamp
|
127
159
|
parsed = raw_rates_careful
|
128
160
|
parsed.key?('timestamp') ? Time.at(parsed['timestamp']) : nil
|
@@ -136,7 +168,7 @@ class Money
|
|
136
168
|
# @example
|
137
169
|
# store_in_cache("{\"quotes\": {\"USDAED\": 3.67304}}")
|
138
170
|
#
|
139
|
-
# @param text [String]
|
171
|
+
# @param text [String] unparsed JSON content
|
140
172
|
# @return [String,Integer]
|
141
173
|
def store_in_cache(text)
|
142
174
|
if cache.is_a?(Proc)
|
@@ -147,7 +179,7 @@ class Money
|
|
147
179
|
end
|
148
180
|
|
149
181
|
# Writes content to file cache
|
150
|
-
# @param text [String]
|
182
|
+
# @param text [String] unparsed JSON content
|
151
183
|
# @return [String,Integer]
|
152
184
|
def write_to_file(text)
|
153
185
|
open(cache, 'w') do |f|
|
@@ -158,7 +190,7 @@ class Money
|
|
158
190
|
end
|
159
191
|
|
160
192
|
# Read from cache when exist
|
161
|
-
# @return [Proc,String] JSON content
|
193
|
+
# @return [Proc,String] unparsed JSON content
|
162
194
|
def read_from_cache
|
163
195
|
if cache.is_a?(Proc)
|
164
196
|
cache.call(nil)
|
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.
|
4
|
+
version: 0.5.0
|
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-
|
11
|
+
date: 2015-09-01 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: money
|