Sha256: 2b678b95dda3774d72737d8347ffa1cb87c7b0f434f5c3e13950d7c91ba09ab9
Contents?: true
Size: 1.6 KB
Versions: 1
Compression:
Stored size: 1.6 KB
Contents
Fixer ==== Fixer wraps the current and historical [foreign exchange rate feeds](http://www.ecb.europa.eu/stats/exchange/eurofxref/html/index.en.html) of the European Central Bank. Usage ----- ```ruby Fixer.daily Fixer.ninety_days Fixer.historical ``` A sample implementation: ```ruby # Migration class CreateForeignExchanges < ActiveRecord::Migration def self.up create_table :foreign_exchanges do |t| t.string :iso_code t.float :rate t.timestamps end add_index :foreign_exchanges, :iso_code end def self.down drop_table :foreign_exchanges end end # Model class ForeignExchange < ActiveRecord::Base class << self # Returns an exchange rate quote. # # Accepts a counter currency code and an optional base currency code. # Latter defaults to EUR if none is specified. def quote(counter, base='EUR') find_rate_by_currency(counter) / find_rate_by_currency(base) end private def find_rate_by_currency(iso_code) where(:iso_code => iso_code).first.rate end end end # Bank class ECB < Money::Bank::Base class << self def refresh hashes = Fixer.daily.first[:rates] hashes.push({ :currency => "EUR", :rate => 1.0 }) hashes.each do |hash| fx = ForeignExchange.find_or_initialize_by_iso_code(hash[:currency]) fx.rate = hash[:rate] fx.save end end end def exchange_with(from, to_currency) rate = ForeignExchange.quote(to_currency.iso_code, from.currency.iso_code) Money.new((from.cents * rate).floor, to_currency) end end # Initializer Money.default_bank = ECB.new ```
Version data entries
1 entries across 1 versions & 1 rubygems
Version | Path |
---|---|
fixer-0.4 | README.md |