Module: I18n::Complements::Numisma

Defined in:
lib/i18n/complements/numisma.rb,
lib/i18n/complements/numisma/currency.rb

Defined Under Namespace

Classes: Currency

Class Method Summary collapse

Class Method Details

.[](currency_code) ⇒ Object

Shorcut to get currency



32
33
34
# File 'lib/i18n/complements/numisma.rb', line 32

def [](currency_code)
  @@currencies[currency_code]
end

.active_currenciesObject

Returns a hash with active currencies only



23
24
25
26
27
28
29
# File 'lib/i18n/complements/numisma.rb', line 23

def active_currencies
  x = {}
  for code, currency in @@currencies
    x[code] = currency if currency.active
  end
  x
end

.currenciesObject



12
13
14
# File 'lib/i18n/complements/numisma.rb', line 12

def currencies
  @@currencies
end

.currencies_fileObject

Returns the path to currencies file



17
18
19
20
# 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) ⇒ Object

Raises:



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/i18n/complements/numisma.rb', line 36

def currency_rate(from, to)
  raise InvalidCurrency.new(":from currency is unknown (#{from.class}:#{from.inspect})") if Numisma[from].nil?
  raise InvalidCurrency.new(":to currency is unknown (#{to.class}:#{to.inspect})") if Numisma[to].nil?
  rate = nil
  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

.load_currenciesObject

Load currencies



54
55
56
57
58
59
60
# File 'lib/i18n/complements/numisma.rb', line 54

def load_currencies
  @@currencies = {}
  for code, details in YAML.load_file(currencies_file)
    currency = Currency.new(code, details.inject({}) { |h, p| h[p[0].to_sym] = p[1]; h })
    @@currencies[currency.code] = currency
  end
end