simple_currency 1.2.1 → 1.2.2

Sign up to get free protection for your applications and to get access to all the features.
data/.rvmrc CHANGED
@@ -1 +1 @@
1
- rvm --create use ruby-1.8.7@simple_currency > /dev/null
1
+ rvm --create use ruby-1.9.2@simple_currency > /dev/null
data/Gemfile CHANGED
@@ -3,7 +3,9 @@ source :gemcutter
3
3
  gem "crack"
4
4
 
5
5
  group :test do
6
+ gem "jeweler"
6
7
  gem "rspec", ">= 2.0.0.beta.22"
7
8
  gem "fakeweb"
8
9
  gem "rails"
10
+ gem "simplecov"
9
11
  end
data/Gemfile.lock CHANGED
@@ -36,7 +36,14 @@ GEM
36
36
  erubis (2.6.6)
37
37
  abstract (>= 1.0.0)
38
38
  fakeweb (1.3.0)
39
+ gemcutter (0.6.1)
40
+ git (1.2.5)
39
41
  i18n (0.4.1)
42
+ jeweler (1.4.0)
43
+ gemcutter (>= 0.1.0)
44
+ git (>= 1.2.5)
45
+ rubyforge (>= 2.0.0)
46
+ json_pure (1.4.6)
40
47
  mail (2.2.5)
41
48
  activesupport (>= 2.3.6)
42
49
  mime-types
@@ -72,6 +79,11 @@ GEM
72
79
  rspec-mocks (2.0.0.beta.22)
73
80
  rspec-core (= 2.0.0.beta.22)
74
81
  rspec-expectations (= 2.0.0.beta.22)
82
+ rubyforge (2.0.4)
83
+ json_pure (>= 1.1.7)
84
+ simplecov (0.3.6)
85
+ simplecov-html (>= 0.3.7)
86
+ simplecov-html (0.3.8)
75
87
  thor (0.14.0)
76
88
  treetop (1.4.8)
77
89
  polyglot (>= 0.3.1)
@@ -84,5 +96,7 @@ PLATFORMS
84
96
  DEPENDENCIES
85
97
  crack
86
98
  fakeweb
99
+ jeweler
87
100
  rails
88
101
  rspec (>= 2.0.0.beta.22)
102
+ simplecov
data/README.md CHANGED
@@ -1,7 +1,7 @@
1
1
  #simple_currency
2
2
 
3
3
  A really simple currency converter using XavierMedia API.
4
- Compatible with Ruby 1.8, 1.9 and JRuby.
4
+ Compatible with Ruby 1.8, 1.9, JRuby 1.5.3 and Rubinius 1.1.
5
5
 
6
6
  ##Notes
7
7
 
data/VERSION CHANGED
@@ -1 +1 @@
1
- 1.2.1
1
+ 1.2.2
@@ -70,59 +70,18 @@ module CurrencyConvertible
70
70
  def exchange(original, target, amount)
71
71
  negative = (self < 0)
72
72
 
73
- result = @exchange_date ? call_xavier_api(original, target, amount) : call_xurrency_api(original, target, amount)
74
-
75
- # Round result to 2 decimals
76
- result = sprintf("%.2f", result).to_f
73
+ # Get the result and round it to 2 decimals
74
+ result = sprintf("%.2f", call_xavier_api(original, target, amount)).to_f
77
75
 
78
76
  return -(result) if negative
79
77
  result
80
78
  end
81
79
 
82
- ##
83
- # API call strategies
84
- ##
85
-
86
- # Calls Xurrency API to perform the exchange without a specific date (assuming today)
87
- #
88
- def call_xurrency_api(original, target, amount)
89
-
90
- if amount > 999_999_999 # Xurrency API does not support numbers bigger than this
91
- amount = 1
92
- multiplier = self.abs # Save a multiplier to apply it to the result later
93
- end
94
-
95
- api_url = "http://xurrency.com/api/#{[original, target, amount].join('/')}"
96
- uri = URI.parse(api_url)
97
-
98
-
99
- retries = 10
100
- json_response = nil
101
- begin
102
- Timeout::timeout(1){
103
- json_response = uri.open.read || nil # Returns the raw response
104
- }
105
- rescue Timeout::Error
106
- retries -= 1
107
- retries > 0 ? sleep(0.42) && retry : raise
108
- end
109
-
110
- return nil unless json_response && parsed_response = Crack::JSON.parse(json_response)
111
- if parsed_response['status'] != 'ok'
112
- raise CurrencyNotFoundException if parsed_response['message'] =~ /currencies/
113
- raise parsed_response['message']
114
- end
115
-
116
- result = parsed_response['result']['value'].to_f
117
- cache_rate(original, target, (result / amount))
118
- result * (multiplier || 1)
119
- end
120
-
121
80
  # Calls Xavier API to perform the exchange with a specific date.
122
81
  # This method is called when using :at method, like this:
123
82
  #
124
83
  # 30.eur.at(1.year.ago).to_usd
125
- #
84
+
126
85
  def call_xavier_api(original, target, amount)
127
86
 
