exchange_rates 0.1.0 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/exchange_rates.rb +15 -2
- data/lib/exchange_rates/version.rb +1 -1
- data/spec/exchange_rates_spec.rb +8 -0
- metadata +4 -4
data/lib/exchange_rates.rb
CHANGED
@@ -12,14 +12,14 @@ class ExchangeRates
|
|
12
12
|
parse_rates
|
13
13
|
if rates = @@rates[date]
|
14
14
|
unless from_rate = rates[from]
|
15
|
-
if from ==
|
15
|
+
if from == @@base_currency
|
16
16
|
from_rate = 1.0
|
17
17
|
else
|
18
18
|
raise "Unknown 'from' currency"
|
19
19
|
end
|
20
20
|
end
|
21
21
|
unless to_rate = rates[to]
|
22
|
-
if to ==
|
22
|
+
if to == @@base_currency
|
23
23
|
to_rate = 1.0
|
24
24
|
else
|
25
25
|
raise "Unknown 'to' currency"
|
@@ -37,6 +37,7 @@ class ExchangeRates
|
|
37
37
|
# to - to currency, defaults to base_currency (typically EUR)
|
38
38
|
# date - date at which to perform conversion, defaults to current date
|
39
39
|
def self.convert(amount, opts = {})
|
40
|
+
parse_rates
|
40
41
|
options = {from: @@base_currency, to: @@base_currency, date: Date.today}.merge(opts)
|
41
42
|
amount.to_f * at(options[:date], options[:from], options[:to])
|
42
43
|
end
|
@@ -84,4 +85,16 @@ class ExchangeRates
|
|
84
85
|
@@base_currency = base
|
85
86
|
end
|
86
87
|
|
88
|
+
# Outputs an array of dates included in the current data set.
|
89
|
+
def self.available_dates
|
90
|
+
parse_rates
|
91
|
+
@@rates.keys
|
92
|
+
end
|
93
|
+
|
94
|
+
# Outputs an array of supported currencies.
|
95
|
+
def self.available_currencies
|
96
|
+
parse_rates
|
97
|
+
@@rates.first[1].keys + [@@base_currency]
|
98
|
+
end
|
99
|
+
|
87
100
|
end
|
data/spec/exchange_rates_spec.rb
CHANGED
@@ -34,6 +34,14 @@ describe ExchangeRates do
|
|
34
34
|
it "should spread data over time" do
|
35
35
|
ExchangeRates.over_time('EUR', 'CZK').should eq({Date::civil(2013, 11, 20) => 27.329, DATE1 => 27.197})
|
36
36
|
end
|
37
|
+
|
38
|
+
it "should give the list of supported currencies" do
|
39
|
+
ExchangeRates.supported_currencies.should eq(%w[USD JPY BGN CZK EUR])
|
40
|
+
end
|
41
|
+
|
42
|
+
it "should give the list of supported dates" do
|
43
|
+
ExchangeRates.supported_dates.should eq([DATE1, Date::civil(2013, 11, 20)])
|
44
|
+
end
|
37
45
|
end
|
38
46
|
|
39
47
|
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: exchange_rates
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0
|
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-11-
|
12
|
+
date: 2013-11-23 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: nokogiri
|
@@ -90,7 +90,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
90
90
|
version: '0'
|
91
91
|
segments:
|
92
92
|
- 0
|
93
|
-
hash:
|
93
|
+
hash: -3645098103329208760
|
94
94
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
95
95
|
none: false
|
96
96
|
requirements:
|
@@ -99,7 +99,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
99
99
|
version: '0'
|
100
100
|
segments:
|
101
101
|
- 0
|
102
|
-
hash:
|
102
|
+
hash: -3645098103329208760
|
103
103
|
requirements: []
|
104
104
|
rubyforge_project:
|
105
105
|
rubygems_version: 1.8.24
|