fx_lib 0.1.2 → 0.1.3
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 +4 -4
- data/lib/fx_lib/version.rb +1 -1
- data/lib/fx_lib.rb +24 -12
- data/lib/tasks/db_seed.rake +1 -1
- data/spec/acceptance/fx_lib_spec.rb +8 -13
- data/spec/acceptance/fx_table_spec.rb +12 -4
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 6c6e936eff076ca680c53ee11cd18101d22b69b0
|
4
|
+
data.tar.gz: f6e8d59edabfcbc043fc991b43190a4fe7fb2471
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: e5742eec3712897fd9c2f1d6293615135b604b7cea21fd1dc3308b439fd324a56369efe183df379c901f4f3d5d55d189fa7ddd3670b20843906fa231b03a66b6
|
7
|
+
data.tar.gz: 566693ca19eb49c85e30f83f0f7a73ca047c7da9b26fead553aee1a355a67b896fa5365fafa163be37415dafa8fe1260f5795c5536237c1b4dc7877111710e95
|
data/lib/fx_lib/version.rb
CHANGED
data/lib/fx_lib.rb
CHANGED
@@ -38,20 +38,32 @@ module FxLib
|
|
38
38
|
return rate
|
39
39
|
end
|
40
40
|
|
41
|
-
def self.
|
41
|
+
def self.fetch_data_on(url, date)
|
42
42
|
begin
|
43
|
-
if (no_of_days < 1 || no_of_days > 90)
|
44
|
-
raise 'Days should be within 1 and 90'
|
45
|
-
end
|
46
43
|
file = open_xml_file(url)
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
44
|
+
d = date.strftime("%Y-%m-%d")
|
45
|
+
extract = file.xpath("//Cube[@time='#{d}']/Cube")
|
46
|
+
extract.each do |e|
|
47
|
+
er = FxRate.create(downloaded_at: d, currency: e.attr('currency'), rate: e.attr('rate'))
|
48
|
+
er.save
|
49
|
+
end
|
50
|
+
rescue Exception => e
|
51
|
+
puts e.to_s
|
52
|
+
return e.to_s
|
53
|
+
end
|
54
|
+
end
|
55
|
+
|
56
|
+
def self.fetch_data(url)
|
57
|
+
begin
|
58
|
+
file = open_xml_file(url)
|
59
|
+
time_cubes = file.xpath("//Cube[@time]")
|
60
|
+
time_cubes.each do |tc|
|
61
|
+
cubes = tc.xpath("./Cube")
|
62
|
+
cubes.each do |c|
|
63
|
+
date = tc.attr('time')
|
64
|
+
currency = c.attr('currency')
|
65
|
+
rate = c.attr('rate')
|
66
|
+
er = FxRate.create(downloaded_at: date, currency: currency, rate: rate)
|
55
67
|
puts er.inspect
|
56
68
|
er.save
|
57
69
|
end
|
data/lib/tasks/db_seed.rake
CHANGED
@@ -2,7 +2,8 @@ require 'spec_helper'
|
|
2
2
|
require './lib/fx_lib.rb'
|
3
3
|
|
4
4
|
describe 'FxLib' do
|
5
|
-
url = "http://www.ecb.europa.eu/stats/eurofxref/eurofxref-hist-90d.xml"
|
5
|
+
#url = "http://www.ecb.europa.eu/stats/eurofxref/eurofxref-hist-90d.xml"
|
6
|
+
url = './spec/internal/eurofx90d.xml'
|
6
7
|
|
7
8
|
it 'set up block yields self' do
|
8
9
|
FxLib.setup do |config|
|
@@ -13,28 +14,22 @@ describe 'FxLib' do
|
|
13
14
|
#Tests for fetching data
|
14
15
|
it "should have a method ExchangeRate.fetch_data" do
|
15
16
|
lambda do
|
16
|
-
FxLib::ExchangeRate.fetch_data(url
|
17
|
+
FxLib::ExchangeRate.fetch_data(url)
|
17
18
|
end.should_not raise_error
|
18
19
|
end
|
19
20
|
|
20
21
|
it "should have a method for ExchangeRate.at" do
|
21
22
|
lambda do
|
22
|
-
FxLib::ExchangeRate.fetch_data(url
|
23
|
-
date = DateTime.new(2013,12,
|
23
|
+
FxLib::ExchangeRate.fetch_data(url)
|
24
|
+
date = DateTime.new(2013,12,13)
|
24
25
|
FxLib::ExchangeRate.at(date, 'GBP', 'USD')
|
25
26
|
end.should_not raise_error
|
26
27
|
end
|
27
28
|
|
28
|
-
#check no_of_days should be > 0 and < 9
|
29
|
-
it 'should check the date range' do
|
30
|
-
str = FxLib::ExchangeRate.fetch_data(url, 0)
|
31
|
-
str.should eq("Days should be within 1 and 90")
|
32
|
-
end
|
33
|
-
|
34
29
|
it 'should calculate exchange rate' do
|
35
|
-
|
36
|
-
|
30
|
+
date = DateTime.new(2013,12,13)
|
31
|
+
FxLib::ExchangeRate.fetch_data_on(url, date)
|
37
32
|
rate = FxLib::ExchangeRate.at(date, 'GBP', 'USD')
|
38
|
-
rate.should eq(1.
|
33
|
+
rate.should eq(1.627)
|
39
34
|
end
|
40
35
|
end
|
@@ -1,12 +1,20 @@
|
|
1
1
|
require 'spec_helper'
|
2
|
+
require 'date'
|
2
3
|
require './lib/fx_lib.rb'
|
3
4
|
|
4
5
|
#test for generating a fx table
|
5
6
|
describe 'Generating a fx table' do
|
6
|
-
url = "http://www.ecb.europa.eu/stats/eurofxref/eurofxref-hist-90d.xml"
|
7
|
+
#url = "http://www.ecb.europa.eu/stats/eurofxref/eurofxref-hist-90d.xml"
|
8
|
+
url = './spec/internal/eurofx90d.xml'
|
7
9
|
|
8
|
-
it 'should fetch
|
9
|
-
FxLib::ExchangeRate.fetch_data(url
|
10
|
-
expect(FxRate.where(currency: 'USD', rate: '1.
|
10
|
+
it 'should fetch all data given correct url' do
|
11
|
+
FxLib::ExchangeRate.fetch_data(url)
|
12
|
+
expect(FxRate.where(currency: 'USD', rate: '1.3727')).not_to be_empty
|
13
|
+
end
|
14
|
+
|
15
|
+
it 'should fetch data for a given date' do
|
16
|
+
date = DateTime.new(2013,12,13)
|
17
|
+
FxLib::ExchangeRate.fetch_data_on(url, date)
|
18
|
+
expect(FxRate.where(currency: 'USD', rate: '1.3727')).not_to be_empty
|
11
19
|
end
|
12
20
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: fx_lib
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- hsu
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2013-12-
|
11
|
+
date: 2013-12-17 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activerecord
|