fx_lib 0.1.2 → 0.1.3

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 7faf2c70cf0b0ac42eb025863ebd3a9d1f1fbc00
4
- data.tar.gz: 9bd0c3a8e6dce41426adcbe57181433b0b34afd7
3
+ metadata.gz: 6c6e936eff076ca680c53ee11cd18101d22b69b0
4
+ data.tar.gz: f6e8d59edabfcbc043fc991b43190a4fe7fb2471
5
5
  SHA512:
6
- metadata.gz: 2fb1a6ba4bff52b1fb5a63f0b9ba065b6633ddf7ad378a1e970fb57e38546c98cb5f9a51817955f7848c489263c68b804799fb59b1a9014863b3d94c245229ed
7
- data.tar.gz: 5a9721a8f3447821109b78c90a78cf2cf4baf3bbca81210144ff630dbf228945e80edb86cfa1ebf51ad2d39dc93257a702ddbd3c38d1564ac99940d68f6f96b3
6
+ metadata.gz: e5742eec3712897fd9c2f1d6293615135b604b7cea21fd1dc3308b439fd324a56369efe183df379c901f4f3d5d55d189fa7ddd3670b20843906fa231b03a66b6
7
+ data.tar.gz: 566693ca19eb49c85e30f83f0f7a73ca047c7da9b26fead553aee1a355a67b896fa5365fafa163be37415dafa8fe1260f5795c5536237c1b4dc7877111710e95
@@ -1,3 +1,3 @@
1
1
  module FxLib
2
- VERSION = "0.1.2"
2
+ VERSION = "0.1.3"
3
3
  end
data/lib/fx_lib.rb CHANGED
@@ -38,20 +38,32 @@ module FxLib
38
38
  return rate
39
39
  end
40
40
 
41
- def self.fetch_data(url, no_of_days)
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
- no_of_days.times do |d|
48
- #ToDo Account for weekends
49
- #date = (Date.today - d - 3).strftime("%Y-%m-%d")
50
- date = (Date.today - d).strftime("%Y-%m-%d")
51
- extract = file.xpath("//Cube[@time='#{date}']/Cube")
52
- puts date.inspect
53
- extract.each do |e|
54
- er = FxRate.create(downloaded_at: date, currency: e.attr('currency'), rate: e.attr('rate'))
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
@@ -1,5 +1,5 @@
1
1
  namespace :fx_lib do
2
2
  task db_seed: :environment do
3
- FxLib::ExchangeRate.fetch_data(ENV['FX_URL'],90)
3
+ FxLib::ExchangeRate.fetch_data(ENV['FX_URL'])
4
4
  end
5
5
  end
@@ -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, 1)
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, 1)
23
- date = DateTime.new(2013,12,16)
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
- FxLib::ExchangeRate.fetch_data(url, 1)
36
- date = DateTime.new(2013,12,16)
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.6325)
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 the data given correct url and date' do
9
- FxLib::ExchangeRate.fetch_data(url, 1)
10
- expect(FxRate.where(currency: 'USD', rate: '1.3776')).not_to be_empty
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.2
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-16 00:00:00.000000000 Z
11
+ date: 2013-12-17 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord