currency-rate 0.2.1 → 0.2.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: e0b5e77d6b38d2c35b7571ad074be82db677be4c
4
- data.tar.gz: 8a30a9a49e696c626435ccc290c80eca6995350a
3
+ metadata.gz: f13270a16a551a2dd99d249aa2d17d6c001383a7
4
+ data.tar.gz: 2a8ef8d2c5ecadbbc80d21dd0446a5c447de338b
5
5
  SHA512:
6
- metadata.gz: b0fdd7232a2fbb6721adbe444a9a90beb1b197d348ee4d08e435b9f32e1984f4a6c86d46fdb2e09dbcdcc54f384c7c375c0ff98d074f4c7f8dbb6d86b6992a0e
7
- data.tar.gz: e0fca9cf058bfc219ac771bc89759ace677e9f4da365103ec4f239ea06b5e5cffb20fa337ff1125cd9f491c6b6cdb7a7096743a9973af8ec1f59b8dbd6942fc7
6
+ metadata.gz: 978abb25b8a76169a863739ca9b162a9f2ca4e6a798ba33e4884925f7df435917ac4d33302b032b09a56d0136387838dd7786ab08ccd601c7da6553728056e7c
7
+ data.tar.gz: 307a3d0240fb7d69895f6e1ca0b215943711e23a52927ac1f06339537949e19a09289cdfb65d71c6dfbdc781b4775f010942b338bddf5eea685cd8955d642686
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.2.1
1
+ 0.2.2
@@ -46,5 +46,19 @@ module CurrencyRate
46
46
  def rate_to_f(rate)
47
47
  rate ? rate.to_f : raise(CurrencyNotSupported)
48
48
  end
49
+
50
+
51
+ private
52
+
53
+ def _invert_rate(rate)
54
+ r = (BigDecimal.new('1')/BigDecimal.new(rate.to_s))
55
+ r = r.round(decimal_precision) if decimal_precision
56
+ r
57
+ end
58
+
59
+ def decimal_precision
60
+ nil
61
+ end
62
+
49
63
  end
50
64
  end
@@ -7,9 +7,15 @@ module CurrencyRate
7
7
  end
8
8
 
9
9
  def invert_rate(from,to,rate)
10
- to == 'BTC' ? 1/rate.to_f : rate
10
+ to == 'BTC' ? _invert_rate(rate) : rate
11
11
  end
12
12
 
13
+ private
14
+
15
+ def decimal_precision
16
+ 9
17
+ end
18
+
13
19
  end
14
20
 
15
21
  end
@@ -20,7 +20,7 @@ module CurrencyRate
20
20
 
21
21
  def invert_rate(from,to,rate)
22
22
  if ['BTC', 'LTC'].include?(to)
23
- 1/rate.to_f
23
+ _invert_rate(rate)
24
24
  else
25
25
  rate
26
26
  end
@@ -17,7 +17,7 @@ module CurrencyRate
17
17
  # Because Bitstamp has USD/EUR pair
18
18
  def invert_rate(from,to,rate)
19
19
  if to == 'BTC' || (from == 'USD' && to == 'EUR')
20
- 1/rate.to_f
20
+ _invert_rate(rate)
21
21
  else
22
22
  rate
23
23
  end
@@ -25,7 +25,7 @@ module CurrencyRate
25
25
 
26
26
  def invert_rate(from,to,rate)
27
27
  if to == 'BTC' || (from == 'RUB' && to == 'USD') || (from == 'RUB' && to == 'EUR')
28
- 1/rate.to_f
28
+ _invert_rate(rate)
29
29
  else
30
30
  rate
31
31
  end
@@ -20,7 +20,7 @@ module CurrencyRate
20
20
 
21
21
  def invert_rate(from,to,rate)
22
22
  if ['BTC', 'LTC'].include?(to)
23
- 1/rate.to_f
23
+ _invert_rate(rate)
24
24
  else
25
25
  rate
26
26
  end
@@ -23,7 +23,7 @@ module CurrencyRate
23
23
  # Because OKCoin has LTC
24
24
  def invert_rate(from,to,rate)
25
25
  if ['BTC', 'LTC'].include?(to)
26
- 1/rate.to_f
26
+ _invert_rate(rate)
27
27
  else
28
28
  rate
29
29
  end
@@ -31,13 +31,13 @@ module CurrencyRate
31
31
  else
