Class: ISO4217::Currency

Inherits:
Object
  • Object
show all
Defined in:
lib/currencies/currency.rb,
lib/currencies/exchange_bank.rb

Direct Known Subclasses

Currency

Defined Under Namespace

Classes: ExchangeBank

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(iso_code, opts = {}) ⇒ Currency

Returns a new instance of Currency.



14
15
16
17
18
19
20
# File 'lib/currencies/currency.rb', line 14

def initialize(iso_code,opts={})
  @code = iso_code.to_s.upcase
  @name = opts['name']
  @symbol = opts['symbol']
  @exchange_currency = opts['exchange_currency'] || self.class.base_currency
  @exchange_rate = opts['exchange_rate'].to_f if opts['exchange_rate']
end

Class Attribute Details

.base_currencyObject

Returns the value of attribute base_currency.



7
8
9
# File 'lib/currencies/currency.rb', line 7

def base_currency
  @base_currency
end

.currenciesObject

Returns the value of attribute currencies.



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

def currencies
  @currencies
end

.import_exchange_ratesObject

Returns the value of attribute import_exchange_rates.



9
10
11
# File 'lib/currencies/currency.rb', line 9

def import_exchange_rates
  @import_exchange_rates
end

.major_codesObject

Returns the value of attribute major_codes.



8
9
10
# File 'lib/currencies/currency.rb', line 8

def major_codes
  @major_codes
end

Instance Attribute Details

#codeObject (readonly)

Returns the value of attribute code.



12
13
14
# File 'lib/currencies/currency.rb', line 12

def code
  @code
end

#exchange_currencyObject (readonly)

Returns the value of attribute exchange_currency.



12
13
14
# File 'lib/currencies/currency.rb', line 12

def exchange_currency
  @exchange_currency
end

#nameObject (readonly)

Returns the value of attribute name.



12
13
14
# File 'lib/currencies/currency.rb', line 12

def name
  @name
end

#symbolObject (readonly)

Returns the value of attribute symbol.



12
13
14
# File 'lib/currencies/currency.rb', line 12

def symbol
  @symbol
end

Class Method Details

.add(new_currency) ⇒ Object



88
89
90
91
# File 'lib/currencies/currency.rb', line 88

def self.add(new_currency)
  self.currencies ||= {}
  self.currencies[new_currency.code] = new_currency
end

.best_from_currencies(currencies) ⇒ Object



58
59
60
61
# File 'lib/currencies/currency.rb', line 58

def self.best_from_currencies(currencies)
  return if currencies.nil? || currencies.empty?
  self.major_currencies_selection(currencies) ? self.major_currencies_selection(currencies)[1] : currencies.first[1]
end

.best_from_name(name) ⇒ Object



71
72
73
# File 'lib/currencies/currency.rb', line 71

def self.best_from_name(name)
  self.best_from_currencies(self.list_from_name(name))
end

.best_from_symbol(symbol) ⇒ Object



75
76
77
# File 'lib/currencies/currency.rb', line 75

def self.best_from_symbol(symbol)
  self.best_from_currencies(self.list_from_symbol(symbol))
end

.best_guess(str) ⇒ Object



79
80
81
82
# File 'lib/currencies/currency.rb', line 79

def self.best_guess(str)
  return if str.nil? || str.empty?
  self.from_code(str) || self.best_from_symbol(str) || self.best_from_name(str)
end

.code_from_best_guess(str) ⇒ Object



84
85
86
# File 'lib/currencies/currency.rb', line 84

def self.code_from_best_guess(str)
  self.best_guess(str).try(:code)
end

.from_code(code) ⇒ Object



50
51
52
# File 'lib/currencies/currency.rb', line 50

def self.from_code(code)
  self.currencies[code.to_s.upcase]
end

.list_from_name(name) ⇒ Object



63
64
65
# File 'lib/currencies/currency.rb', line 63

def self.list_from_name(name)
  self.currencies.select { |code, currency| currency.name == name }
end

.list_from_symbol(symbol) ⇒ Object



67
68
69
# File 'lib/currencies/currency.rb', line 67

def self.list_from_symbol(symbol)
  self.currencies.select { |code, currency| currency.symbol == symbol }
end

.load_file(file) ⇒ Object



44
45
46
47
48
# File 'lib/currencies/currency.rb', line 44

def self.load_file(file)
  YAML.load_file(file).each do |code,options|
    self.add(self.new(code,options))
  end
end

.major_currencies_selection(currencies) ⇒ Object



54
55
56
# File 'lib/currencies/currency.rb', line 54

def self.major_currencies_selection(currencies)
  currencies.select { |code, currency| self.major_codes.include?(code) }.first
end

Instance Method Details

#[](value) ⇒ Object



22
23
24
# File 'lib/currencies/currency.rb', line 22

def [](value)
  self.instance_variable_get("@#{value}")
end

#exchange_rateObject



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

def exchange_rate
  @exchange_rate = nil unless @exchange_currency == self.class.base_currency
  @exchange_rate ||= load_exchange_rate
end

#load_exchange_rateObject



31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/currencies/currency.rb', line 31

def load_exchange_rate
  @exchange_currency = self.class.base_currency
  return 1.0 if @code == @exchange_currency
  if self.class.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