simple_currency 1.0.0 → 1.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.rvmrc +1 -0
- data/VERSION +1 -1
- data/lib/simple_currency/currency_convertible.rb +5 -3
- data/simple_currency.gemspec +3 -3
- data/spec/simple_currency_spec.rb +16 -2
- metadata +4 -4
- data/.document +0 -5
data/.rvmrc
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
rvm --create use ruby-1.8.7@simple_currency > /dev/null
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
1.0.
|
1
|
+
1.0.1
|
@@ -35,8 +35,10 @@ module CurrencyConvertible
|
|
35
35
|
|
36
36
|
# If not, perform a call to the api
|
37
37
|
json_response = call_api(original, target, amount)
|
38
|
-
return nil unless json_response
|
39
|
-
|
38
|
+
return nil unless json_response && parsed_response = JSON.parse(json_response)
|
39
|
+
raise parsed_response["message"] if parsed_response["status"] != "ok"
|
40
|
+
|
41
|
+
result = sprintf("%.2f", parsed_response["result"]["value"]).to_f
|
40
42
|
|
41
43
|
# Cache exchange rate for today only
|
42
44
|
Rails.cache.write("#{original}_#{target}_#{Time.now.to_a[3..5].join('-')}", calculate_rate(amount,result)) if defined?(Rails)
|
@@ -55,7 +57,7 @@ module CurrencyConvertible
|
|
55
57
|
end
|
56
58
|
|
57
59
|
def check_cache(original, target, amount)
|
58
|
-
if rate =
|
60
|
+
if rate = Rails.cache.read("#{original}_#{target}_#{Time.now.to_a[3..5].join('-')}")
|
59
61
|
result = (amount * rate).to_f
|
60
62
|
return result = (result * 100).round.to_f / 100
|
61
63
|
end
|
data/simple_currency.gemspec
CHANGED
@@ -5,11 +5,11 @@
|
|
5
5
|
|
6
6
|
Gem::Specification.new do |s|
|
7
7
|
s.name = %q{simple_currency}
|
8
|
-
s.version = "1.0.
|
8
|
+
s.version = "1.0.1"
|
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-08-
|
12
|
+
s.date = %q{2010-08-31}
|
13
13
|
s.description = %q{A really simple currency converter using the Xurrency 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 = [
|
@@ -19,9 +19,9 @@ Gem::Specification.new do |s|
|
|
19
19
|
s.files = [
|
20
20
|
".autotest",
|
21
21
|
".bundle/config",
|
22
|
-
".document",
|
23
22
|
".gitignore",
|
24
23
|
".rspec",
|
24
|
+
".rvmrc",
|
25
25
|
"Gemfile",
|
26
26
|
"Gemfile.lock",
|
27
27
|
"LICENSE",
|
@@ -2,9 +2,13 @@ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
|
|
2
2
|
|
3
3
|
describe "SimpleCurrency" do
|
4
4
|
|
5
|
-
def mock_uri(from_currency, to_currency, amount, result)
|
5
|
+
def mock_uri(from_currency, to_currency, amount, result, options = {})
|
6
6
|
args = [from_currency, to_currency, amount]
|
7
|
+
|
7
8
|
response = "{\"result\":{\"value\":#{result},\"target\":\"#{to_currency}\",\"base\":\"#{from_currency}\"},\"status\":\"ok\"}"
|
9
|
+
|
10
|
+
response = "{\"message\":\"#{options[:fail_with]}\", \"status\":\"fail\"\}" if options[:fail_with]
|
11
|
+
|
8
12
|
FakeWeb.register_uri(:get, "http://xurrency.com/api/#{args.join('/')}", :body => response)
|
9
13
|
end
|
10
14
|
|
@@ -29,6 +33,14 @@ describe "SimpleCurrency" do
|
|
29
33
|
8.should respond_to(:to_eur)
|
30
34
|
end
|
31
35
|
|
36
|
+
it "raises any error returned by the api call" do
|
37
|
+
mock_uri('usd', 'xxx', 1, 1.5, :fail_with => "The currencies are not valid")
|
38
|
+
mock_uri('usd', 'eur', 1_000_000_000, 1.5, :fail_with => "The amount should be between 0 and 999999999")
|
39
|
+
|
40
|
+
expect {1.usd.to_xxx}.to raise_error("The currencies are not valid")
|
41
|
+
expect {1_000_000_000.usd.to_eur}.to raise_error("The amount should be between 0 and 999999999")
|
42
|
+
end
|
43
|
+
|
32
44
|
context "when Rails is present" do
|
33
45
|
|
34
46
|
before(:all) do
|
@@ -50,13 +62,15 @@ describe "SimpleCurrency" do
|
|
50
62
|
it "ensures the cache is valid only for today" do
|
51
63
|
now = Time.parse('2010-08-30')
|
52
64
|
|
65
|
+
Time.stub(:now).and_return(now)
|
66
|
+
|
53
67
|
Rails.stub_chain("cache.write").and_return(true)
|
54
68
|
Rails.stub_chain("cache.read").with('usd_eur_30-8-2010').and_return(1.5)
|
55
69
|
|
56
70
|
mock_uri('usd', 'eur', 1, 1.5)
|
57
71
|
1.usd.to_eur.should == 1.5
|
58
72
|
|
59
|
-
Time.stub(:now).and_return(now + 86400) #
|
73
|
+
Time.stub(:now).and_return(now + 86400) # One day later
|
60
74
|
Rails.stub_chain("cache.read").with('usd_eur_31-8-2010').and_return(nil)
|
61
75
|
|
62
76
|
# Exchange rate has changed next day, so forget cache rate!
|
metadata
CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
|
|
5
5
|
segments:
|
6
6
|
- 1
|
7
7
|
- 0
|
8
|
-
-
|
9
|
-
version: 1.0.
|
8
|
+
- 1
|
9
|
+
version: 1.0.1
|
10
10
|
platform: ruby
|
11
11
|
authors:
|
12
12
|
- Oriol Gual
|
@@ -16,7 +16,7 @@ autorequire:
|
|
16
16
|
bindir: bin
|
17
17
|
cert_chain: []
|
18
18
|
|
19
|
-
date: 2010-08-
|
19
|
+
date: 2010-08-31 00:00:00 +02:00
|
20
20
|
default_executable:
|
21
21
|
dependencies:
|
22
22
|
- !ruby/object:Gem::Dependency
|
@@ -117,9 +117,9 @@ extra_rdoc_files:
|
|
117
117
|
files:
|
118
118
|
- .autotest
|
119
119
|
- .bundle/config
|
120
|
-
- .document
|
121
120
|
- .gitignore
|
122
121
|
- .rspec
|
122
|
+
- .rvmrc
|
123
123
|
- Gemfile
|
124
124
|
- Gemfile.lock
|
125
125
|
- LICENSE
|