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 = {}
  @@currencies.each do |code, currency|
    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



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# 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) ⇒ Object



36
37
38
39
40
# 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_currenciesObject

Load currencies



65
66
67
68
69
70
71
72
# 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_ratesObject

Load fixed currency rates with reverse rates too.



75
76
77
78
79
80
81
82
83
84
85
86
# 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