32
32
  rate_from = get(adapter_name, anchor_currency, from)
33
33
  rate_to = get(adapter_name, anchor_currency, to )
34
- rate_to.to_f/rate_from.to_f
34
+ BigDecimal.new(rate_to.to_s)/BigDecimal.new(rate_from.to_s)
35
35
  end
36
36
  end
37
37
 
38
38
  def self.convert(adapter_name, amount:, from:, to:, anchor_currency: nil)
39
- result = amount*get(adapter_name, from, to, anchor_currency: nil)
40
- to == 'BTC' ? result : result.round(2)
39
+ result = BigDecimal.new(amount.to_s)*BigDecimal.new(get(adapter_name, from, to, anchor_currency: nil).to_s)
40
+ to == 'BTC' ? result.round(9) : result.round(2)
41
41
  end
42
42
 
43
43
  private
@@ -3,7 +3,7 @@ module CurrencyRate
3
3
  class FiatAdapter < Adapter
4
4
 
5
5
  BigDecimal.mode BigDecimal::ROUND_MODE, :banker
6
- DECIMAL_PRECISION = 2
6
+ #@@decimal_precision = 2
7
7
 
8
8
  def rate_for(from,to)
9
9
  super
@@ -11,6 +11,12 @@ module CurrencyRate
11
11
  invert_rate(from,to,rate)
12
12
  end
13
13
 
14
+ private
15
+
16
+ def decimal_precision
17
+ 2
18
+ end
19
+
14
20
  end
15
21
 
16
22
  end
@@ -11,12 +11,12 @@ module CurrencyRate
11
11
  raise CurrencyNotSupported unless rates
12
12
  rate = rates["rates"][currency1] || rates["rates"][currency2]
13
13
  raise CurrencyNotSupported unless rate
14
- rate.round(DECIMAL_PRECISION)
14
+ BigDecimal.new(rate.to_s).round(decimal_precision)
15
15
  end
16
16
 
17
17
  def invert_rate(from,to,rate)
18
18
  if (to == 'USD' || to == 'EUR')
19
- (1/rate.to_f).round(DECIMAL_PRECISION)
19
+ _invert_rate(rate)
20
20
  else
21
21
  rate
22
22
  end
@@ -22,7 +22,7 @@ module CurrencyRate
22
22
 
23
23
  def invert_rate(from,to,rate)
24
24
  if to == 'USD'
25
- (1/rate.to_f).round(DECIMAL_PRECISION)
25
+ _invert_rate(rate)
26
26
  else
27
27
  rate
28
28
  end
@@ -36,8 +36,8 @@ module CurrencyRate
36
36
  rates.find { |x| x['id'] == "#{CROSS_RATE_CURRENCY}#{currency1.upcase}" }
37
37
  end
38
38
  raise CurrencyNotSupported unless rate
39
- rate = rate['Rate'].to_f
40
- rate.round(DECIMAL_PRECISION)
39
+ rate = BigDecimal.new(rate['Rate'])
40
+ rate.round(decimal_precision)
41
41
  end
42
42
 
43
43
  end
@@ -21,7 +21,7 @@ RSpec.describe CurrencyRate::BtcAdapter do
21
21
  it "inverts currency rate when needed" do
22
22
  allow(SomeExchangeAdapter.instance).to receive(:fetch_rates!)
23
23
  @exchange_adapter = SomeExchangeAdapter.instance
24
- expect(@exchange_adapter.rate_for('USD', 'BTC')).to eq(0.0013333333333333333)
24
+ expect(@exchange_adapter.rate_for('USD', 'BTC')).to eq(0.001333333)
25
25
  end
26
26
 
27
27
 
@@ -16,9 +16,9 @@ RSpec.describe CurrencyRate::BitfinexAdapter do
16
16
 
17
17
  it "finds the rate for currency code" do
18
18
  expect(@exchange_adapter.rate_for('BTC', 'USD')).to eq(763.0)
19
- expect(@exchange_adapter.rate_for('USD', 'BTC')).to eq(0.001310615989515072)
19
+ expect(@exchange_adapter.rate_for('USD', 'BTC')).to eq(0.001310616)
20
20
  expect(@exchange_adapter.rate_for('LTC', 'USD')).to eq(5.5105)
