Sha256: 043c15f8c3aa77e6fa37551348b2c24160c1ae6d4b4ea1fdc732fa24d9460954
Contents?: true
Size: 1.56 KB
Versions: 2
Compression:
Stored size: 1.56 KB
Contents
class Currency class << self attr_accessor :currencies attr_accessor :base_currency attr_accessor :import_exchange_rates end attr_reader :code, :symbol, :name, :exchange_currency 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 def [](value) self.instance_variable_get("@#{value}") end def exchange_rate @exchange_rate = nil unless @exchange_currency == Currency.base_currency @exchange_rate ||= load_exchange_rate end 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 def self.load_file(file) YAML.load_file(file).each do |code,options| self.add(self.new(code,options)) end end def self.from_code(code) self.currencies[code.to_s.upcase] end def self.add(new_currency) self.currencies ||= {} self.currencies[new_currency.code] = new_currency end load_file(File.join(File.dirname(__FILE__), '..', 'data', 'iso4217.yaml')) self.base_currency = 'USD' self.import_exchange_rates = true end
Version data entries
2 entries across 2 versions & 1 rubygems
Version | Path |
---|---|
currencies-0.2.1 | lib/currencies/currency.rb |
currencies-0.2.0 | lib/currencies/currency.rb |