money-open-exchange-rates 0.0.4 → 0.0.5

Sign up to get free protection for your applications and to get access to all the features.
@@ -25,7 +25,9 @@ class Money
25
25
 
26
26
  def save_rates
27
27
  raise InvalidCache unless cache
28
- open(cache, 'w').write(open(OER_URL).read)
28
+ open(cache, 'w') do |f|
29
+ f.write(open(OER_URL).read)
30
+ end
29
31
  rescue Errno::ENOENT
30
32
  raise InvalidCache
31
33
  end
@@ -39,7 +41,7 @@ class Money
39
41
  unless rate
40
42
  from_base_rate = get_rate("USD", from.currency)
41
43
  to_base_rate = get_rate("USD", to_currency)
42
- rate = to_base_rate / from_base_rate
44
+ rate = to_base_rate.to_f / from_base_rate.to_f
43
45
  end
44
46
  Money.new(((Money::Currency.wrap(to_currency).subunit_to_unit.to_f / from.currency.subunit_to_unit.to_f) * from.cents * rate).round, to_currency)
45
47
  end
@@ -24,7 +24,7 @@ describe Money::Bank::OpenExchangeRatesBank do
24
24
  @bank.oer_rates.keys.each do |currency|
25
25
  next unless Money::Currency.find(currency)
26
26
  subunit = Money::Currency.wrap(currency).subunit_to_unit
27
- @bank.exchange(100, "USD", currency).cents.must_equal (@bank.oer_rates[currency].to_f * subunit).round
27
+ @bank.exchange(100, "USD", currency).cents.must_equal((@bank.oer_rates[currency].to_f * subunit).round)
28
28
  end
29
29
  end
30
30
 
@@ -33,12 +33,28 @@ describe Money::Bank::OpenExchangeRatesBank do
33
33
  @bank.oer_rates.keys.each do |currency|
34
34
  next unless Money::Currency.find(currency)
35
35
  subunit = Money::Currency.wrap(currency).subunit_to_unit
36
- @bank.exchange_with(Money.new(100, "USD"), currency).cents.must_equal (@bank.oer_rates[currency].to_f * subunit).round
37
- @bank.exchange_with(1.to_money("USD"), currency).cents.must_equal (@bank.oer_rates[currency].to_f * subunit).round
36
+ @bank.exchange_with(Money.new(100, "USD"), currency).cents.must_equal((@bank.oer_rates[currency].to_f * subunit).round)
37
+ @bank.exchange_with(1.to_money("USD"), currency).cents.must_equal((@bank.oer_rates[currency].to_f * subunit).round)
38
38
  end
39
39
  @bank.exchange_with(5000.to_money('JPY'), 'USD').cents.must_equal 6441
40
40
  end
41
41
 
42
+ it "should not return 0 with integer rate" do
43
+ Money::Currency::TABLE[:wtf] = {
44
+ :priority => 1,
45
+ :iso_code => "WTF",
46
+ :name => "WTF",
47
+ :symbol => "WTF",
48
+ :subunit => "Cent",
49
+ :subunit_to_unit => 1000,
50
+ :separator => ".",
51
+ :delimiter => ","
52
+ }
53
+ Money::Currency::STRINGIFIED_KEYS << 'wtf'
54
+ @bank.add_rate("USD", "WTF", 2)
55
+ @bank.exchange_with(5000.to_money('WTF'), 'USD').cents.wont_equal 0
56
+ end
57
+
42
58
  # in response to #4
43
59
  it "should exchange btc" do
44
60
  Money::Currency::TABLE[:btc] = {
@@ -51,6 +67,7 @@ describe Money::Bank::OpenExchangeRatesBank do
51
67
  :separator => ".",
52
68
  :delimiter => ","
53
69
  }
70
+ Money::Currency::STRINGIFIED_KEYS << 'btc'
54
71
  @bank.add_rate("USD", "BTC", 1 / 13.7603)
