Class: CurrencyUpdater::Currencies

Inherits:
Object
  • Object
show all
Defined in:
lib/currency_updater/currencies.rb

Constant Summary collapse

UNSUPPORTED =

EUR is unsupported because it’s our reference currency TRY is unsupported because it’s a method used in actionpack the other currencies are not supported because of google

%w(EUR TRY AFN ALL AMD AOA AZN BAM BIF BMD BSD BTN CDF CUP ETB FIM FKP GEL GNF GYD IMP JEP KGS KMF LRD LSL LYD MGA MMK MNT MRO MZN SBD SHP SOS STD TJS TMM TOP TVD VUV WST ZWD)
@@currencies =
{}

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.add(currency) ⇒ Object



12
13
14
15
16
17
# File 'lib/currency_updater/currencies.rb', line 12

def add(currency)
  currency = currency.to_s.downcase
  result = @@currencies[currency] = Currency.new(currency)
  define_currency(currency)
  result
end

.allObject



19
20
21
# File 'lib/currency_updater/currencies.rb', line 19

def all
  @@currencies.values
end

.all_codesObject



34
35
36
37
38
# File 'lib/currency_updater/currencies.rb', line 34

def all_codes
  result = IsoCountryCodes.all.map(&:currency).sort
  result = result - UNSUPPORTED
  result.uniq
end

.define_currency(currency) ⇒ Object



40
41
42
43
44
# File 'lib/currency_updater/currencies.rb', line 40

def define_currency(currency)
  define_method(currency) do
    @@currencies[currency]
  end
end

.deleteObject Also known as: clear



27
28
29
# File 'lib/currency_updater/currencies.rb', line 27

def delete
  @@currencies = {}
end

.include?(currency) ⇒ Boolean

Returns:

  • (Boolean)


23
24
25
# File 'lib/currency_updater/currencies.rb', line 23

def include?(currency)
  @@currencies.keys.include?(currency.downcase)
end

.unsupported?(currency) ⇒ Boolean

Returns:

  • (Boolean)


46
47
48
# File 'lib/currency_updater/currencies.rb', line 46

def unsupported?(currency)
  UNSUPPORTED.include?(currency)
end

Instance Method Details

#to_jsonObject



51
52
53
# File 'lib/currency_updater/currencies.rb', line 51

def to_json
  @@currencies.to_json
end

#to_xmlObject



55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/currency_updater/currencies.rb', line 55

def to_xml
  xml = Builder::XmlMarkup.new(:indent=>2)
  xml.instruct! :xml, :version=>"1.0", :encoding=>"UTF-8"
  xml.currencies do
    @@currencies.values.each do |currency|
      xml.currency(:name => currency.currency) do
        xml.code(currency.code)
        xml.rate(currency.rate)
      end
    end
  end
  xml.target!
end

#to_yamlObject



69
70
71
# File 'lib/currency_updater/currencies.rb', line 69

def to_yaml
  @@currencies.to_yaml
end