Class: CurrencyUpdater::Currency

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(currency) ⇒ Currency

Returns a new instance of Currency.



7
8
9
10
11
12
# File 'lib/currency_updater/currency.rb', line 7

def initialize(currency)
  raise "Need a 3LC for currency #{currency}" unless currency.length == 3
  @currency = currency
  @code = currency.upcase
  @rate = 1.0
end

Instance Attribute Details

#codeObject

Returns the value of attribute code.



6
7
8
# File 'lib/currency_updater/currency.rb', line 6

def code
  @code
end

#currencyObject

Returns the value of attribute currency.



5
6
7
# File 'lib/currency_updater/currency.rb', line 5

def currency
  @currency
end

#rateObject

Returns the value of attribute rate.



4
5
6
# File 'lib/currency_updater/currency.rb', line 4

def rate
  @rate
end

Instance Method Details

#nameObject



14
15
16
# File 'lib/currency_updater/currency.rb', line 14

def name
  ::I18n.t("currency.#{@currency}")
end

#to_jsonObject



18
19
20
21
22
# File 'lib/currency_updater/currency.rb', line 18

def to_json
  { :rate => rate,
    :code => code,
    :currency => currency }.to_json
end

#to_xmlObject



24
25
26
27
28
29
30
31
32
# File 'lib/currency_updater/currency.rb', line 24

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