money-open-exchange-rates 0.0.5 → 0.0.6

Sign up to get free protection for your applications and to get access to all the features.
@@ -23,10 +23,25 @@ class Money
23
23
  end
24
24
  end
25
25
 
26
+ def read_from_url
27
+ open(OER_URL).read
28
+ end
29
+
30
+ def has_valid_rates?(text)
31
+ parsed = Yajl::Parser.parse(text)
32
+ parsed && parsed.has_key?('rates')
33
+ rescue Yajl::ParseError
34
+ false
35
+ end
36
+
37
+
26
38
  def save_rates
27
39
  raise InvalidCache unless cache
28
- open(cache, 'w') do |f|
29
- f.write(open(OER_URL).read)
40
+ new_text = read_from_url
41
+ if has_valid_rates?(new_text)
42
+ open(cache, 'w') do |f|
43
+ f.write(new_text)
44
+ end
30
45
  end
31
46
  rescue Errno::ENOENT
32
47
  raise InvalidCache
@@ -37,6 +52,7 @@ class Money
37
52
  end
38
53
 
39
54
  def exchange_with(from, to_currency)
55
+ return from if same_currency?(from.currency, to_currency)
40
56
  rate = get_rate(from.currency, to_currency)
41
57
  unless rate
42
58
  from_base_rate = get_rate("USD", from.currency)
@@ -4,6 +4,23 @@ require File.expand_path(File.join(File.dirname(__FILE__), 'test_helper'))
4
4
 
5
5
  describe Money::Bank::OpenExchangeRatesBank do
6
6
 
7
+ describe 'exchange' do
8
+ include RR::Adapters::TestUnit
9
+
10
+ before do
11
+ @bank = Money::Bank::OpenExchangeRatesBank.new
12
+ @temp_cache_path = File.expand_path(File.join(File.dirname(__FILE__), 'tmp.json'))
13
+ @bank.cache = @temp_cache_path
14
+ stub(OpenURI::OpenRead).open(Money::Bank::OpenExchangeRatesBank::OER_URL) { File.read @cache_path }
15
+ @bank.save_rates
16
+ end
17
+
18
+ it "should be able to exchange a money to its own currency even without rates" do
19
+ money = Money.new(0, "USD");
20
+ @bank.exchange_with(money, "USD").must_equal money
21
+ end
22
+ end
23
+
7
24
  describe 'update_rates' do
8
25
  before do
9
26
  @cache_path = File.expand_path(File.join(File.dirname(__FILE__), 'latest.json'))
@@ -20,7 +37,6 @@ describe Money::Bank::OpenExchangeRatesBank do
20
37
  end
21
38
 
22
39
  it "should return the correct oer rates using oer" do
23
- @bank.update_rates
24
40
  @bank.oer_rates.keys.each do |currency|
25
41
  next unless Money::Currency.find(currency)
26
42
  subunit = Money::Currency.wrap(currency).subunit_to_unit
@@ -29,7 +45,6 @@ describe Money::Bank::OpenExchangeRatesBank do
29
45
  end
30
46
 
31
47
  it "should return the correct oer rates using exchange_with" do
32
- @bank.update_rates
33
48
  @bank.oer_rates.keys.each do |currency|
34
49
  next unless Money::Currency.find(currency)
35
50
  subunit = Money::Currency.wrap(currency).subunit_to_unit
@@ -40,7 +55,7 @@ describe Money::Bank::OpenExchangeRatesBank do
40
55
  end
41
56
 
42
57
  it "should not return 0 with integer rate" do
43
- Money::Currency::TABLE[:wtf] = {
58
+ wtf = {
44
59
  :priority => 1,
45
60
  :iso_code => "WTF",
46
61
  :name => "WTF",
@@ -50,14 +65,14 @@ describe Money::Bank::OpenExchangeRatesBank do
50
65
  :separator => ".",
51
66
  :delimiter => ","
52
67
  }
53
- Money::Currency::STRINGIFIED_KEYS << 'wtf'
68
+ Money::Currency.register(wtf)
54
69
  @bank.add_rate("USD", "WTF", 2)
55
70
  @bank.exchange_with(5000.to_money('WTF'), 'USD').cents.wont_equal 0
56
71
  end
57
72
 
58
73
  # in response to #4
59
74
  it "should exchange btc" do
60
- Money::Currency::TABLE[:btc] = {
75
+ btc = {
61
76
  :priority => 1,
62
77
  :iso_code => "BTC",
63
78
  :name => "Bitcoin",
@@ -67,7 +82,7 @@ describe Money::Bank::OpenExchangeRatesBank do
67
82
  :separator => ".",
68
83
  :delimiter => ","
69
84
  }
70
- Money::Currency::STRINGIFIED_KEYS << 'btc'
85
+ Money::Currency.register(btc)
71
86
  @bank.add_rate("USD", "BTC", 1 / 13.7603)
72
87
  @bank.add_rate("BTC", "USD", 13.7603)
73
88
  @bank.exchange(100, "BTC", "USD").cents.must_equal 138
@@ -130,6 +145,27 @@ describe Money::Bank::OpenExchangeRatesBank do
130
145
  end
131
146
  end
132
147
 
148
+ it "should not break an existing file if save fails to read" do
149
+ initial_size = File.read(@temp_cache_path).size
150
+ stub(@bank).read_from_url {""}
151
+ @bank.save_rates
152
+ File.open(@temp_cache_path).read.size.must_equal initial_size
153
+ end
154
+
155
+ it "should not break an existing file if save returns json without rates" do
156
+ initial_size = File.read(@temp_cache_path).size
157
+ stub(@bank).read_from_url { %Q({"error": "An error"}) }
158
+ @bank.save_rates
159
+ File.open(@temp_cache_path).read.size.must_equal initial_size
160
+ end
161
+
162
+ it "should not break an existing file if save returns a invalid json" do
163
+ initial_size = File.read(@temp_cache_path).size
164
+ stub(@bank).read_from_url { %Q({invalid_json: "An error"}) }
165
+ @bank.save_rates
166
+ File.open(@temp_cache_path).read.size.must_equal initial_size
167
+ end
168
+
133
169
  after do
134
170
  File.delete @temp_cache_path
135
171
  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.5
4
+ version: 0.0.6
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: 2012-03-29 00:00:00.000000000 Z
12
+ date: 2012-05-08 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: yajl-ruby
16
- requirement: &13990160 !ruby/object:Gem::Requirement
16
+ requirement: &13265040 !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: *13990160
24
+ version_requirements: *13265040
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: money
27
- requirement: &13989400 !ruby/object:Gem::Requirement
27
+ requirement: &13263120 !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: *13989400
35
+ version_requirements: *13263120
36
36
  - !ruby/object:Gem::Dependency
37
37
  name: minitest
38
- requirement: &13988080 !ruby/object:Gem::Requirement
38
+ requirement: &13260700 !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: *13988080
46
+ version_requirements: *13260700
47
47
  - !ruby/object:Gem::Dependency
48
48
  name: rr
49
- requirement: &13987380 !ruby/object:Gem::Requirement
49
+ requirement: &13257140 !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: *13987380
57
+ version_requirements: *13257140
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