Class: Currency
- Inherits:
-
Object
- Object
- Currency
- Defined in:
- lib/currencies/currency.rb,
lib/currencies/exchange_bank.rb
Defined Under Namespace
Classes: ExchangeBank
Class Attribute Summary collapse
-
.base_currency ⇒ Object
Returns the value of attribute base_currency.
-
.currencies ⇒ Object
Returns the value of attribute currencies.
-
.import_exchange_rates ⇒ Object
Returns the value of attribute import_exchange_rates.
Instance Attribute Summary collapse
-
#code ⇒ Object
readonly
Returns the value of attribute code.
-
#exchange_currency ⇒ Object
readonly
Returns the value of attribute exchange_currency.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#symbol ⇒ Object
readonly
Returns the value of attribute symbol.
Class Method Summary collapse
Instance Method Summary collapse
- #[](value) ⇒ Object
- #exchange_rate ⇒ Object
-
#initialize(iso_code, opts = {}) ⇒ Currency
constructor
A new instance of Currency.
- #load_exchange_rate ⇒ Object
Constructor Details
#initialize(iso_code, opts = {}) ⇒ Currency
Returns a new instance of Currency.
10 11 12 13 14 15 16 |
# File 'lib/currencies/currency.rb', line 10 def initialize(iso_code,opts={}) @code = iso_code.to_s.upcase @name = opts['name'] @symbol = opts['symbol'] @exchange_currency = opts['exchange_currency'] || Currency.base_currency @exchange_rate = opts['exchange_rate'].to_f if opts['exchange_rate'] end |
Class Attribute Details
.base_currency ⇒ Object
Returns the value of attribute base_currency.
4 5 6 |
# File 'lib/currencies/currency.rb', line 4 def base_currency @base_currency end |
.currencies ⇒ Object
Returns the value of attribute currencies.
3 4 5 |
# File 'lib/currencies/currency.rb', line 3 def currencies @currencies end |
.import_exchange_rates ⇒ Object
Returns the value of attribute import_exchange_rates.
5 6 7 |
# File 'lib/currencies/currency.rb', line 5 def import_exchange_rates @import_exchange_rates end |
Instance Attribute Details
#code ⇒ Object (readonly)
Returns the value of attribute code.
8 9 10 |
# File 'lib/currencies/currency.rb', line 8 def code @code end |
#exchange_currency ⇒ Object (readonly)
Returns the value of attribute exchange_currency.
8 9 10 |
# File 'lib/currencies/currency.rb', line 8 def exchange_currency @exchange_currency end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
8 9 10 |
# File 'lib/currencies/currency.rb', line 8 def name @name end |
#symbol ⇒ Object (readonly)
Returns the value of attribute symbol.
8 9 10 |
# File 'lib/currencies/currency.rb', line 8 def symbol @symbol end |
Class Method Details
.add(new_currency) ⇒ Object
50 51 52 53 |
# File 'lib/currencies/currency.rb', line 50 def self.add(new_currency) self.currencies ||= {} self.currencies[new_currency.code] = new_currency end |
.from_code(code) ⇒ Object
46 47 48 |
# File 'lib/currencies/currency.rb', line 46 def self.from_code(code) self.currencies[code.to_s.upcase] end |
.load_file(file) ⇒ Object
40 41 42 43 44 |
# File 'lib/currencies/currency.rb', line 40 def self.load_file(file) YAML.load_file(file).each do |code,| self.add(self.new(code,)) end end |
Instance Method Details
#[](value) ⇒ Object
18 19 20 |
# File 'lib/currencies/currency.rb', line 18 def [](value) self.instance_variable_get("@#{value}") end |
#exchange_rate ⇒ Object
22 23 24 25 |
# File 'lib/currencies/currency.rb', line 22 def exchange_rate @exchange_rate = nil unless @exchange_currency == Currency.base_currency @exchange_rate ||= load_exchange_rate end |
#load_exchange_rate ⇒ Object
27 28 29 30 31 32 33 34 35 36 37 38 |
# File 'lib/currencies/currency.rb', line 27 def load_exchange_rate @exchange_currency = Currency.base_currency return 1.0 if @code == @exchange_currency if Currency.import_exchange_rates http = Net::HTTP.new('download.finance.yahoo.com', 80) response = http.get("/d/quotes.csv?e=.csv&f=sl1d1t1&s=#{@code}#{@exchange_currency}=X") rate = response.body.split(',')[1] rate == '0.0' ? nil : rate.to_f else nil end end |