Module: Currency
- Defined in:
- lib/currency.rb,
lib/currency/version.rb
Constant Summary collapse
- VERSION =
'1.0.3'
Class Method Summary collapse
- .extract_rate(html) ⇒ Object
- .get_html(origin, target) ⇒ Object
- .rate(origin, target) ⇒ Object
- .run_updater ⇒ Object
- .update ⇒ Object
- .update_rate(origin, target) ⇒ Object
Class Method Details
.extract_rate(html) ⇒ Object
11 12 13 14 15 |
# 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) ⇒ Object
16 17 18 19 20 |
# 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) ⇒ Object
21 22 23 24 25 26 27 28 |
# 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 ⇒ Object
29 30 31 32 33 34 35 36 |
# File 'lib/currency.rb', line 29 def Currency.run_updater Thread.new { loop do update sleep(24*60*60) end } end |
.update ⇒ Object
37 38 39 |
# File 'lib/currency.rb', line 37 def Currency.update @rates.each_key { |pair| update_rate(*pair) } end |
.update_rate(origin, target) ⇒ Object
40 41 42 43 44 |
# 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 |