128
87
  # Check if there is any cached XML for the specified date
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{simple_currency}
8
- s.version = "1.2.1"
8
+ s.version = "1.2.2"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Oriol Gual", "Josep M. Bach", "Josep Jaume Rey"]
12
- s.date = %q{2010-10-04}
12
+ s.date = %q{2010-10-14}
13
13
  s.description = %q{A really simple currency converter using XavierMedia API. It's Ruby 1.8, 1.9 and JRuby compatible, and it also takes advantage of Rails cache when available.}
14
14
  s.email = %q{info@codegram.com}
15
15
  s.extra_rdoc_files = [
@@ -51,6 +51,11 @@ describe "SimpleCurrency" do
51
51
  2.gbp.at(the_past).to_usd.should == 3.07
52
52
  end
53
53
 
54
+ it "retries in case of timeout" do
55
+ mock_xavier_api(the_past, :timeout => 2)
56
+ 2.eur.at(the_past).to_usd.should == 2.54
57
+ end
58
+
54
59
  it "retries yesterday and two days ago if no data found (could be a holiday)" do
55
60
  mock_xavier_api(the_past, :fail => true) # It's a holiday
56
61
  mock_xavier_api(the_past - 86400, :fail => true) # It's a holiday
@@ -60,11 +65,20 @@ describe "SimpleCurrency" do
60
65
  end
61
66
 
62
67
  it "raises an error when no data available" do
63
- mock_xavier_api(the_past, :fail => true) # It's a holiday
64
- mock_xavier_api(the_past - 86400, :fail => true) # It's a holiday
65
- mock_xavier_api(the_past - 86400 - 86400, :fail => true) # It's a holiday
68
+ counter = 0
69
+ 10.times do
70
+ mock_xavier_api(the_past - counter, :fail => true) # It's a holiday
71
+ counter += 86400
72
+ end
73
+
74
+ expect {
75
+ begin
76
+ 1.usd.at(the_past).to_eur
77
+ rescue Timeout::Error
78
+ retry
79
+ end
80
+ }.to raise_error("404 Not Found")
66
81
 
67
- expect {1.usd.at(the_past).to_eur}.to raise_error("404 Not Found")
68
82
  end
69
83
 
70
84
  end
data/spec/spec_helper.rb CHANGED
@@ -2,6 +2,11 @@ $LOAD_PATH.unshift(File.dirname(__FILE__))
2
2
  $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
3
3
 
4
4
  require 'rubygems'
5
+
6
+ require 'simplecov'
7
+ SimpleCov.start do
8
+ add_group "Lib", 'lib'
9
+ end
5
10
  require 'fakeweb'
6
11
 
7
12
  require 'simple_currency'
@@ -13,16 +18,6 @@ module HelperMethods
13
18
  File.read(File.dirname(__FILE__) + "/support/#{name}")
14
19
  end
15
20
 
16
- def mock_xurrency_api(from_currency, to_currency, amount, result, options = {})
17
- args = [from_currency, to_currency, amount]
18
-
19
- response = "{\"result\":{\"value\":#{result},\"target\":\"#{to_currency}\",\"base\":\"#{from_currency}\"},\"status\":\"ok\"}"
20
-
21
- response = "{\"message\":\"#{options[:fail_with]}\", \"status\":\"fail\"\}" if options[:fail_with]
22
-
23
- FakeWeb.register_uri(:get, "http://xurrency.com/api/#{args.join('/')}", :body => response)
24
- end
25
-
26
21
  def mock_xavier_api(date, options = {})
27
22
  date = date.send(:to_date)
28
23
  args = [date.year, date.month.to_s.rjust(2, '0'), date.day.to_s.rjust(2,'0')]
@@ -30,7 +25,17 @@ module HelperMethods
30
25
  response = {:body => fixture("xavier.xml")}
31
26
  response = {:body => "No exchange rate available", :status => ["404", "Not Found"]} if options[:fail]
32
27
 
33
- FakeWeb.register_uri(:get, "http://api.finance.xaviermedia.com/api/#{args.join('/')}.xml", response)
28
+ url = "http://api.finance.xaviermedia.com/api/#{args.join('/')}.xml"
29
+ FakeWeb.register_uri(:get, url, response)
30
+
31
+ if t = options[:timeout]
32
+ uri = double('uri')
33
+ uri_open = double('uri.open')
34
+ URI.stub(:parse).with(url).and_return(uri)
35
+ uri.stub(:open).and_return(uri_open)
36
+ t.times { uri_open.should_receive(:read).once.ordered.and_raise(Timeout::Error) }
37
+ uri_open.should_receive(:read).once.ordered.and_return(response[:body])
38
+ end
34
39
  end
35
40
 
36
41
  end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: simple_currency
3
3
  version: !ruby/object:Gem::Version
4
- hash: 29
4
+ hash: 27
5
5
  prerelease: false
6
6
  segments:
7
7
  - 1
8
8
  - 2
9
- - 1
10
- version: 1.2.1
9
+ - 2
10
+ version: 1.2.2
11
11
  platform: ruby
12
12
  authors:
13
13
  - Oriol Gual
@@ -17,7 +17,7 @@ autorequire:
17
17
  bindir: bin
18
18
  cert_chain: []
19
19
 
20
- date: 2010-10-04 00:00:00 +02:00
20
+ date: 2010-10-14 00:00:00 +02:00
21
21
  default_executable:
22
22
  dependencies:
23
23
  - !ruby/object:Gem::Dependency