55
72
  @bank.add_rate("BTC", "USD", 13.7603)
56
73
  @bank.exchange(100, "BTC", "USD").cents.must_equal 138
@@ -94,4 +111,27 @@ describe Money::Bank::OpenExchangeRatesBank do
94
111
  end
95
112
  end
96
113
 
114
+ describe 'save rates' do
115
+ include RR::Adapters::TestUnit
116
+
117
+ before do
118
+ @bank = Money::Bank::OpenExchangeRatesBank.new
119
+ @temp_cache_path = File.expand_path(File.join(File.dirname(__FILE__), 'tmp.json'))
120
+ @bank.cache = @temp_cache_path
121
+ stub(OpenURI::OpenRead).open(Money::Bank::OpenExchangeRatesBank::OER_URL) { File.read @cache_path }
122
+ @bank.save_rates
123
+ end
124
+
125
+ it 'should allow update after save' do
126
+ begin
127
+ @bank.update_rates
128
+ rescue
129
+ assert false, "Should allow updating after saving"
130
+ end
131
+ end
132
+
133
+ after do
134
+ File.delete @temp_cache_path
135
+ end
136
+ end
97
137
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: money-open-exchange-rates
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.4
4
+ version: 0.0.5
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,11 +9,11 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2011-12-11 00:00:00.000000000 Z
12
+ date: 2012-03-29 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: yajl-ruby
16
- requirement: &20494700 !ruby/object:Gem::Requirement
16
+ requirement: &13990160 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ! '>='
@@ -21,10 +21,10 @@ dependencies:
21
21
  version: 0.8.3
22
22
  type: :runtime
23
23
  prerelease: false
24
- version_requirements: *20494700
24
+ version_requirements: *13990160
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: money
27
- requirement: &20492640 !ruby/object:Gem::Requirement
27
+ requirement: &13989400 !ruby/object:Gem::Requirement
28
28
  none: false
29
29
  requirements:
30
30
  - - ! '>='
@@ -32,10 +32,10 @@ dependencies:
32
32
  version: 3.7.1
33
33
  type: :runtime
34
34
  prerelease: false
35
- version_requirements: *20492640
35
+ version_requirements: *13989400
36
36
  - !ruby/object:Gem::Dependency
37
37
  name: minitest
38
- requirement: &20491460 !ruby/object:Gem::Requirement
38
+ requirement: &13988080 !ruby/object:Gem::Requirement
39
39
  none: false
40
40
  requirements:
41
41
  - - ! '>='
@@ -43,10 +43,10 @@ dependencies:
43
43
  version: '2.0'
44
44
  type: :development
45
45
  prerelease: false
46
- version_requirements: *20491460
46
+ version_requirements: *13988080
47
47
  - !ruby/object:Gem::Dependency
48
48
  name: rr
49
- requirement: &20490720 !ruby/object:Gem::Requirement
49
+ requirement: &13987380 !ruby/object:Gem::Requirement
50
50
  none: false
51
51
  requirements:
52
52
  - - ! '>='
@@ -54,7 +54,7 @@ dependencies:
54
54
  version: 1.0.4
55
55
  type: :development
56
56
  prerelease: false
57
- version_requirements: *20490720
57
+ version_requirements: *13987380
58
58
  description: A gem that calculates the exchange rate using published rates from open-exchange-rates.
59
59
  Compatible with the money gem.
60
60
  email: laurent@spkdev.net
@@ -90,10 +90,9 @@ required_rubygems_version: !ruby/object:Gem::Requirement
90
90
  version: '0'
91
91
  requirements: []
92
92
  rubyforge_project:
93
- rubygems_version: 1.7.2
93
+ rubygems_version: 1.8.11
94
94
  signing_key:
95
95
  specification_version: 3
96
96
  summary: A gem that calculates the exchange rate using published rates from open-exchange-rates.
97
97
  test_files:
98
98
  - test/open_exchange_rates_bank_test.rb
99
- has_rdoc: