Class: FxLib::ExchangeRate

Inherits:
Object
  • Object
show all
Defined in:
lib/fx_lib.rb

Class Method Summary collapse

Class Method Details

.at(date, base_curr, counter_curr) ⇒ Object



31
32
33
34
35
36
37
38
39
# File 'lib/fx_lib.rb', line 31

def self.at(date,base_curr,counter_curr)
  d            = date.strftime("%Y-%m-%d")
  ers_base     = FxRate.find_by_downloaded_at_and_currency(d,base_curr)
  ers_counter  = FxRate.find_by_downloaded_at_and_currency(d,counter_curr)
  base_rate    = ers_base.rate
  counter_rate = ers_counter.rate
  rate         = (counter_rate/base_rate).round(4)
  return rate
end

.fetch_data(url) ⇒ Object



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/fx_lib.rb', line 56

def self.fetch_data(url)
  begin
    file         = open_xml_file(url)
    time_cubes   = file.xpath("//Cube[@time]")
    time_cubes.each do |tc|
      cubes    = tc.xpath("./Cube")
      cubes.each do |c|
        date     = tc.attr('time')
        currency = c.attr('currency')
        rate     = c.attr('rate')
        er       = FxRate.create(downloaded_at: date, currency: currency, rate: rate)
        puts er.inspect
        er.save
      end
    end
  rescue Exception => e
    puts e.to_s
    return e.to_s
  end
end

.fetch_data_on(url, date) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/fx_lib.rb', line 41

def self.fetch_data_on(url, date)
  begin
    file = open_xml_file(url)
    d    = date.strftime("%Y-%m-%d")
    extract = file.xpath("//Cube[@time='#{d}']/Cube")
    extract.each do |e|
      er = FxRate.create(downloaded_at: d, currency: e.attr('currency'), rate: e.attr('rate'))
      er.save
    end
  rescue Exception => e
    puts e.to_s
    return e.to_s
  end
end

.open_xml_file(url) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/fx_lib.rb', line 18

def self.open_xml_file(url)
  begin
  status = Timeout::timeout(60) {
    file = Nokogiri.XML(open(url))
    file.remove_namespaces!
    return file
  }
  rescue Timeout::Error => e
    puts e.to_s
    return e.to_s
  end
end