Module: Atech::ForeignCurrency::Cacher

Defined in:
lib/atech/foreign_currency/cacher.rb

Class Method Summary collapse

Class Method Details

.getObject



10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/atech/foreign_currency/cacher.rb', line 10

def get
  url = "http://www.ecb.int/stats/eurofxref/eurofxref-daily.xml"
  uri = URI.parse(url)
  data = Net::HTTP.start(uri.host, uri.port) {|http| http.get(uri.path) }.body

  gbp = get_rate_from_string(data, 'GBP')
  eur = 1 / gbp
  usd = get_rate_from_string(data, 'USD')
  usd = usd / gbp

  cache_file = File.expand_path(ForeignCurrency.cache_file)
  File.open(cache_file, 'w') { |f| f.write({:eur => eur, :usd => usd}.to_yaml)}
end

.get_rate_from_string(string, rate) ⇒ Object



24
25
26
27
28
29
30
# File 'lib/atech/foreign_currency/cacher.rb', line 24

def get_rate_from_string(string, rate)
  if s = string.match(/\<Cube currency='#{rate}' rate='(.*)'\/>/)
    s[1].to_f
  else
    nil
  end
end