module Currency

Constants

VERSION

Public Class Methods

extract_rate(html) click to toggle source
# File lib/currency.rb, line 11
def Currency.extract_rate(html)
  if(match = /\<span class=bld\>(\d+\.\d+) \w+\<\/span\>/.match(html))
    match[1]
  end
end
get_html(origin, target) click to toggle source
# File lib/currency.rb, line 16
def Currency.get_html(origin, target)
  url = "https://finance.google.com/finance/converter?a=1&from=#{origin.upcase}&to=#{target.upcase}"
  @data = URI.parse(url).read
  return @data
end
rate(origin, target) click to toggle source
# File lib/currency.rb, line 21
def Currency.rate(origin, target)
  origin = origin.to_s.upcase[/^[A-Z]{3}/]
  target = target.to_s.upcase[/^[A-Z]{3}/]
  return 1.0 if(origin == target)
  @rates[[origin, target]] \
    || ((rate = @rates[[target, origin]]) && (1.0 / rate)) \
    || update_rate(origin, target)
end
run_updater() click to toggle source
# File lib/currency.rb, line 29
def Currency.run_updater
  Thread.new { 
    loop do
      update
      sleep(24*60*60)
    end
  }
end
update() click to toggle source
# File lib/currency.rb, line 37
def Currency.update
  @rates.each_key { |pair| update_rate(*pair) }
end
update_rate(origin, target) click to toggle source
# File lib/currency.rb, line 40
def Currency.update_rate(origin, target)
  if(rate = extract_rate(get_html(origin, target)))
    @rates[[origin, target]] = rate.to_f
  end
end