module I18n::Complements::Numisma
Public Class Methods
[](currency_code)
click to toggle source
Shorcut to get currency
# File lib/i18n/complements/numisma.rb, line 32 def [](currency_code) @@currencies[currency_code] end
active_currencies()
click to toggle source
Returns a hash with active currencies only
# File lib/i18n/complements/numisma.rb, line 23 def active_currencies x = {} @@currencies.each do |code, currency| x[code] = currency if currency.active end x end
currencies()
click to toggle source
# File lib/i18n/complements/numisma.rb, line 12 def currencies @@currencies end
currencies_file()
click to toggle source
Returns the path to currencies file
# File lib/i18n/complements/numisma.rb, line 17 def currencies_file # Rails.root.join("config", "currencies.yml") File.join(File.dirname(__FILE__), 'numisma', 'currencies.yml') end
currency_rate(from, to)
click to toggle source
# File lib/i18n/complements/numisma.rb, line 42 def currency_rate(from, to) if Numisma[from].nil? raise InvalidCurrency, ":from currency is unknown (#{from.class}:#{from.inspect})" end if Numisma[to].nil? raise InvalidCurrency, ":to currency is unknown (#{to.class}:#{to.inspect})" end rate = fixed_currency_rate(from, to) return rate if rate begin uri = URI('http://www.webservicex.net/CurrencyConvertor.asmx/ConversionRate') response = Net::HTTP.post_form(uri, 'FromCurrency' => from, 'ToCurrency' => to) doc = ::LibXML::XML::Parser.string(response.body).parse rate = doc.root.content.to_f rescue uri = URI("http://download.finance.yahoo.com/d/quotes.csv?s=#{from}#{to}=X&f=l1") response = Net::HTTP.get(uri) rate = response.strip.to_f end rate end
fixed_currency_rate(from, to)
click to toggle source
# File lib/i18n/complements/numisma.rb, line 36 def fixed_currency_rate(from, to) rates = @@fixed_currency_rates[from] return nil unless rates rates[to] end
load_currencies()
click to toggle source
Load currencies
# File lib/i18n/complements/numisma.rb, line 65 def load_currencies @@currencies = {} yaml = YAML.load_file(currencies_file) yaml.each do |code, attributes| currency = Currency.new(code, attributes.inject({}) { |h, p| h[p[0].to_sym] = p[1]; h }) @@currencies[currency.code] = currency end end
load_fixed_currency_rates()
click to toggle source
Load fixed currency rates with reverse rates too.
# File lib/i18n/complements/numisma.rb, line 75 def load_fixed_currency_rates @@fixed_currency_rates = {} yaml = YAML.load_file(File.join(File.dirname(__FILE__), 'numisma', 'fixed_currency_rates.yml')) yaml.each do |from, rates| @@fixed_currency_rates[from] ||= {} rates.each do |to, rate| @@fixed_currency_rates[from][to] = rate @@fixed_currency_rates[to] ||= {} @@fixed_currency_rates[to][from] = 1 / rate end end end