currency-rate 0.3.8 → 0.3.9
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/VERSION +1 -1
- data/currency-rate.gemspec +25 -25
- data/lib/adapter.rb +3 -6
- data/lib/crypto_adapter.rb +19 -0
- data/lib/{btc_adapters → crypto_adapters}/average_rate_adapter.rb +2 -1
- data/lib/{btc_adapters → crypto_adapters}/bitfinex_adapter.rb +3 -2
- data/lib/{btc_adapters → crypto_adapters}/bitpay_adapter.rb +2 -1
- data/lib/{btc_adapters → crypto_adapters}/bitstamp_adapter.rb +3 -2
- data/lib/{btc_adapters → crypto_adapters}/btcchina_adapter.rb +2 -1
- data/lib/{btc_adapters → crypto_adapters}/btce_adapter.rb +3 -2
- data/lib/{btc_adapters → crypto_adapters}/coinbase_adapter.rb +2 -1
- data/lib/{btc_adapters → crypto_adapters}/huobi_adapter.rb +3 -2
- data/lib/{btc_adapters → crypto_adapters}/kraken_adapter.rb +4 -4
- data/lib/{btc_adapters → crypto_adapters}/localbitcoins_adapter.rb +3 -2
- data/lib/{btc_adapters → crypto_adapters}/okcoin_adapter.rb +3 -2
- data/lib/currency_rate.rb +18 -13
- data/lib/fiat_adapter.rb +2 -7
- data/lib/fiat_adapters/fixer_adapter.rb +3 -2
- data/lib/fiat_adapters/yahoo_adapter.rb +1 -1
- data/spec/lib/{btc_adapter_spec.rb → crypto_adapter_spec.rb} +5 -3
- data/spec/lib/{btc_adapters → crypto_adapters}/average_rate_adapter_spec.rb +0 -0
- data/spec/lib/{btc_adapters → crypto_adapters}/bitfinex_adapter_spec.rb +0 -0
- data/spec/lib/{btc_adapters → crypto_adapters}/bitpay_adapter_spec.rb +0 -0
- data/spec/lib/{btc_adapters → crypto_adapters}/bitstamp_adapter_spec.rb +0 -0
- data/spec/lib/{btc_adapters → crypto_adapters}/btcchina_adapter_spec.rb +0 -0
- data/spec/lib/{btc_adapters → crypto_adapters}/btce_adapter_spec.rb +0 -0
- data/spec/lib/{btc_adapters → crypto_adapters}/coinbase_adapter_spec.rb +0 -0
- data/spec/lib/{btc_adapters → crypto_adapters}/huobi_adapter_spec.rb +0 -0
- data/spec/lib/{btc_adapters → crypto_adapters}/kraken_adapter_spec.rb +0 -0
- data/spec/lib/{btc_adapters → crypto_adapters}/localbitcoins_adapter_spec.rb +0 -0
- data/spec/lib/{btc_adapters → crypto_adapters}/okcoin_adapter_spec.rb +0 -0
- metadata +26 -26
- data/lib/btc_adapter.rb +0 -21
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: dae4fdb70ee423c0951959f02d617c058c7db632
|
4
|
+
data.tar.gz: a63d7445c9ef171e55b9540272a43ab92645ed90
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: fdfcfc768c1b40fbd944f78ea1822c9254dc1385d63ff023f9fc4806c47c30a2021faf2e42648a9ee6bfabbd0062bd12c2b9bac6b67ebd2148d395f43161a44f
|
7
|
+
data.tar.gz: 3632250c161414a608933f067dd5325ebfb9cf7054727c7e064d1b833a09be7e31017e8e588c59c528731b664fecddeb07ec98c32e1b48807938948d053f15a5
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.3.
|
1
|
+
0.3.9
|
data/currency-rate.gemspec
CHANGED
@@ -11,7 +11,7 @@ Gem::Specification.new do |s|
|
|
11
11
|
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
12
12
|
s.require_paths = ["lib"]
|
13
13
|
s.authors = ["Roman Snitko"]
|
14
|
-
s.date = "2017-
|
14
|
+
s.date = "2017-08-24"
|
15
15
|
s.description = "Fetches exchange rates from various sources and does the conversion"
|
16
16
|
s.email = "roman.snitko@gmail.com"
|
17
17
|
s.extra_rdoc_files = [
|
@@ -29,19 +29,19 @@ Gem::Specification.new do |s|
|
|
29
29
|
"VERSION",
|
30
30
|
"currency-rate.gemspec",
|
31
31
|
"lib/adapter.rb",
|
32
|
-
"lib/btc_adapter.rb",
|
33
|
-
"lib/btc_adapters/average_rate_adapter.rb",
|
34
|
-
"lib/btc_adapters/bitfinex_adapter.rb",
|
35
|
-
"lib/btc_adapters/bitpay_adapter.rb",
|
36
|
-
"lib/btc_adapters/bitstamp_adapter.rb",
|
37
|
-
"lib/btc_adapters/btcchina_adapter.rb",
|
38
|
-
"lib/btc_adapters/btce_adapter.rb",
|
39
|
-
"lib/btc_adapters/coinbase_adapter.rb",
|
40
|
-
"lib/btc_adapters/huobi_adapter.rb",
|
41
|
-
"lib/btc_adapters/kraken_adapter.rb",
|
42
|
-
"lib/btc_adapters/localbitcoins_adapter.rb",
|
43
|
-
"lib/btc_adapters/okcoin_adapter.rb",
|
44
32
|
"lib/core_ext/deep_get.rb",
|
33
|
+
"lib/crypto_adapter.rb",
|
34
|
+
"lib/crypto_adapters/average_rate_adapter.rb",
|
35
|
+
"lib/crypto_adapters/bitfinex_adapter.rb",
|
36
|
+
"lib/crypto_adapters/bitpay_adapter.rb",
|
37
|
+
"lib/crypto_adapters/bitstamp_adapter.rb",
|
38
|
+
"lib/crypto_adapters/btcchina_adapter.rb",
|
39
|
+
"lib/crypto_adapters/btce_adapter.rb",
|
40
|
+
"lib/crypto_adapters/coinbase_adapter.rb",
|
41
|
+
"lib/crypto_adapters/huobi_adapter.rb",
|
42
|
+
"lib/crypto_adapters/kraken_adapter.rb",
|
43
|
+
"lib/crypto_adapters/localbitcoins_adapter.rb",
|
44
|
+
"lib/crypto_adapters/okcoin_adapter.rb",
|
45
45
|
"lib/currency_rate.rb",
|
46
46
|
"lib/fiat_adapter.rb",
|
47
47
|
"lib/fiat_adapters/fixer_adapter.rb",
|
@@ -60,18 +60,18 @@ Gem::Specification.new do |s|
|
|
60
60
|
"spec/fixtures/vcr/exchange_rate_adapters/btc_adapters/okcoin_adapter.yml",
|
61
61
|
"spec/fixtures/vcr/exchange_rate_adapters/fiat_adapters/fixer_adapter.yml",
|
62
62
|
"spec/fixtures/vcr/exchange_rate_adapters/fiat_adapters/yahoo_adapter.yml",
|
63
|
-
"spec/lib/
|
64
|
-
"spec/lib/
|
65
|
-
"spec/lib/
|
66
|
-
"spec/lib/
|
67
|
-
"spec/lib/
|
68
|
-
"spec/lib/
|
69
|
-
"spec/lib/
|
70
|
-
"spec/lib/
|
71
|
-
"spec/lib/
|
72
|
-
"spec/lib/
|
73
|
-
"spec/lib/
|
74
|
-
"spec/lib/
|
63
|
+
"spec/lib/crypto_adapter_spec.rb",
|
64
|
+
"spec/lib/crypto_adapters/average_rate_adapter_spec.rb",
|
65
|
+
"spec/lib/crypto_adapters/bitfinex_adapter_spec.rb",
|
66
|
+
"spec/lib/crypto_adapters/bitpay_adapter_spec.rb",
|
67
|
+
"spec/lib/crypto_adapters/bitstamp_adapter_spec.rb",
|
68
|
+
"spec/lib/crypto_adapters/btcchina_adapter_spec.rb",
|
69
|
+
"spec/lib/crypto_adapters/btce_adapter_spec.rb",
|
70
|
+
"spec/lib/crypto_adapters/coinbase_adapter_spec.rb",
|
71
|
+
"spec/lib/crypto_adapters/huobi_adapter_spec.rb",
|
72
|
+
"spec/lib/crypto_adapters/kraken_adapter_spec.rb",
|
73
|
+
"spec/lib/crypto_adapters/localbitcoins_adapter_spec.rb",
|
74
|
+
"spec/lib/crypto_adapters/okcoin_adapter_spec.rb",
|
75
75
|
"spec/lib/fiat_adapters/fixer_adapter_spec.rb",
|
76
76
|
"spec/lib/fiat_adapters/yahoo_adapter_spec.rb",
|
77
77
|
"spec/lib/storage_spec.rb",
|
data/lib/adapter.rb
CHANGED
@@ -5,6 +5,8 @@ module CurrencyRate
|
|
5
5
|
class FetchingFailed < Exception; end
|
6
6
|
class CurrencyNotSupported < Exception; end
|
7
7
|
|
8
|
+
DECIMAL_PRECISION = nil
|
9
|
+
|
8
10
|
attr_accessor :try_storage_on_fetching_failed
|
9
11
|
attr_reader :storage
|
10
12
|
|
@@ -97,13 +99,8 @@ module CurrencyRate
|
|
97
99
|
|
98
100
|
def _invert_rate(rate)
|
99
101
|
r = (BigDecimal.new('1')/BigDecimal.new(rate.to_s))
|
100
|
-
r = r.round(
|
102
|
+
r = r.round(self.class::DECIMAL_PRECISION) if self.class::DECIMAL_PRECISION
|
101
103
|
r
|
102
104
|
end
|
103
|
-
|
104
|
-
def decimal_precision
|
105
|
-
nil
|
106
|
-
end
|
107
|
-
|
108
105
|
end
|
109
106
|
end
|
@@ -0,0 +1,19 @@
|
|
1
|
+
module CurrencyRate
|
2
|
+
|
3
|
+
class CryptoAdapter < Adapter
|
4
|
+
|
5
|
+
SUPPORTED_CRYPTO_CURRENCIES = []
|
6
|
+
|
7
|
+
DECIMAL_PRECISION = 9
|
8
|
+
|
9
|
+
def rate_for(from,to)
|
10
|
+
super
|
11
|
+
end
|
12
|
+
|
13
|
+
def invert_rate(from,to,rate)
|
14
|
+
self.class::SUPPORTED_CRYPTO_CURRENCIES.include?(to) ? _invert_rate(rate) : rate
|
15
|
+
end
|
16
|
+
|
17
|
+
end
|
18
|
+
|
19
|
+
end
|
@@ -1,7 +1,8 @@
|
|
1
1
|
module CurrencyRate
|
2
|
-
class AverageRateAdapter <
|
2
|
+
class AverageRateAdapter < CryptoAdapter
|
3
3
|
|
4
4
|
DEFAULT_CURRENCIES = ["USD", "BTC"]
|
5
|
+
SUPPORTED_CRYPTO_CURRENCIES = ["BTC"]
|
5
6
|
|
6
7
|
# Takes exchange rate adapters instances or classes as arguments
|
7
8
|
def self.instance(*adapters)
|
@@ -1,11 +1,12 @@
|
|
1
1
|
module CurrencyRate
|
2
|
-
class BitfinexAdapter <
|
2
|
+
class BitfinexAdapter < CryptoAdapter
|
3
3
|
|
4
4
|
FETCH_URL = {
|
5
5
|
'btc_usd' => 'https://api.bitfinex.com/v1/pubticker/btcusd',
|
6
6
|
'ltc_usd' => 'https://api.bitfinex.com/v1/pubticker/ltcusd'
|
7
7
|
}
|
8
8
|
DEFAULT_CURRENCIES = ["USD", "BTC"]
|
9
|
+
SUPPORTED_CRYPTO_CURRENCIES = ["BTC", "LTC"]
|
9
10
|
|
10
11
|
def rate_for(from,to)
|
11
12
|
super
|
@@ -20,7 +21,7 @@ module CurrencyRate
|
|
20
21
|
end
|
21
22
|
|
22
23
|
def invert_rate(from,to,rate)
|
23
|
-
if
|
24
|
+
if self.class::SUPPORTED_CRYPTO_CURRENCIES.include?(to)
|
24
25
|
_invert_rate(rate)
|
25
26
|
else
|
26
27
|
rate
|
@@ -1,5 +1,5 @@
|
|
1
1
|
module CurrencyRate
|
2
|
-
class BitstampAdapter <
|
2
|
+
class BitstampAdapter < CryptoAdapter
|
3
3
|
|
4
4
|
FETCH_URL = {
|
5
5
|
'btc_usd' => 'https://www.bitstamp.net/api/v2/ticker/btcusd/',
|
@@ -7,6 +7,7 @@ module CurrencyRate
|
|
7
7
|
'eur_usd' => 'https://www.bitstamp.net/api/v2/ticker/eurusd/'
|
8
8
|
}
|
9
9
|
DEFAULT_CURRENCIES = ["USD", "BTC"]
|
10
|
+
SUPPORTED_CRYPTO_CURRENCIES = ["BTC"]
|
10
11
|
|
11
12
|
def rate_for(from,to)
|
12
13
|
super
|
@@ -16,7 +17,7 @@ module CurrencyRate
|
|
16
17
|
|
17
18
|
# Because Bitstamp has USD/EUR pair
|
18
19
|
def invert_rate(from,to,rate)
|
19
|
-
if to
|
20
|
+
if self.class::SUPPORTED_CRYPTO_CURRENCIES.include?(to) || (from == 'USD' && to == 'EUR')
|
20
21
|
_invert_rate(rate)
|
21
22
|
else
|
22
23
|
rate
|
@@ -1,8 +1,9 @@
|
|
1
1
|
module CurrencyRate
|
2
|
-
class BTCChinaAdapter <
|
2
|
+
class BTCChinaAdapter < CryptoAdapter
|
3
3
|
|
4
4
|
FETCH_URL = 'https://data.btcchina.com/data/ticker'
|
5
5
|
DEFAULT_CURRENCIES = ["CNY", "BTC"]
|
6
|
+
SUPPORTED_CRYPTO_CURRENCIES = ["BTC"]
|
6
7
|
|
7
8
|
def rate_for(from,to)
|
8
9
|
super
|
@@ -1,5 +1,5 @@
|
|
1
1
|
module CurrencyRate
|
2
|
-
class BtceAdapter <
|
2
|
+
class BtceAdapter < CryptoAdapter
|
3
3
|
|
4
4
|
FETCH_URL = {
|
5
5
|
'btc_usd' => 'https://btc-e.com/api/2/btc_usd/ticker',
|
@@ -9,6 +9,7 @@ module CurrencyRate
|
|
9
9
|
'eur_rub' => 'https://btc-e.com/api/2/eur_rur/ticker'
|
10
10
|
}
|
11
11
|
DEFAULT_CURRENCIES = ["USD", "BTC"]
|
12
|
+
SUPPORTED_CRYPTO_CURRENCIES = ["BTC"]
|
12
13
|
|
13
14
|
|
14
15
|
def rate_for(from,to)
|
@@ -24,7 +25,7 @@ module CurrencyRate
|
|
24
25
|
end
|
25
26
|
|
26
27
|
def invert_rate(from,to,rate)
|
27
|
-
if to
|
28
|
+
if self.class::SUPPORTED_CRYPTO_CURRENCIES.include?(to) || (from == 'RUB' && to == 'USD') || (from == 'RUB' && to == 'EUR')
|
28
29
|
_invert_rate(rate)
|
29
30
|
else
|
30
31
|
rate
|
@@ -1,8 +1,9 @@
|
|
1
1
|
module CurrencyRate
|
2
|
-
class CoinbaseAdapter <
|
2
|
+
class CoinbaseAdapter < CryptoAdapter
|
3
3
|
|
4
4
|
FETCH_URL = 'https://coinbase.com/api/v1/currencies/exchange_rates'
|
5
5
|
DEFAULT_CURRENCIES = ["USD", "BTC"]
|
6
|
+
SUPPORTED_CRYPTO_CURRENCIES = ["BTC", "LTC"]
|
6
7
|
|
7
8
|
def rate_for(from,to)
|
8
9
|
super
|
@@ -1,11 +1,12 @@
|
|
1
1
|
module CurrencyRate
|
2
|
-
class HuobiAdapter <
|
2
|
+
class HuobiAdapter < CryptoAdapter
|
3
3
|
|
4
4
|
FETCH_URL = {
|
5
5
|
'btc_cny' => 'http://api.huobi.com/staticmarket/ticker_btc_json.js',
|
6
6
|
'ltc_cny' => 'http://api.huobi.com/staticmarket/ticker_ltc_json.js'
|
7
7
|
}
|
8
8
|
DEFAULT_CURRENCIES = ["CNY", "BTC"]
|
9
|
+
SUPPORTED_CRYPTO_CURRENCIES = ["BTC", "LTC"]
|
9
10
|
|
10
11
|
def rate_for(from,to)
|
11
12
|
super
|
@@ -20,7 +21,7 @@ module CurrencyRate
|
|
20
21
|
end
|
21
22
|
|
22
23
|
def invert_rate(from,to,rate)
|
23
|
-
if
|
24
|
+
if self.class::SUPPORTED_CRYPTO_CURRENCIES.include?(to)
|
24
25
|
_invert_rate(rate)
|
25
26
|
else
|
26
27
|
rate
|
@@ -1,5 +1,5 @@
|
|
1
1
|
module CurrencyRate
|
2
|
-
class KrakenAdapter <
|
2
|
+
class KrakenAdapter < CryptoAdapter
|
3
3
|
|
4
4
|
FETCH_URL = {
|
5
5
|
"usd_btc" => 'https://api.kraken.com/0/public/Ticker?pair=xbtusd',
|
@@ -8,7 +8,7 @@ module CurrencyRate
|
|
8
8
|
"eur_ltc" => 'https://api.kraken.com/0/public/Ticker?pair=ltceur',
|
9
9
|
}
|
10
10
|
DEFAULT_CURRENCIES = ["USD", "BTC"]
|
11
|
-
|
11
|
+
SUPPORTED_CRYPTO_CURRENCIES = ["BTC", "LTC"]
|
12
12
|
ASSET_MAP = {
|
13
13
|
"BTC" => "XBT",
|
14
14
|
}
|
@@ -21,8 +21,8 @@ module CurrencyRate
|
|
21
21
|
|
22
22
|
def currency_pair_rate(currency1, currency2)
|
23
23
|
rate = @rates["#{currency1.downcase}_#{currency2.downcase}"] || @rates["#{currency2.downcase}_#{currency1.downcase}"]
|
24
|
-
raise CurrencyNotSupported unless rate || ([currency1, currency2] &
|
25
|
-
fiat, crypto =
|
24
|
+
raise CurrencyNotSupported unless rate || ([currency1, currency2] & self.class::SUPPORTED_CRYPTO_CURRENCIES).any?
|
25
|
+
fiat, crypto = self.class::SUPPORTED_CRYPTO_CURRENCIES.include?(currency1) ? [currency2, currency1] : [currency1, currency2]
|
26
26
|
rate['result']["X#{ta(crypto)}Z#{ta(fiat)}"]['c'].first
|
27
27
|
end
|
28
28
|
|
@@ -1,8 +1,9 @@
|
|
1
1
|
module CurrencyRate
|
2
|
-
class LocalbitcoinsAdapter <
|
2
|
+
class LocalbitcoinsAdapter < CryptoAdapter
|
3
3
|
|
4
4
|
FETCH_URL = 'https://localbitcoins.com/bitcoinaverage/ticker-all-currencies/'
|
5
5
|
DEFAULT_CURRENCIES = ["USD", "BTC"]
|
6
|
+
SUPPORTED_CRYPTO_CURRENCIES = ["BTC"]
|
6
7
|
|
7
8
|
def rate_for(from,to)
|
8
9
|
super
|
@@ -13,7 +14,7 @@ module CurrencyRate
|
|
13
14
|
|
14
15
|
def currency_pair_rate(currency1, currency2)
|
15
16
|
rate = @rates[currency1] || @rates[currency2]
|
16
|
-
raise CurrencyNotSupported if !rate || !([currency1, currency2].
|
17
|
+
raise CurrencyNotSupported if !rate || !([currency1, currency2] & self.class::SUPPORTED_CRYPTO_CURRENCIES).any?
|
17
18
|
rate['rates']['last']
|
18
19
|
end
|
19
20
|
|
@@ -1,5 +1,5 @@
|
|
1
1
|
module CurrencyRate
|
2
|
-
class OkcoinAdapter <
|
2
|
+
class OkcoinAdapter < CryptoAdapter
|
3
3
|
|
4
4
|
FETCH_URL = {
|
5
5
|
'ltc_usd' => 'https://www.okcoin.com/api/v1/ticker.do?symbol=ltc_usd',
|
@@ -8,6 +8,7 @@ module CurrencyRate
|
|
8
8
|
'btc_cny' => 'https://www.okcoin.cn/api/ticker.do?symbol=btc_cny'
|
9
9
|
}
|
10
10
|
DEFAULT_CURRENCIES = ["CNY", "BTC"]
|
11
|
+
SUPPORTED_CRYPTO_CURRENCIES = ["BTC", "LTC"]
|
11
12
|
|
12
13
|
def rate_for(from,to)
|
13
14
|
super
|
@@ -23,7 +24,7 @@ module CurrencyRate
|
|
23
24
|
|
24
25
|
# Because OKCoin has LTC
|
25
26
|
def invert_rate(from,to,rate)
|
26
|
-
if
|
27
|
+
if self.class::SUPPORTED_CRYPTO_CURRENCIES.include?(to)
|
27
28
|
_invert_rate(rate)
|
28
29
|
else
|
29
30
|
rate
|
data/lib/currency_rate.rb
CHANGED
@@ -5,7 +5,7 @@ require 'open-uri'
|
|
5
5
|
require 'json'
|
6
6
|
|
7
7
|
require "adapter"
|
8
|
-
require "
|
8
|
+
require "crypto_adapter"
|
9
9
|
require "fiat_adapter"
|
10
10
|
|
11
11
|
Dir["#{File.expand_path File.dirname(__FILE__)}/**/*.rb"].each { |f| require f }
|
@@ -14,42 +14,47 @@ module CurrencyRate
|
|
14
14
|
|
15
15
|
def self.get(adapter_name, from, to, anchor_currency: nil, try_storage_on_fetching_failed: false)
|
16
16
|
|
17
|
-
a =
|
17
|
+
a = adapter_instance(adapter_name)
|
18
18
|
a.try_storage_on_fetching_failed = try_storage_on_fetching_failed
|
19
19
|
|
20
20
|
# Setting default values for anchor currency depending on
|
21
21
|
# which adapter type is un use.
|
22
|
-
anchor_currency ||= if a.kind_of?(
|
23
|
-
|
22
|
+
anchor_currency ||= if a.kind_of?(CryptoAdapter)
|
23
|
+
a.class::SUPPORTED_CRYPTO_CURRENCIES.include?("BTC") ? "BTC" : a.class::SUPPORTED_CRYPTO_CURRENCIES.first
|
24
24
|
else
|
25
|
-
|
26
|
-
end
|
25
|
+
a.class::SUPPORTED_CURRENCIES.include?("USD") ? "USD" : a.class::SUPPORTED_CURRENCIES.first
|
26
|
+
end if anchor_currency.nil?
|
27
27
|
|
28
28
|
# None of the currencies is anchor currency?
|
29
29
|
# No problem, convert the amount given into the anchor currency first.
|
30
30
|
if [to, from].include?(anchor_currency)
|
31
31
|
a.rate_for(from, to)
|
32
32
|
else
|
33
|
-
rate_from = get(adapter_name, anchor_currency, from)
|
34
|
-
rate_to = get(adapter_name, anchor_currency, to )
|
33
|
+
rate_from = get(adapter_name, anchor_currency, from, try_storage_on_fetching_failed: try_storage_on_fetching_failed)
|
34
|
+
rate_to = get(adapter_name, anchor_currency, to , try_storage_on_fetching_failed: try_storage_on_fetching_failed)
|
35
35
|
BigDecimal.new(rate_to.to_s)/BigDecimal.new(rate_from.to_s)
|
36
36
|
end
|
37
37
|
end
|
38
38
|
|
39
39
|
def self.convert(adapter_name, amount:, from:, to:, anchor_currency: nil, try_storage_on_fetching_failed: false)
|
40
|
-
|
41
|
-
|
40
|
+
a = adapter_instance(adapter_name)
|
41
|
+
result = BigDecimal.new(amount.to_s)*BigDecimal.new(get(a, from, to, anchor_currency: anchor_currency, try_storage_on_fetching_failed: try_storage_on_fetching_failed).to_s)
|
42
|
+
result.round(a.kind_of?(CryptoAdapter) && a.class::SUPPORTED_CRYPTO_CURRENCIES.include?(to) ? a.class::DECIMAL_PRECISION : 2)
|
42
43
|
end
|
43
44
|
|
44
45
|
def self.default_currencies_for(adapter_name)
|
45
|
-
adapter_class(adapter_name)
|
46
|
+
adapter_class(adapter_name)::DEFAULT_CURRENCIES
|
46
47
|
end
|
47
48
|
|
48
49
|
private
|
49
50
|
|
50
|
-
def self.
|
51
|
+
def self.adapter_instance(s)
|
51
52
|
return s unless s.kind_of?(String) # if we pass class, no need to convert
|
52
|
-
|
53
|
+
adapter_class(s).instance
|
54
|
+
end
|
55
|
+
|
56
|
+
def self.adapter_class(s)
|
57
|
+
classify_string("#{s}_adapter", CurrencyRate)
|
53
58
|
end
|
54
59
|
|
55
60
|
def self.classify_string(s, prefix="")
|
data/lib/fiat_adapter.rb
CHANGED
@@ -3,7 +3,8 @@ module CurrencyRate
|
|
3
3
|
class FiatAdapter < Adapter
|
4
4
|
|
5
5
|
BigDecimal.mode BigDecimal::ROUND_MODE, :banker
|
6
|
-
|
6
|
+
|
7
|
+
DECIMAL_PRECISION = 2
|
7
8
|
|
8
9
|
def rate_for(from,to)
|
9
10
|
super
|
@@ -11,12 +12,6 @@ module CurrencyRate
|
|
11
12
|
invert_rate(from,to,rate)
|
12
13
|
end
|
13
14
|
|
14
|
-
private
|
15
|
-
|
16
|
-
def decimal_precision
|
17
|
-
2
|
18
|
-
end
|
19
|
-
|
20
15
|
end
|
21
16
|
|
22
17
|
end
|
@@ -5,17 +5,18 @@ module CurrencyRate
|
|
5
5
|
"eur" => "http://api.fixer.io/latest?base=eur",
|
6
6
|
"usd" => "http://api.fixer.io/latest?base=usd"
|
7
7
|
}
|
8
|
+
SUPPORTED_CURRENCIES = %w(USD EUR)
|
8
9
|
|
9
10
|
def currency_pair_rate(currency1, currency2)
|
10
11
|
rates = @rates[currency1.downcase] || @rates[currency2.downcase]
|
11
12
|
raise CurrencyNotSupported unless rates
|
12
13
|
rate = rates["rates"][currency1] || rates["rates"][currency2]
|
13
14
|
raise CurrencyNotSupported unless rate
|
14
|
-
BigDecimal.new(rate.to_s).round(
|
15
|
+
BigDecimal.new(rate.to_s).round(self.class::DECIMAL_PRECISION)
|
15
16
|
end
|
16
17
|
|
17
18
|
def invert_rate(from,to,rate)
|
18
|
-
if (to
|
19
|
+
if SUPPORTED_CURRENCIES.include?(to)
|
19
20
|
_invert_rate(rate)
|
20
21
|
else
|
21
22
|
rate
|
@@ -1,13 +1,15 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
|
-
RSpec.describe CurrencyRate::
|
3
|
+
RSpec.describe CurrencyRate::CryptoAdapter do
|
4
4
|
|
5
5
|
class CurrencyRate::Adapter
|
6
6
|
FETCH_URL = ''
|
7
7
|
DEFAULT_CURRENCIES = { from: "BTC", to: "USD" }
|
8
8
|
end
|
9
9
|
|
10
|
-
class SomeExchangeAdapter < CurrencyRate::
|
10
|
+
class SomeExchangeAdapter < CurrencyRate::CryptoAdapter
|
11
|
+
SUPPORTED_CRYPTO_CURRENCIES = ["BTC"]
|
12
|
+
|
11
13
|
def rate_for(from,to)
|
12
14
|
super
|
13
15
|
rate = rate_to_f(750)
|
@@ -20,7 +22,7 @@ RSpec.describe CurrencyRate::BtcAdapter do
|
|
20
22
|
end
|
21
23
|
|
22
24
|
before(:each) do
|
23
|
-
@exchange_adapter = CurrencyRate::
|
25
|
+
@exchange_adapter = CurrencyRate::CryptoAdapter.instance
|
24
26
|
end
|
25
27
|
|
26
28
|
it "inverts currency rate when needed" do
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: currency-rate
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.3.
|
4
|
+
version: 0.3.9
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Roman Snitko
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2017-
|
11
|
+
date: 2017-08-24 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: satoshi-unit
|
@@ -112,19 +112,19 @@ files:
|
|
112
112
|
- VERSION
|
113
113
|
- currency-rate.gemspec
|
114
114
|
- lib/adapter.rb
|
115
|
-
- lib/btc_adapter.rb
|
116
|
-
- lib/btc_adapters/average_rate_adapter.rb
|
117
|
-
- lib/btc_adapters/bitfinex_adapter.rb
|
118
|
-
- lib/btc_adapters/bitpay_adapter.rb
|
119
|
-
- lib/btc_adapters/bitstamp_adapter.rb
|
120
|
-
- lib/btc_adapters/btcchina_adapter.rb
|
121
|
-
- lib/btc_adapters/btce_adapter.rb
|
122
|
-
- lib/btc_adapters/coinbase_adapter.rb
|
123
|
-
- lib/btc_adapters/huobi_adapter.rb
|
124
|
-
- lib/btc_adapters/kraken_adapter.rb
|
125
|
-
- lib/btc_adapters/localbitcoins_adapter.rb
|
126
|
-
- lib/btc_adapters/okcoin_adapter.rb
|
127
115
|
- lib/core_ext/deep_get.rb
|
116
|
+
- lib/crypto_adapter.rb
|
117
|
+
- lib/crypto_adapters/average_rate_adapter.rb
|
118
|
+
- lib/crypto_adapters/bitfinex_adapter.rb
|
119
|
+
- lib/crypto_adapters/bitpay_adapter.rb
|
120
|
+
- lib/crypto_adapters/bitstamp_adapter.rb
|
121
|
+
- lib/crypto_adapters/btcchina_adapter.rb
|
122
|
+
- lib/crypto_adapters/btce_adapter.rb
|
123
|
+
- lib/crypto_adapters/coinbase_adapter.rb
|
124
|
+
- lib/crypto_adapters/huobi_adapter.rb
|
125
|
+
- lib/crypto_adapters/kraken_adapter.rb
|
126
|
+
- lib/crypto_adapters/localbitcoins_adapter.rb
|
127
|
+
- lib/crypto_adapters/okcoin_adapter.rb
|
128
128
|
- lib/currency_rate.rb
|
129
129
|
- lib/fiat_adapter.rb
|
130
130
|
- lib/fiat_adapters/fixer_adapter.rb
|
@@ -143,18 +143,18 @@ files:
|
|
143
143
|
- spec/fixtures/vcr/exchange_rate_adapters/btc_adapters/okcoin_adapter.yml
|
144
144
|
- spec/fixtures/vcr/exchange_rate_adapters/fiat_adapters/fixer_adapter.yml
|
145
145
|
- spec/fixtures/vcr/exchange_rate_adapters/fiat_adapters/yahoo_adapter.yml
|
146
|
-
- spec/lib/
|
147
|
-
- spec/lib/
|
148
|
-
- spec/lib/
|
149
|
-
- spec/lib/
|
150
|
-
- spec/lib/
|
151
|
-
- spec/lib/
|
152
|
-
- spec/lib/
|
153
|
-
- spec/lib/
|
154
|
-
- spec/lib/
|
155
|
-
- spec/lib/
|
156
|
-
- spec/lib/
|
157
|
-
- spec/lib/
|
146
|
+
- spec/lib/crypto_adapter_spec.rb
|
147
|
+
- spec/lib/crypto_adapters/average_rate_adapter_spec.rb
|
148
|
+
- spec/lib/crypto_adapters/bitfinex_adapter_spec.rb
|
149
|
+
- spec/lib/crypto_adapters/bitpay_adapter_spec.rb
|
150
|
+
- spec/lib/crypto_adapters/bitstamp_adapter_spec.rb
|
151
|
+
- spec/lib/crypto_adapters/btcchina_adapter_spec.rb
|
152
|
+
- spec/lib/crypto_adapters/btce_adapter_spec.rb
|
153
|
+
- spec/lib/crypto_adapters/coinbase_adapter_spec.rb
|
154
|
+
- spec/lib/crypto_adapters/huobi_adapter_spec.rb
|
155
|
+
- spec/lib/crypto_adapters/kraken_adapter_spec.rb
|
156
|
+
- spec/lib/crypto_adapters/localbitcoins_adapter_spec.rb
|
157
|
+
- spec/lib/crypto_adapters/okcoin_adapter_spec.rb
|
158
158
|
- spec/lib/fiat_adapters/fixer_adapter_spec.rb
|
159
159
|
- spec/lib/fiat_adapters/yahoo_adapter_spec.rb
|
160
160
|
- spec/lib/storage_spec.rb
|
data/lib/btc_adapter.rb
DELETED
@@ -1,21 +0,0 @@
|
|
1
|
-
module CurrencyRate
|
2
|
-
|
3
|
-
class BtcAdapter < Adapter
|
4
|
-
|
5
|
-
def rate_for(from,to)
|
6
|
-
super
|
7
|
-
end
|
8
|
-
|
9
|
-
def invert_rate(from,to,rate)
|
10
|
-
to == 'BTC' ? _invert_rate(rate) : rate
|
11
|
-
end
|
12
|
-
|
13
|
-
private
|
14
|
-
|
15
|
-
def decimal_precision
|
16
|
-
9
|
17
|
-
end
|
18
|
-
|
19
|
-
end
|
20
|
-
|
21
|
-
end
|