currency-rate 0.1.1 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (50) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +13 -0
  3. data/VERSION +1 -1
  4. data/currency-rate.gemspec +22 -3
  5. data/lib/adapter.rb +17 -9
  6. data/lib/btc_adapter.rb +4 -6
  7. data/lib/btc_adapters/average_rate_adapter.rb +4 -3
  8. data/lib/btc_adapters/bitfinex_adapter.rb +30 -0
  9. data/lib/btc_adapters/bitpay_adapter.rb +10 -3
  10. data/lib/btc_adapters/bitstamp_adapter.rb +24 -5
  11. data/lib/btc_adapters/btcchina_adapter.rb +13 -0
  12. data/lib/btc_adapters/btce_adapter.rb +26 -5
  13. data/lib/btc_adapters/coinbase_adapter.rb +2 -2
  14. data/lib/btc_adapters/huobi_adapter.rb +30 -0
  15. data/lib/btc_adapters/kraken_adapter.rb +14 -5
  16. data/lib/btc_adapters/localbitcoins_adapter.rb +10 -3
  17. data/lib/btc_adapters/okcoin_adapter.rb +24 -5
  18. data/lib/currency_rate.rb +11 -10
  19. data/lib/fiat_adapter.rb +4 -18
  20. data/lib/fiat_adapters/fixer_adapter.rb +20 -5
  21. data/lib/fiat_adapters/yahoo_adapter.rb +28 -6
  22. data/spec/currency_rate_spec.rb +10 -6
  23. data/spec/fixtures/vcr/exchange_rate_adapters/btc_adapters/average_rate_adapter.yml +423 -0
  24. data/spec/fixtures/vcr/exchange_rate_adapters/btc_adapters/bitfinex_adapter.yml +125 -0
  25. data/spec/fixtures/vcr/exchange_rate_adapters/btc_adapters/bitpay_adapter_yml.yml +272 -0
  26. data/spec/fixtures/vcr/exchange_rate_adapters/btc_adapters/bitstamp_adapter.yml +153 -0
  27. data/spec/fixtures/vcr/exchange_rate_adapters/btc_adapters/btcchina_adapter.yml +83 -0
  28. data/spec/fixtures/vcr/exchange_rate_adapters/btc_adapters/btce_adapter.yml +223 -0
  29. data/spec/fixtures/vcr/exchange_rate_adapters/btc_adapters/coinbase_adapter.yml +76 -0
  30. data/spec/fixtures/vcr/exchange_rate_adapters/btc_adapters/huobi_adapter.yml +83 -0
  31. data/spec/fixtures/vcr/exchange_rate_adapters/btc_adapters/kraken_adapter.yml +83 -0
  32. data/spec/fixtures/vcr/exchange_rate_adapters/btc_adapters/localbitcoins_adapter.yml +128 -0
  33. data/spec/fixtures/vcr/exchange_rate_adapters/btc_adapters/okcoin_adapter.yml +207 -0
  34. data/spec/fixtures/vcr/exchange_rate_adapters/fiat_adapters/fixer_adapter.yml +81 -0
  35. data/spec/fixtures/vcr/exchange_rate_adapters/fiat_adapters/yahoo_adapter.yml +50 -0
  36. data/spec/lib/btc_adapter_spec.rb +16 -28
  37. data/spec/lib/btc_adapters/average_rate_adapter_spec.rb +4 -4
  38. data/spec/lib/btc_adapters/bitfinex_adapter_spec.rb +38 -0
  39. data/spec/lib/btc_adapters/bitpay_adapter_spec.rb +8 -3
  40. data/spec/lib/btc_adapters/bitstamp_adapter_spec.rb +8 -3
  41. data/spec/lib/btc_adapters/btcchina_adapter_spec.rb +36 -0
  42. data/spec/lib/btc_adapters/btce_adapter_spec.rb +10 -3
  43. data/spec/lib/btc_adapters/coinbase_adapter_spec.rb +5 -3
  44. data/spec/lib/btc_adapters/huobi_adapter_spec.rb +38 -0
  45. data/spec/lib/btc_adapters/kraken_adapter_spec.rb +6 -3
  46. data/spec/lib/btc_adapters/localbitcoins_adapter_spec.rb +4 -3
  47. data/spec/lib/btc_adapters/okcoin_adapter_spec.rb +11 -3
  48. data/spec/lib/fiat_adapters/fixer_adapter_spec.rb +5 -2
  49. data/spec/lib/fiat_adapters/yahoo_adapter_spec.rb +5 -2
  50. metadata +21 -2
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: f65d7412d5bdf082fb39bc670010cf88aaf83ace
4
- data.tar.gz: 7fc7e9af03abb1747d6f1fc01c6f880d64433a31
3
+ metadata.gz: 3840cff6b0d67d1daba2cf2a74231d2c8655311e
4
+ data.tar.gz: 9057b69eeef323940d40e0f127abe91cc902e949
5
5
  SHA512:
6
- metadata.gz: 1844ba52a93f9cdf1d5c3d6b2ecc7b8516ef8678f32771499fdb3c3c52785b3c5ed2dd3d9b1215e38256308ad19a58bc4e85d05cd87eab7cb9e92ed9c4586ac9
7
- data.tar.gz: bc1a32af222d5555c8b7d1fe6cac660df64a512dee62c6ce7fd6d8995b7abd29bea28169edfbbe5dc8a6c17b5b81399143e57100eb73f6d85ac699aaf31ec632
6
+ metadata.gz: 64b511ad2f1a37c491a0e3d805c0053130dfeceb9e19874a8a3a981b175840098abbb529d9a89881b466ed15bd4364eeecfc9be04b3aefa6b8d05b7851f798d4
7
+ data.tar.gz: 9b4708166f703fb4ebc34563073dd846e593fe87b2a17716ee5b76f1a0d60800caf1efcd1a75f94f869d381b448ba4123a0fae54de26f351720cbcef933b30bf
data/README.md CHANGED
@@ -30,6 +30,19 @@ simply pass it as another argument, for example:
30
30
 
31
31
  CurrencyRate.convert('Bitstamp', amount: 1000, from: 'USD', to: 'EUR', anchor_currency: 'BTC')
32
32
 
33
+ You can also use `#get` method to get just the rate. It basically works as `#convert`, but only returns the rate and the method signature (attributes passed) is different - attributes are not named:
34
+
35
+ CurrencyRate.get('Bitstamp', 'BTC', 'USD') # => 750
36
+ CurrencyRate.get('Bitstamp', 'USD', 'BTC') # => 0.001818182
37
+
38
+ In the second case, we are trying to get the exchange rate for USD/BTC pair, which literally means
39
+ "how much in BTC would 1 USD be?". If the source (Bitstamp, in this case) doesn't provide the
40
+ inverted rate, the Adapter inverts the rate itself using the exchange rate for BTC/USD.
41
+
42
+ You can also using `anchor_currency` with `#get`:
43
+
44
+ CurrencyRate.get('Bitstamp', 'USD', 'EUR', anchor_currency: 'BTC')
45
+
33
46
  For a list of available adapters, please see
