national_bank_of_romania 0.0.1 → 0.0.2
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.
- data/CHANGELOG.md +5 -0
- data/README.md +1 -2
- data/Rakefile +4 -0
- data/lib/bank.rb +30 -3
- data/lib/national_bank_of_romania/version.rb +1 -1
- data/spec/bank_spec.rb +40 -6
- data/spec/fixtures/exchange_rates.yml +3 -0
- metadata +2 -2
data/CHANGELOG.md
CHANGED
data/README.md
CHANGED
@@ -1,5 +1,4 @@
|
|
1
|
-
# NationalBankOfRomania
|
2
|
-
|
1
|
+
# NationalBankOfRomania [](http://badge.fury.io/rb/national_bank_of_romania) [](https://codeclimate.com/github/suciuvlad/national-bank-of-romania) [](https://travis-ci.org/suciuvlad/national-bank-of-romania)
|
3
2
|
|
4
3
|
This gem provides the ability to download the exchange rates from the National Bank of Romania and it is compatible with the [money gem](https://github.com/RubyMoney/money/).
|
5
4
|
|
data/Rakefile
CHANGED
data/lib/bank.rb
CHANGED
@@ -22,10 +22,37 @@ module NationalBankOfRomania
|
|
22
22
|
@last_updated = Time.now
|
23
23
|
end
|
24
24
|
|
25
|
-
def
|
25
|
+
def exchange_with(from, to_currency)
|
26
|
+
return from if same_currency?(from.currency, to_currency)
|
27
|
+
|
28
|
+
unless rate = get_rate(from.currency, to_currency)
|
29
|
+
from_base_rate = BigDecimal.new(get_rate("RON", from.currency).to_s)
|
30
|
+
to_base_rate = BigDecimal.new(get_rate("RON", to_currency).to_s)
|
31
|
+
|
32
|
+
rate = from_base_rate / to_base_rate
|
33
|
+
end
|
34
|
+
_to_currency_ = Money::Currency.wrap(to_currency)
|
35
|
+
|
36
|
+
fractional = BigDecimal.new(from.fractional.to_s) /
|
37
|
+
(BigDecimal.new(from.currency.subunit_to_unit.to_s) /
|
38
|
+
BigDecimal.new(_to_currency_.subunit_to_unit.to_s))
|
39
|
+
|
40
|
+
ex = fractional * BigDecimal.new(rate.to_s)
|
41
|
+
ex = ex.to_f
|
42
|
+
ex = if block_given?
|
43
|
+
yield ex
|
44
|
+
elsif @rounding_method
|
45
|
+
@rounding_method.call(ex)
|
46
|
+
else
|
47
|
+
ex.to_s.to_i
|
48
|
+
end
|
49
|
+
Money.new(ex, _to_currency_)
|
50
|
+
end
|
51
|
+
|
52
|
+
def exchange(cents, from_currency, to_currency)
|
26
53
|
from_currency = Money.new(cents, from_currency)
|
27
|
-
|
28
|
-
exchange_with(from_currency,
|
54
|
+
to_currency = Money::Currency.wrap(to_currency)
|
55
|
+
exchange_with(from_currency, to_currency)
|
29
56
|
end
|
30
57
|
|
31
58
|
def parser
|
data/spec/bank_spec.rb
CHANGED
@@ -50,13 +50,47 @@ describe "Bank" do
|
|
50
50
|
bank2.should_not eq(bank3)
|
51
51
|
end
|
52
52
|
|
53
|
-
|
54
|
-
|
53
|
+
context "#exchange" do
|
54
|
+
it "calls 'exchange_with' with the correct params" do
|
55
|
+
@bank.update_rates(@feed)
|
56
|
+
|
57
|
+
from = Money.new(100, "RON")
|
58
|
+
to = Money::Currency.wrap("USD")
|
59
|
+
money = double(:fractional => 324, :currency => "USD")
|
60
|
+
|
61
|
+
@bank.should_receive(:exchange_with).with(from, to).and_return(money)
|
62
|
+
@bank.exchange(100, "RON", "USD")
|
63
|
+
end
|
64
|
+
|
65
|
+
it "exchanges currencies based on the base currency" do
|
66
|
+
@bank.update_rates(@feed)
|
67
|
+
|
68
|
+
@bank.exchange(100, "EGP", "MDL").to_f.should ===
|
69
|
+
@exchange_rates["mixed_currencies"]["EGP_TO_MDL"]
|
70
|
+
@bank.exchange(100, "EUR", "USD").to_f.should ===
|
71
|
+
@exchange_rates["mixed_currencies"]["EUR_TO_USD"]
|
72
|
+
end
|
73
|
+
end
|
74
|
+
|
75
|
+
context "#exchange_with" do
|
76
|
+
it "returns the same currency if the to_currency is the same" do
|
77
|
+
@bank.update_rates(@feed)
|
78
|
+
m1 = Money.new(1000, "RON")
|
79
|
+
|
80
|
+
@bank.exchange_with(m1, "RON").should == m1
|
81
|
+
end
|
55
82
|
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
83
|
+
it "returns the correct exchange rates" do
|
84
|
+
@bank.update_rates(@feed)
|
85
|
+
NationalBankOfRomania::Bank::CURRENCIES.each do |currency|
|
86
|
+
m1 = Money.new(100, "RON")
|
87
|
+
subunit = Money::Currency.wrap(currency).subunit_to_unit
|
88
|
+
.to_s.scan(/0/).count
|
89
|
+
value = BigDecimal.new(@exchange_rates["currencies"][currency].to_s)
|
90
|
+
.truncate(subunit.to_i).to_f
|
91
|
+
|
92
|
+
@bank.exchange_with(m1, currency).to_f.should == value
|
93
|
+
end
|
60
94
|
end
|
61
95
|
end
|
62
96
|
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: national_bank_of_romania
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.2
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2013-01-
|
12
|
+
date: 2013-01-22 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: nokogiri
|