spot-rate

8 views
Skip to first unread message

Jeff Iacono

unread,
May 8, 2013, 2:19:19 AM5/8/13
to dall...@googlegroups.com
Hi Dallas.rb

Wanted to pass along a quick gem, spot-rate: https://github.com/jeffreyiacono/spot-rate

It comes with a built in default currency convert but allows any number of currency converters to be registered.

Anyway, wanted to pass it along - any and all feedback, critiques, pull requests, etc. are more than welcome!

Usage

A SpotRate instance will by default use the GoogleCurrencyConverter if no other currency converters are registered:


require 'spot-rate'

puts Time.now
# => 2013-05-01 18:34:09 -0700
puts SpotRate.new(:from_currency => 'USD', :to_currency => 'CAD').spot_rate
# => 1.01689986

# or, more concisely:
puts SpotRate['USD' => 'CAD']
# => 1.01689986

If you'd like to register your own currency converter, use the .register_currency_converter method:


require 'spot-rate'

class MyRandomCurrencyConverter
  def initialize from_currency, to_currency
    @from_currency = from_currency
    @to_currency   = to_currency
  end

  def spot_rate
    rand # yolo
  end
end

SpotRate.register_currency_converter(:random_converter, MyRandomCurrencyConverter)
spot_rate = SpotRate.new(:from_currency => 'USD', :to_currency => 'CAD')
puts spot_rate.use(:random_converter).spot_rate
# => 0.5363022464905228
puts spot_rate.spot_rate # will go back to using the pre-packaged default Google converter
# => 1.01689986
Reply all
Reply to author
Forward
0 new messages