34
47
  [lib/btc_adapters](https://github.com/snitko/currency-rate/tree/master/lib/btc_adapters)
35
48
  and [lib/fiat_adapters](https://github.com/snitko/currency-rate/tree/master/lib/fiat_adapters).
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.1.1
1
+ 0.2.0
@@ -2,16 +2,16 @@
2
2
  # DO NOT EDIT THIS FILE DIRECTLY
3
3
  # Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec'
4
4
  # -*- encoding: utf-8 -*-
5
- # stub: currency-rate 0.1.1 ruby lib
5
+ # stub: currency-rate 0.2.0 ruby lib
6
6
 
7
7
  Gem::Specification.new do |s|
8
8
  s.name = "currency-rate"
9
- s.version = "0.1.1"
9
+ s.version = "0.2.0"
10
10
 
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 = "2016-06-18"
14
+ s.date = "2016-06-20"
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 = [
@@ -31,10 +31,13 @@ Gem::Specification.new do |s|
31
31
  "lib/adapter.rb",
32
32
  "lib/btc_adapter.rb",
33
33
  "lib/btc_adapters/average_rate_adapter.rb",
34
+ "lib/btc_adapters/bitfinex_adapter.rb",
34
35
  "lib/btc_adapters/bitpay_adapter.rb",
35
36
  "lib/btc_adapters/bitstamp_adapter.rb",
37
+ "lib/btc_adapters/btcchina_adapter.rb",
36
38
  "lib/btc_adapters/btce_adapter.rb",
37
39
  "lib/btc_adapters/coinbase_adapter.rb",
40
+ "lib/btc_adapters/huobi_adapter.rb",
38
41
  "lib/btc_adapters/kraken_adapter.rb",
39
42
  "lib/btc_adapters/localbitcoins_adapter.rb",
40
43
  "lib/btc_adapters/okcoin_adapter.rb",
@@ -46,12 +49,28 @@ Gem::Specification.new do |s|
46
49
  "lib/fiat_adapters/yahoo_adapter.rb",
47
50
  "lib/storage.rb",
48
51
  "spec/currency_rate_spec.rb",
52
+ "spec/fixtures/vcr/exchange_rate_adapters/btc_adapters/average_rate_adapter.yml",
53
+ "spec/fixtures/vcr/exchange_rate_adapters/btc_adapters/bitfinex_adapter.yml",
54
+ "spec/fixtures/vcr/exchange_rate_adapters/btc_adapters/bitpay_adapter_yml.yml",
55
+ "spec/fixtures/vcr/exchange_rate_adapters/btc_adapters/bitstamp_adapter.yml",
56
+ "spec/fixtures/vcr/exchange_rate_adapters/btc_adapters/btcchina_adapter.yml",
57
+ "spec/fixtures/vcr/exchange_rate_adapters/btc_adapters/btce_adapter.yml",
58
+ "spec/fixtures/vcr/exchange_rate_adapters/btc_adapters/coinbase_adapter.yml",
59
+ "spec/fixtures/vcr/exchange_rate_adapters/btc_adapters/huobi_adapter.yml",
60
+ "spec/fixtures/vcr/exchange_rate_adapters/btc_adapters/kraken_adapter.yml",
61
+ "spec/fixtures/vcr/exchange_rate_adapters/btc_adapters/localbitcoins_adapter.yml",
62
+ "spec/fixtures/vcr/exchange_rate_adapters/btc_adapters/okcoin_adapter.yml",
63
+ "spec/fixtures/vcr/exchange_rate_adapters/fiat_adapters/fixer_adapter.yml",
64
+ "spec/fixtures/vcr/exchange_rate_adapters/fiat_adapters/yahoo_adapter.yml",
49
65
  "spec/lib/btc_adapter_spec.rb",
50
66
  "spec/lib/btc_adapters/average_rate_adapter_spec.rb",
67
+ "spec/lib/btc_adapters/bitfinex_adapter_spec.rb",
51
68
  "spec/lib/btc_adapters/bitpay_adapter_spec.rb",
52
69
  "spec/lib/btc_adapters/bitstamp_adapter_spec.rb",
70
+ "spec/lib/btc_adapters/btcchina_adapter_spec.rb",
53
71
  "spec/lib/btc_adapters/btce_adapter_spec.rb",
54
72
  "spec/lib/btc_adapters/coinbase_adapter_spec.rb",
73
+ "spec/lib/btc_adapters/huobi_adapter_spec.rb",
55
74
  "spec/lib/btc_adapters/kraken_adapter_spec.rb",
56
75
  "spec/lib/btc_adapters/localbitcoins_adapter_spec.rb",
57
76
  "spec/lib/btc_adapters/okcoin_adapter_spec.rb",
data/lib/adapter.rb CHANGED
@@ -2,8 +2,8 @@ module CurrencyRate
2
2
  class Adapter
3
3
  include Singleton
4
4
 
5
- class FetchingFailed < Exception; end
6
- class CurrencyNotSupported < Exception; end
5
+ class FetchingFailed < Exception; end
6
+ class CurrencyNotSupported < Exception; end
7
7
 
8
8
  def initialize
9
9
  @storage = Storage.new
@@ -11,26 +11,34 @@ module CurrencyRate
11
11
 
12
12
  def fetch_rates!
13
13
  raise "FETCH_URL is not defined!" unless self.class::FETCH_URL
14
- uri = URI.parse(self.class::FETCH_URL)
15
14
  begin
16
- @rates = JSON.parse(uri.read(read_timeout: 4))
15
+ if self.class::FETCH_URL.kind_of?(Hash)
16
+ @rates = {}
17
+ self.class::FETCH_URL.each do |name, url|
18
+ uri = URI.parse(url)
19
+ @rates[name] = JSON.parse(uri.read(read_timeout: 4))
20
+ end
21
+ else
22
+ uri = URI.parse(self.class::FETCH_URL)
23
+ @rates = JSON.parse(uri.read(read_timeout: 4))
24
+ end
17
25
  @rates_updated_at = Time.now
18
26
  rescue OpenURI::HTTPError => e
19
27
  raise FetchingFailed
20
28
  end
21
29
  end
22
30
 
23
- def rate_for(currency_code)
31
+ def rate_for(from,to)
24
32
  @storage.fetch(self.class.to_s) do
25
33
  self.fetch_rates!
26
34
  end
27
35
  nil # this should be changed in descendant classes
28
36
  end
29
37
 
30
- # This method will get value we are interested in from hash and
31
- # prevent failing with 'undefined method [] for Nil' if at some point hash doesn't have such key value pair
32
- def get_rate_value_from_hash(rates_hash, *keys)
33
- rates_hash.deep_get(*keys) || raise(CurrencyNotSupported)
38
+ # Must be implemented inside every descendant class
39
+ # because storage format may be completely different
40
+ def get_rate_value_from_source(rates_hash, *keys)
41
+ raise "Please implement this method in your Adapter"
34
42
  end
35
43
 
36
44
  # We dont want to have false positive rate, because nil.to_f is 0.0
data/lib/btc_adapter.rb CHANGED
@@ -2,14 +2,12 @@ module CurrencyRate
2
2
 
3
3
  class BtcAdapter < Adapter
4
4
 
5
- def convert_from_currency(amount_in_currency, btc_denomination: :satoshi, currency: 'USD')
6
- btc_amount = amount_in_currency.to_f/rate_for(currency)
7
- Satoshi.new(btc_amount, from_unit: :btc, to_unit: btc_denomination).to_unit
5
+ def rate_for(from,to)
6
+ super
8
7
  end
9
8
 
10
- def convert_to_currency(amount, btc_denomination: :satoshi, currency: 'USD')
11
- amount_in_btc = Satoshi.new(amount.to_f, from_unit: btc_denomination).to_btc
12
- amount_in_btc*rate_for(currency)
9
+ def invert_rate(from,to,rate)
10
+ to == 'BTC' ? 1/rate.to_f : rate
13
11
  end
14
12
 
15
13
  end
@@ -20,11 +20,11 @@ module CurrencyRate
20
20
  end
21
21
  end
22
22
 
23
- def rate_for(currency_code)
23
+ def rate_for(from, to)
24
24
  rates = []
25
25
  @adapters.each do |adapter|
26
26
  begin
27
- rates << adapter.rate_for(currency_code)
27
+ rates << adapter.rate_for(from,to)
28
28
  rescue CurrencyNotSupported
29
29
  rates << nil
30
30
  end
@@ -32,7 +32,8 @@ module CurrencyRate
32
32
 
33
33
  unless rates.select(&:nil?).size == @adapters.size
34
34
  rates.compact!
35
- rates.inject {|sum, rate| sum + rate} / rates.size
35
+ rate = rates.inject {|sum, rate| sum + rate} / rates.size
36
+ invert_rate(from,to, rate)
36
37
  else
37
38
  raise CurrencyNotSupported
38
39
  end
@@ -0,0 +1,30 @@
1
+ module CurrencyRate
2
+ class BitfinexAdapter < BtcAdapter
3
+
4
+ FETCH_URL = {
5
+ 'btc_usd' => 'https://api.bitfinex.com/v1/pubticker/btcusd',
6
+ 'ltc_usd' => 'https://api.bitfinex.com/v1/pubticker/ltcusd'
7
+ }
8
+
9
+ def rate_for(from,to)
10
+ super
11
+ rate = rate_to_f(currency_pair_rate(to,from))
12
+ invert_rate(from,to,rate)
13
+ end
14
+
15
+ def currency_pair_rate(currency1, currency2)
16
+ rate = @rates["#{currency1.downcase}_#{currency2.downcase}"] || @rates["#{currency2.downcase}_#{currency1.downcase}"]
17
+ raise CurrencyNotSupported unless rate
18
+ rate['last_price']
19
+ end
20
+
21
+ def invert_rate(from,to,rate)
22
+ if ['BTC', 'LTC'].include?(to)
23
+ 1/rate.to_f
24
+ else
25
+ rate
26
+ end
27
+ end
28
+
29
+ end
30
+ end
@@ -3,16 +3,23 @@ module CurrencyRate
3
3
 
4
4
  FETCH_URL = 'https://bitpay.com/api/rates'
5
5
 
6
- def rate_for(currency_code)
6
+ def rate_for(from,to)
7
7
  super
8
+ rate = nil
8
9
  @rates.each do |rt|
9
- if rt['code'] == currency_code
10
- rate = get_rate_value_from_hash(rt, 'rate')
10
+ if rt['code'] == to
11
+ rate = invert_rate(from,to, currency_pair_rate(to,from,rt))
11
12
  return rate_to_f(rate)
12
13
  end
13
14
  end
14
15
  raise CurrencyNotSupported
15
16
  end
16
17
 
18
+ def currency_pair_rate(currency1, currency2, rt)
19
+ rate = rt['rate'] if [currency1, currency2].include?(rt['code'])
20
+ raise CurrencyNotSupported if !rate || !([currency1, currency2].include?('BTC'))
21
+ rate
22
+ end
23
+
17
24
  end
18
25
  end
@@ -1,13 +1,32 @@
1
1
  module CurrencyRate
2
2
  class BitstampAdapter < BtcAdapter
3
3
 
4
- FETCH_URL = 'https://www.bitstamp.net/api/ticker/'
4
+ FETCH_URL = {
5
+ 'btc_usd' => 'https://www.bitstamp.net/api/v2/ticker/btcusd/',
6
+ 'btc_eur' => 'https://www.bitstamp.net/api/v2/ticker/btceur/',
7
+ 'eur_usd' => 'https://www.bitstamp.net/api/v2/ticker/eurusd/'
8
+ }
5
9
 
6
- def rate_for(currency_code)
10
+ def rate_for(from,to)
11
+ raise CurrencyNotSupported unless ["BTC", "USD", "EUR"].include?(to) && ["BTC", "USD", "EUR"].include?(from)
7
12
  super
8
- raise CurrencyNotSupported if currency_code != 'USD'
9
- rate = get_rate_value_from_hash(@rates, "last")
10
- rate_to_f(rate)
13
+ rate = rate_to_f(currency_pair_rate(to,from))
14
+ invert_rate(from,to,rate)
15
+ end
16
+
17
+ # Because Bitstamp has USD/EUR pair
18
+ def invert_rate(from,to,rate)
19
+ if to == 'BTC' || (from == 'USD' && to == 'EUR')
20
+ 1/rate.to_f
21
+ else
22
+ rate
23
+ end
24
+ end
25
+
26
+ def currency_pair_rate(currency1, currency2)
27
+ rate = @rates["#{currency1.downcase}_#{currency2.downcase}"] || @rates["#{currency2.downcase}_#{currency1.downcase}"]
28
+ raise CurrencyNotSupported unless rate
29
+ rate['last']
11
30
  end
12
31
 
13
32
  end
@@ -0,0 +1,13 @@
1
+ module CurrencyRate
2
+ class BTCChinaAdapter < BtcAdapter
3
+
4
+ FETCH_URL = 'https://data.btcchina.com/data/ticker'
5
+
6
+ def rate_for(from,to)
7
+ super
8
+ rate = rate_to_f(@rates['ticker']['last'])
9
+ invert_rate(from,to,rate)
10
+ end
11
+
12
+ end
13
+ end
@@ -1,13 +1,34 @@
1
1
  module CurrencyRate
2
2
  class BtceAdapter < BtcAdapter
3
3
 
4
- FETCH_URL = 'https://btc-e.com/api/2/btc_usd/ticker'
4
+ FETCH_URL = {
5
+ 'btc_usd' => 'https://btc-e.com/api/2/btc_usd/ticker',
6
+ 'btc_eur' => 'https://btc-e.com/api/2/btc_eur/ticker',
7
+ 'btc_rub' => 'https://btc-e.com/api/2/btc_rur/ticker',
8
+ 'usd_rub' => 'https://btc-e.com/api/2/usd_rur/ticker',
9
+ 'eur_rub' => 'https://btc-e.com/api/2/eur_rur/ticker'
10
+ }
5
11
 
6
- def rate_for(currency_code)
12
+
13
+ def rate_for(from,to)
14
+ raise CurrencyNotSupported unless ["BTC", "USD", "EUR", "RUB"].include?(to) && ["BTC", "USD", "EUR", "RUB"].include?(from)
7
15
  super
8
- raise CurrencyNotSupported if !FETCH_URL.include?("btc_#{currency_code.downcase}")
9
- rate = get_rate_value_from_hash(@rates, 'ticker', 'last')
10
- rate_to_f(rate)
16
+ rate = rate_to_f(currency_pair_rate(to,from))
17
+ invert_rate(from,to,rate)
18
+ end
19
+
20
+ def currency_pair_rate(currency1, currency2)
21
+ rate = @rates["#{currency1.downcase}_#{currency2.downcase}"] || @rates["#{currency2.downcase}_#{currency1.downcase}"]
22
+ raise CurrencyNotSupported unless rate
23
+ rate['ticker']['last']
24
+ end
25
+
26
+ def invert_rate(from,to,rate)
27
+ if to == 'BTC' || (from == 'RUB' && to == 'USD') || (from == 'RUB' && to == 'EUR')
28
+ 1/rate.to_f
29
+ else
30
+ rate
31
+ end
11
32
  end
12
33
 
13
34
  end
@@ -3,9 +3,9 @@ module CurrencyRate
3
3
 
4
4
  FETCH_URL = 'https://coinbase.com/api/v1/currencies/exchange_rates'
5
5
 
6
- def rate_for(currency_code)
6
+ def rate_for(from,to)
7
7
  super
8
- rate = get_rate_value_from_hash(@rates, "btc_to_#{currency_code.downcase}")
8
+ rate = @rates["#{from.downcase}_to_#{to.downcase}"]
9
9
  rate_to_f(rate)
10
10
  end
11
11
 
@@ -0,0 +1,30 @@
1
+ module CurrencyRate
2
+ class HuobiAdapter < BtcAdapter
3
+
4
+ FETCH_URL = {
5
+ 'btc_cny' => 'http://api.huobi.com/staticmarket/ticker_btc_json.js',
6
+ 'ltc_cny' => 'http://api.huobi.com/staticmarket/ticker_ltc_json.js'
7
+ }
8
+
9
+ def rate_for(from,to)
10
+ super
11
+ rate = rate_to_f(currency_pair_rate(to,from))
12
+ invert_rate(from,to,rate)
13
+ end
14
+
15
+ def currency_pair_rate(currency1, currency2)
16
+ rate = @rates["#{currency1.downcase}_#{currency2.downcase}"] || @rates["#{currency2.downcase}_#{currency1.downcase}"]
17
+ raise CurrencyNotSupported unless rate
18
+ rate["ticker"]['last']
19
+ end
20
+
21
+ def invert_rate(from,to,rate)
22
+ if ['BTC', 'LTC'].include?(to)
23
+ 1/rate.to_f
24
+ else
25
+ rate
26
+ end
27
+ end
28
+
29
+ end
30
+ end
@@ -1,13 +1,22 @@
1
1
  module CurrencyRate
2
2
  class KrakenAdapter < BtcAdapter
3
3
 
4
- FETCH_URL = 'https://api.kraken.com/0/public/Ticker?pair=xbtusd'
4
+ FETCH_URL = {
5
+ "usd_btc" => 'https://api.kraken.com/0/public/Ticker?pair=xbtusd',
6
+ "eur_btc" => 'https://api.kraken.com/0/public/Ticker?pair=xbteur'
7
+ }
5
8
 
6
- def rate_for(currency_code)
9
+ def rate_for(to,from)
7
10
  super
8
- rate = get_rate_value_from_hash(@rates, 'result', 'XXBTZ' + currency_code.upcase, 'c')
9
- rate = rate.kind_of?(Array) ? rate.first : raise(CurrencyNotSupported)
10
- rate_to_f(rate)
11
+ rate = rate_to_f(currency_pair_rate(to,from))
12
+ invert_rate(to,from,rate)
13
+ end
14
+
15
+ def currency_pair_rate(currency1, currency2)
16
+ rate = @rates["#{currency1.downcase}_#{currency2.downcase}"] || @rates["#{currency2.downcase}_#{currency1.downcase}"]
17
+ raise CurrencyNotSupported unless rate || [currency1, currency2].include?("BTC")
18
+ currency = currency1 == "BTC" ? currency2 : currency1
19
+ rate['result']["XXBTZ#{currency}"]['c'].first
11
20
  end
12
21
 
13
22
  end
@@ -3,10 +3,17 @@ module CurrencyRate
3
3
 
4
4
  FETCH_URL = 'https://localbitcoins.com/bitcoinaverage/ticker-all-currencies/'
5
5
 
6
- def rate_for(currency_code)
6
+ def rate_for(from,to)
7
7
  super
8
- rate = get_rate_value_from_hash(@rates, currency_code.upcase, 'rates', 'last')
9
- rate_to_f(rate)
8
+ rate = currency_pair_rate(to,from)
9
+ rate = rate_to_f(rate)
10
+ invert_rate(from,to,rate)
11
+ end
12
+
13
+ def currency_pair_rate(currency1, currency2)
14
+ rate = @rates[currency1] || @rates[currency2]
15
+ raise CurrencyNotSupported if !rate || !([currency1, currency2].include?('BTC'))
16
+ rate['rates']['last']
10
17
  end
11
18
 
12
19
  end