Forex

Code Climate Build Status Dependency Status Gem Version

Provides a simple DSL for managing Foreign Exchange rates (forex) for various traders (Inspired by CurrrencyJA).

Installation

Add this line to your application's Gemfile:

gem 'forex'

And then execute:

$ bundle

Or install it yourself as:

$ gem install forex

Usage

To add a new trader, create a new file at lib/forex/traders/<country-code>/<trader-name>.rb with the following contents:


Forex::Trader.define "NCB" do |t|
  t.base_currency  = "JMD"
  t.name           = "National Commercial Bank"
  t.endpoint       = "http://www.jncb.com/rates/foreignexchangerates"
  t.twitter_handle = "@ncbja"

  t.rates_parser = ->(doc) do # doc is a nokogiri document
    # process the doc and return rates hash in the following format

    # {
    #   "USD" => {
    #     :buy_cash => 102.0,
    #     :buy_draft => 103.3,
    #     :sell_cash => 105.0
    #   },
    #   # ...
    # }
  end
end

# Rates may be fetched for a specific trader
Forex::Trader.all['BNS'].fetch

TabularRates

TabularRates allows you to simplify the process of parsing a table of rates. Simply find the table and pass it along with options of what values are in which columns (zero indexed).

For example:

  #...

  t.rates_parser = ->(doc) do # doc is a nokogiri document

    options = {
      currency_code: 1,
      sell_cash: 2,
      buy_cash: 4,
      buy_draft: 3,
    }

    table = doc.css(".rates table").first

    translations = {
      'US$' => 'USD',
      'TT$' => 'TTD',
    }

    # translations are optional
    Forex::TabularRates.new(table, options).parse_rates(translations)
  end

  #...

TODO

  • Make it possible to have the same trader in multiple countries
  • Add other traders in Jamaica

Contributing

  1. Fork it
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Add some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create new Pull Request