21
- expect(@exchange_adapter.rate_for('USD', 'LTC')).to eq(0.1814717357771527)
21
+ expect(@exchange_adapter.rate_for('USD', 'LTC')).to eq(0.181471736)
22
22
  expect( -> { @exchange_adapter.rate_for('FEDcoin', 'USD') }).to raise_error(CurrencyRate::Adapter::CurrencyNotSupported)
23
23
  end
24
24
 
@@ -16,10 +16,10 @@ RSpec.describe CurrencyRate::BitstampAdapter do
16
16
 
17
17
  it "finds the rate for currency code" do
18
18
  expect(@exchange_adapter.rate_for('BTC', 'USD')).to eq(751.97)
19
- expect(@exchange_adapter.rate_for('USD', 'BTC')).to eq(0.0013298402861816295)
19
+ expect(@exchange_adapter.rate_for('USD', 'BTC')).to eq(0.00132984)
20
20
  expect(@exchange_adapter.rate_for('BTC', 'EUR')).to eq(674.5)
21
- expect(@exchange_adapter.rate_for('EUR', 'BTC')).to eq(0.0014825796886582653)
22
- expect(@exchange_adapter.rate_for('USD', 'EUR')).to eq(0.9008846687447073)
21
+ expect(@exchange_adapter.rate_for('EUR', 'BTC')).to eq(0.00148258)
22
+ expect(@exchange_adapter.rate_for('USD', 'EUR')).to eq(0.900884669)
23
23
  expect(@exchange_adapter.rate_for('EUR', 'USD')).to eq(1.11002)
24
24
  expect( -> { @exchange_adapter.rate_for('FEDcoin', 'USD') }).to raise_error(CurrencyRate::Adapter::CurrencyNotSupported)
25
25
  end
@@ -16,7 +16,7 @@ RSpec.describe CurrencyRate::BTCChinaAdapter do
16
16
 
17
17
  it "finds the rate for currency code" do
18
18
  expect(@exchange_adapter.rate_for('BTC', 'CNY')).to eq(4785.55)
19
- expect(@exchange_adapter.rate_for('CNY', 'BTC')).to eq(0.00020896239721662085)
19
+ expect(@exchange_adapter.rate_for('CNY', 'BTC')).to eq(0.000208962)
20
20
  expect( -> { @exchange_adapter.rate_for('FEDcoin', 'USD') }).to raise_error(CurrencyRate::Adapter::CurrencyNotSupported)
21
21
  end
22
22
 
@@ -16,13 +16,13 @@ RSpec.describe CurrencyRate::BtceAdapter do
16
16
 
17
17
  it "finds the rate for currency code" do
18
18
  expect(@exchange_adapter.rate_for('BTC', 'USD')).to eq(723.872)
19
- expect(@exchange_adapter.rate_for('USD', 'BTC')).to eq(0.0013814597055833075)
19
+ expect(@exchange_adapter.rate_for('USD', 'BTC')).to eq(0.00138146)
20
20
  expect(@exchange_adapter.rate_for('BTC', 'EUR')).to eq(665.022)
21
- expect(@exchange_adapter.rate_for('EUR', 'BTC')).to eq(0.0015037096517107704)
21
+ expect(@exchange_adapter.rate_for('EUR', 'BTC')).to eq(0.00150371)
22
22
  expect(@exchange_adapter.rate_for('USD', 'RUB')).to eq(60.5)
23
- expect(@exchange_adapter.rate_for('RUB', 'USD')).to eq(0.01652892561983471)
23
+ expect(@exchange_adapter.rate_for('RUB', 'USD')).to eq(0.016528926)
24
24
  expect(@exchange_adapter.rate_for('EUR', 'RUB')).to eq(65.63)
25
- expect(@exchange_adapter.rate_for('RUB', 'EUR')).to eq(0.015236934328813043)
25
+ expect(@exchange_adapter.rate_for('RUB', 'EUR')).to eq(0.015236934)
26
26
  expect( -> { @exchange_adapter.rate_for('FEDcoin', 'USD') }).to raise_error(CurrencyRate::Adapter::CurrencyNotSupported)
27
27
  end
28
28
 
@@ -16,9 +16,9 @@ RSpec.describe CurrencyRate::HuobiAdapter do
16
16
 
17
17
  it "finds the rate for currency code" do
18
18
  expect(@exchange_adapter.rate_for('BTC', 'CNY')).to eq(4791.5)
