exchange_rates 0.2.1 → 0.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/.gitignore +1 -0
- data/README.md +4 -4
- data/lib/exchange_rates.rb +26 -15
- data/lib/exchange_rates/version.rb +1 -1
- data/spec/exchange_rates_spec.rb +2 -2
- metadata +14 -28
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 6f53418a5a4f780eb32abd1760186c270b96d687
|
4
|
+
data.tar.gz: 66de054b372efc9638957322a6cc738b87eb02d3
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 6c557a9a6dcdf257d7174429372e0ec01a5524f491396cd47e1d6df750ec9f3af89cd6ee00e12707efa05b2ffd903d1ef5573eeff58c8c144b368ec3bf36cf3b
|
7
|
+
data.tar.gz: 108226d20f7c576cfffcf6cb42b4c0f0a18a400a81b325d215d145910682bba7d13b681ce03085bdcc62da5489150124008040af3f1e51d0f12e0048329c7dc0
|
data/.gitignore
CHANGED
data/README.md
CHANGED
@@ -21,11 +21,11 @@ Or install it yourself as:
|
|
21
21
|
|
22
22
|
## Usage
|
23
23
|
|
24
|
-
The first task that the programmer must use is to obtain the data source. The library assumes it will be in `./exchange-rates.xml`, however the path can be set via the environment variable `EXCHANGE_RATE_FILE`, which should point to the exchange rate file. This file can be downloaded from the [ECB website](http://www.ecb.europa.eu/stats/eurofxref/eurofxref-hist-90d.xml).
|
24
|
+
The first task that the programmer must use is to obtain the data source. The library assumes it will be in `./exchange-rates.xml`, however the path can be set via the environment variable `EXCHANGE_RATE_FILE`, which should point to the exchange rate file. This file can be downloaded from the [ECB website](http://www.ecb.europa.eu/stats/eurofxref/eurofxref-hist-90d.xml).
|
25
25
|
|
26
|
-
|
27
|
-
|
28
|
-
|
26
|
+
The library will fetch this file automatically if not present, however it is your responsibility to
|
27
|
+
maintain this file as fresh as you need it. You can download a new version with
|
28
|
+
`ExchangeRates.fetch_rates!`.
|
29
29
|
|
30
30
|
Finally if you have a different datasource, you can simply parse it and call `ExchangeRates.set_rates(data, base_currency)`, where `data` will be your currency data organized by date and `base_currency` will be the currency code to which your rates are relative to. For example:
|
31
31
|
|
data/lib/exchange_rates.rb
CHANGED
@@ -1,12 +1,12 @@
|
|
1
1
|
require "exchange_rates/version"
|
2
2
|
require "nokogiri"
|
3
3
|
|
4
|
-
# Exchange Rates is responsible for parsing a locally available XML file
|
4
|
+
# Exchange Rates is responsible for parsing a locally available XML file
|
5
5
|
# listing ratios between the base_currency and target currencies. All currencies should
|
6
6
|
# be provided as strings with their 3 letter code.
|
7
7
|
class ExchangeRates
|
8
8
|
# ExchangeRates.at gives an exchange rate between two currencies at a particular
|
9
|
-
# date. Raises exceptions if the dates are out of range of the file or if the
|
9
|
+
# date. Raises exceptions if the dates are out of range of the file or if the
|
10
10
|
# currencies are unknown.
|
11
11
|
def self.at(date, from, to)
|
12
12
|
parse_rates
|
@@ -28,20 +28,20 @@ class ExchangeRates
|
|
28
28
|
to_rate / from_rate
|
29
29
|
else
|
30
30
|
raise "Date out of known dates."
|
31
|
-
end
|
31
|
+
end
|
32
32
|
end
|
33
|
-
|
33
|
+
|
34
34
|
# Converts an amount of money between currencies. The options for this method are:
|
35
35
|
#
|
36
36
|
# from - from currency, defaults to base_currency (typically EUR)
|
37
37
|
# to - to currency, defaults to base_currency (typically EUR)
|
38
|
-
# date - date at which to perform conversion, defaults to
|
38
|
+
# date - date at which to perform conversion, defaults to latest available date in dateset
|
39
39
|
def self.convert(amount, opts = {})
|
40
40
|
parse_rates
|
41
|
-
options = {from: @@base_currency, to: @@base_currency, date:
|
41
|
+
options = {from: @@base_currency, to: @@base_currency, date: @@rates.keys.sort.reverse.first}.merge(opts)
|
42
42
|
amount.to_f * at(options[:date], options[:from], options[:to])
|
43
43
|
end
|
44
|
-
|
44
|
+
|
45
45
|
# Calculates exchange rates between two currencies over all available dates.
|
46
46
|
def self.over_time(from, to)
|
47
47
|
parse_rates
|
@@ -51,9 +51,9 @@ class ExchangeRates
|
|
51
51
|
end
|
52
52
|
results
|
53
53
|
end
|
54
|
-
|
54
|
+
|
55
55
|
# Reads and parses the XML data feed providing the underlying data source.
|
56
|
-
# The data source is currently assumed to be in the format of the European
|
56
|
+
# The data source is currently assumed to be in the format of the European
|
57
57
|
# Central Bank feed accesible at <http://www.ecb.europa.eu/stats/eurofxref/eurofxrefhist90d.xml>
|
58
58
|
# which provides a 90 day history of exchange rates. It is assumed that this
|
59
59
|
# file is saved locally, best through a cron tab (see README).
|
@@ -63,18 +63,29 @@ class ExchangeRates
|
|
63
63
|
return @@rates if @@rates
|
64
64
|
@@base_currency = 'EUR'
|
65
65
|
rate_file = ENV['EXCHANGE_RATE_FILE'] || './exchange-rates.xml'
|
66
|
-
|
66
|
+
if File.exist?(rate_file)
|
67
|
+
doc = Nokogiri::XML(File.read(rate_file))
|
68
|
+
else
|
69
|
+
doc = Nokogiri::XML(fetch_rates!)
|
70
|
+
end
|
67
71
|
@@rates = {}
|
68
72
|
doc.css('Cube>Cube[time]').each do |day|
|
69
73
|
time = Date.parse day.attr('time')
|
70
74
|
@@rates[time] = {}
|
71
|
-
day.css('Cube').each{ |c|
|
75
|
+
day.css('Cube').each{ |c|
|
72
76
|
@@rates[time][c.attr('currency')] = c.attr('rate').to_f
|
73
77
|
}
|
74
78
|
end
|
75
79
|
@@rates
|
76
80
|
end
|
77
|
-
|
81
|
+
|
82
|
+
# Goes and makes a request to download latest ECB data
|
83
|
+
def self.fetch_rates!
|
84
|
+
rates = Net::HTTP.get URI.parse 'http://www.ecb.europa.eu/stats/eurofxref/eurofxref-hist-90d.xml'
|
85
|
+
File.write(ENV['EXCHANGE_RATE_FILE'] || './exchange-rates.xml', rates)
|
86
|
+
rates
|
87
|
+
end
|
88
|
+
|
78
89
|
# Set custom rates data. The data should be a hash of hashes, where the keys in the
|
79
90
|
# outer hash are Date objects and the keys in the inner hashes are three letter currency
|
80
91
|
# codes. The values are floats showing the exchange rate between a currency and the base
|
@@ -84,17 +95,17 @@ class ExchangeRates
|
|
84
95
|
@@rates = rates
|
85
96
|
@@base_currency = base
|
86
97
|
end
|
87
|
-
|
98
|
+
|
88
99
|
# Outputs an array of dates included in the current data set.
|
89
100
|
def self.available_dates
|
90
101
|
parse_rates
|
91
102
|
@@rates.keys
|
92
103
|
end
|
93
|
-
|
104
|
+
|
94
105
|
# Outputs an array of supported currencies.
|
95
106
|
def self.available_currencies
|
96
107
|
parse_rates
|
97
108
|
@@rates.first[1].keys + [@@base_currency]
|
98
109
|
end
|
99
|
-
|
110
|
+
|
100
111
|
end
|
data/spec/exchange_rates_spec.rb
CHANGED
@@ -36,11 +36,11 @@ describe ExchangeRates do
|
|
36
36
|
end
|
37
37
|
|
38
38
|
it "should give the list of supported currencies" do
|
39
|
-
ExchangeRates.
|
39
|
+
ExchangeRates.available_currencies.should eq(%w[USD JPY BGN CZK EUR])
|
40
40
|
end
|
41
41
|
|
42
42
|
it "should give the list of supported dates" do
|
43
|
-
ExchangeRates.
|
43
|
+
ExchangeRates.available_dates.should eq([DATE1, Date::civil(2013, 11, 20)])
|
44
44
|
end
|
45
45
|
end
|
46
46
|
|
metadata
CHANGED
@@ -1,62 +1,55 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: exchange_rates
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
5
|
-
prerelease:
|
4
|
+
version: 0.3.0
|
6
5
|
platform: ruby
|
7
6
|
authors:
|
8
7
|
- Jakub Hampl
|
9
8
|
autorequire:
|
10
9
|
bindir: bin
|
11
10
|
cert_chain: []
|
12
|
-
date:
|
11
|
+
date: 2015-07-01 00:00:00.000000000 Z
|
13
12
|
dependencies:
|
14
13
|
- !ruby/object:Gem::Dependency
|
15
14
|
name: nokogiri
|
16
15
|
requirement: !ruby/object:Gem::Requirement
|
17
|
-
none: false
|
18
16
|
requirements:
|
19
|
-
- -
|
17
|
+
- - ">="
|
20
18
|
- !ruby/object:Gem::Version
|
21
19
|
version: '0'
|
22
20
|
type: :runtime
|
23
21
|
prerelease: false
|
24
22
|
version_requirements: !ruby/object:Gem::Requirement
|
25
|
-
none: false
|
26
23
|
requirements:
|
27
|
-
- -
|
24
|
+
- - ">="
|
28
25
|
- !ruby/object:Gem::Version
|
29
26
|
version: '0'
|
30
27
|
- !ruby/object:Gem::Dependency
|
31
28
|
name: rspec
|
32
29
|
requirement: !ruby/object:Gem::Requirement
|
33
|
-
none: false
|
34
30
|
requirements:
|
35
|
-
- -
|
31
|
+
- - ">="
|
36
32
|
- !ruby/object:Gem::Version
|
37
33
|
version: '0'
|
38
34
|
type: :development
|
39
35
|
prerelease: false
|
40
36
|
version_requirements: !ruby/object:Gem::Requirement
|
41
|
-
none: false
|
42
37
|
requirements:
|
43
|
-
- -
|
38
|
+
- - ">="
|
44
39
|
- !ruby/object:Gem::Version
|
45
40
|
version: '0'
|
46
41
|
- !ruby/object:Gem::Dependency
|
47
42
|
name: rake
|
48
43
|
requirement: !ruby/object:Gem::Requirement
|
49
|
-
none: false
|
50
44
|
requirements:
|
51
|
-
- -
|
45
|
+
- - ">="
|
52
46
|
- !ruby/object:Gem::Version
|
53
47
|
version: '0'
|
54
48
|
type: :development
|
55
49
|
prerelease: false
|
56
50
|
version_requirements: !ruby/object:Gem::Requirement
|
57
|
-
none: false
|
58
51
|
requirements:
|
59
|
-
- -
|
52
|
+
- - ">="
|
60
53
|
- !ruby/object:Gem::Version
|
61
54
|
version: '0'
|
62
55
|
description: exchange_rates is a gem that allows currency conversion and rate history
|
@@ -67,7 +60,7 @@ executables: []
|
|
67
60
|
extensions: []
|
68
61
|
extra_rdoc_files: []
|
69
62
|
files:
|
70
|
-
- .gitignore
|
63
|
+
- ".gitignore"
|
71
64
|
- Gemfile
|
72
65
|
- LICENSE
|
73
66
|
- README.md
|
@@ -80,33 +73,26 @@ files:
|
|
80
73
|
homepage: https://github.com/gampleman/exchange_rates
|
81
74
|
licenses:
|
82
75
|
- MIT
|
76
|
+
metadata: {}
|
83
77
|
post_install_message:
|
84
78
|
rdoc_options: []
|
85
79
|
require_paths:
|
86
80
|
- lib
|
87
81
|
required_ruby_version: !ruby/object:Gem::Requirement
|
88
|
-
none: false
|
89
82
|
requirements:
|
90
|
-
- -
|
83
|
+
- - ">="
|
91
84
|
- !ruby/object:Gem::Version
|
92
85
|
version: '0'
|
93
|
-
segments:
|
94
|
-
- 0
|
95
|
-
hash: -1027841455123845636
|
96
86
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
97
|
-
none: false
|
98
87
|
requirements:
|
99
|
-
- -
|
88
|
+
- - ">="
|
100
89
|
- !ruby/object:Gem::Version
|
101
90
|
version: '0'
|
102
|
-
segments:
|
103
|
-
- 0
|
104
|
-
hash: -1027841455123845636
|
105
91
|
requirements: []
|
106
92
|
rubyforge_project:
|
107
|
-
rubygems_version:
|
93
|
+
rubygems_version: 2.2.2
|
108
94
|
signing_key:
|
109
|
-
specification_version:
|
95
|
+
specification_version: 4
|
110
96
|
summary: Gives historical exchange rates
|
111
97
|
test_files:
|
112
98
|
- spec/exchange_rates_spec.rb
|