Module: I18nComplements::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



34
35
36
# File 'lib/i18n-complements/numisma.rb', line 34

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

.active_currenciesObject

Returns a hash with active currencies only



25
26
27
28
29
30
31
# File 'lib/i18n-complements/numisma.rb', line 25

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

.currenciesObject



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

def currencies
  @@currencies
end

.currencies_fileObject

Returns the path to currencies file



19
20
21
22
# File 'lib/i18n-complements/numisma.rb', line 19

def currencies_file
  # Rails.root.join("config", "currencies.yml")
  File.join(File.dirname(__FILE__), "numisma", "currencies.yml")
end

.currency_rate(from, to) ⇒ Object

Raises:

  • (ArgumentError)


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

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

.load_currenciesObject

Load currencies



57
58
59
60
61
62
63
64
# File 'lib/i18n-complements/numisma.rb', line 57

def load_currencies
  @@currencies = {}
  for code, details in YAML.load_file(self.currencies_file)

    currency = Currency.new(code, details.inject({}){|h, p| h[p[0].to_sym] = p[1]; h})
    @@currencies[currency.code] = currency
  end
end