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 CHANGED
@@ -1,3 +1,8 @@
1
1
  ## v0.0.1
2
2
 
3
3
  * initial release
4
+
5
+ ## v0.0.2
6
+
7
+ * Added support for exchanging all currencies between them
8
+
data/README.md CHANGED
@@ -1,5 +1,4 @@
1
- # NationalBankOfRomania
2
-
1
+ # NationalBankOfRomania [![Gem Version](https://badge.fury.io/rb/national_bank_of_romania.png)](http://badge.fury.io/rb/national_bank_of_romania) [![Code Climate](https://codeclimate.com/badge.png)](https://codeclimate.com/github/suciuvlad/national-bank-of-romania) [![Build Status](https://travis-ci.org/suciuvlad/national-bank-of-romania.png?branch=master)](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
@@ -1 +1,5 @@
1
1
  require "bundler/gem_tasks"
2
+ require 'rspec/core/rake_task'
3
+
4
+ task :default => :spec
5
+ RSpec::Core::RakeTask.new
data/lib/bank.rb CHANGED
@@ -22,10 +22,37 @@ module NationalBankOfRomania
22
22
  @last_updated = Time.now
23
23
  end
24
24
 
25
- def exchange(cents, from_currency, other_currency)
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
- other_currency = Money::Currency.wrap(other_currency)
28
- exchange_with(from_currency, other_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
@@ -1,3 +1,3 @@
1
1
  module NationalBankOfRomania
2
- VERSION = "0.0.1"
2
+ VERSION = "0.0.2"
3
3
  end
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
- it "should return the correct exchange rates using exchange" do
54
- @bank.update_rates(@feed)
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
- NationalBankOfRomania::Bank::CURRENCIES.each do |currency|
57
- subunit = Money::Currency.wrap(currency).subunit_to_unit.to_s.scan(/0/).count
58
- value = BigDecimal.new(@exchange_rates["currencies"][currency].to_s).truncate(subunit.to_i).to_f
59
- @bank.exchange(100, "RON", currency).to_f.should == value
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
 
@@ -29,3 +29,6 @@ currencies:
29
29
  XAU: 175.6294
30
30
  XDR: 4.9900
31
31
  ZAR: 0.3697
32
+ mixed_currencies:
33
+ EGP_TO_MDL: 1.83
34
+ EUR_TO_USD: 1.33
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.1
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-20 00:00:00.000000000 Z
12
+ date: 2013-01-22 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: nokogiri