19
- expect(@exchange_adapter.rate_for('CNY', 'BTC')).to eq(0.0002087029114056141)
19
+ expect(@exchange_adapter.rate_for('CNY', 'BTC')).to eq(0.000208703)
20
20
  expect(@exchange_adapter.rate_for('LTC', 'CNY')).to eq(34.25)
21
- expect(@exchange_adapter.rate_for('CNY', 'LTC')).to eq(0.029197080291970802)
21
+ expect(@exchange_adapter.rate_for('CNY', 'LTC')).to eq(0.02919708)
22
22
  expect( -> { @exchange_adapter.rate_for('FEDcoin', 'USD') }).to raise_error(CurrencyRate::Adapter::CurrencyNotSupported)
23
23
  end
24
24
 
@@ -16,9 +16,9 @@ RSpec.describe CurrencyRate::KrakenAdapter do
16
16
 
17
17
  it "finds the rate for currency code" do
18
18
  expect(@exchange_adapter.rate_for('BTC', 'USD')).to eq(755.15)
19
- expect(@exchange_adapter.rate_for('USD', 'BTC')).to eq(0.0013242402171753957)
19
+ expect(@exchange_adapter.rate_for('USD', 'BTC')).to eq(0.00132424)
20
20
  expect(@exchange_adapter.rate_for('BTC', 'EUR')).to eq(671.215)
21
- expect(@exchange_adapter.rate_for('EUR', 'BTC')).to eq(0.0014898355966419105)
21
+ expect(@exchange_adapter.rate_for('EUR', 'BTC')).to eq(0.001489836)
22
22
  expect( -> { @exchange_adapter.rate_for('FEDcoin', 'USD') }).to raise_error(CurrencyRate::Adapter::CurrencyNotSupported)
23
23
  end
24
24
 
@@ -16,7 +16,7 @@ RSpec.describe CurrencyRate::LocalbitcoinsAdapter do
16
16
 
17
17
  it "finds the rate for currency code" do
18
18
  expect(@exchange_adapter.rate_for('BTC', 'USD')).to eq(591.95)
19
- expect(@exchange_adapter.rate_for('USD', 'BTC')).to eq(0.0016893318692457132)
19
+ expect(@exchange_adapter.rate_for('USD', 'BTC')).to eq(BigDecimal.new('0.001689332'))
20
20
  expect( -> { @exchange_adapter.rate_for('FEDcoin', 'USD') }).to raise_error(CurrencyRate::Adapter::CurrencyNotSupported)
21
21
  end
22
22
 
@@ -16,14 +16,14 @@ RSpec.describe CurrencyRate::OkcoinAdapter do
16
16
 
17
17
  it "finds the rate for currency code" do
18
18
  expect(@exchange_adapter.rate_for('BTC', 'USD')).to eq(769.05)
19
- expect(@exchange_adapter.rate_for('USD', 'BTC')).to eq(0.0013003055718093753)
19
+ expect(@exchange_adapter.rate_for('USD', 'BTC')).to eq(0.001300306)
20
20
  expect(@exchange_adapter.rate_for('BTC', 'CNY')).to eq(5113.0)
21
- expect(@exchange_adapter.rate_for('CNY', 'BTC')).to eq(0.00019557989438685703)
21
+ expect(@exchange_adapter.rate_for('CNY', 'BTC')).to eq(0.000195580)
22
22
 
23
23
  expect(@exchange_adapter.rate_for('LTC', 'USD')).to eq(5.661)
24
- expect(@exchange_adapter.rate_for('USD', 'LTC')).to eq(0.17664723547076489)
24
+ expect(@exchange_adapter.rate_for('USD', 'LTC')).to eq(0.176647235)
25
25
  expect(@exchange_adapter.rate_for('LTC', 'CNY')).to eq(37.68)
26
- expect(@exchange_adapter.rate_for('CNY', 'LTC')).to eq(0.02653927813163482)
26
+ expect(@exchange_adapter.rate_for('CNY', 'LTC')).to eq(0.026539278)
27
27
  expect( -> { @exchange_adapter.rate_for('FEDcoin', 'USD') }).to raise_error(CurrencyRate::Adapter::CurrencyNotSupported)
28
28
  end
29
29
 
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.2.1
4
+ version: 0.2.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Roman Snitko
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-06-21 00:00:00.000000000 Z
11
+ date: 2016-06-23 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: satoshi-unit