Class: RwandaNationalBank::HistoricRates
- Inherits:
-
Object
- Object
- RwandaNationalBank::HistoricRates
- Defined in:
- lib/rwanda_national_bank/historic_rates.rb
Instance Attribute Summary collapse
-
#as_of ⇒ Object
readonly
Returns the value of attribute as_of.
Class Method Summary collapse
-
.scrape(as_of) ⇒ Object
Scrapes the RNB website for the historic exchange rates of a given day.
Instance Method Summary collapse
-
#currencies ⇒ Object
Returns a list of ISO currencies.
-
#has_rates? ⇒ Boolean
Returns true when reading the website was successful.
-
#import! ⇒ Object
Triggers the srcaping.
-
#initialize(options = {}) ⇒ HistoricRates
constructor
A new instance of HistoricRates.
- #rate(iso_from, iso_to) ⇒ Object
-
#rates ⇒ Object
Returns all rates returned.
- #scrape(as_of) ⇒ Object
Constructor Details
#initialize(options = {}) ⇒ HistoricRates
Returns a new instance of HistoricRates.
12 13 14 |
# File 'lib/rwanda_national_bank/historic_rates.rb', line 12 def initialize = {} @as_of = [:as_of] || Date.yesterday end |
Instance Attribute Details
#as_of ⇒ Object (readonly)
Returns the value of attribute as_of.
10 11 12 |
# File 'lib/rwanda_national_bank/historic_rates.rb', line 10 def as_of @as_of end |
Class Method Details
.scrape(as_of) ⇒ Object
Scrapes the RNB website for the historic exchange rates of a given day
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 |
# File 'lib/rwanda_national_bank/historic_rates.rb', line 56 def self.scrape as_of # POST /index.php?id=384 uri = URI('http://www.bnr.rw/index.php?id=384') # fday=30&fmonth=NOV&fyear=2015&history=Get+History params = { 'fday' => as_of.strftime('%d'), 'fmonth' => as_of.strftime('%b').upcase, 'fyear' => as_of.strftime('%Y'), 'history' => 'Get History' } response = Net::HTTP.post_form(uri, params) dom = Nokogiri::HTML(response.body) rows = dom.css('#middlebar > table > tr') # will always return an [] # RNB does not really use ISO codes thoroughly so we have to map some of their symbols translate_currency_symbols = { 'KD' => 'KWD', # Kuwait Dinar 'RS' => 'INR', # Indian Rupees 'LD' => 'LRD' # Lybian Dinar } Hash[rows.map do |row| currency = row.css(':nth-child(1) > a').text next if currency.empty? avrg = row.css(':nth-child(4)').text next if avrg.empty? # translate currency to ISO symbol currency = translate_currency_symbols[currency.upcase] || currency [currency, Float(avrg)] rescue nil end.compact] end |
Instance Method Details
#currencies ⇒ Object
Returns a list of ISO currencies
29 30 31 32 |
# File 'lib/rwanda_national_bank/historic_rates.rb', line 29 def currencies fail MissingRates unless has_rates? @rates.keys end |
#has_rates? ⇒ Boolean
Returns true when reading the website was successful
41 42 43 |
# File 'lib/rwanda_national_bank/historic_rates.rb', line 41 def has_rates? !@rates.nil? && !@rates.empty? end |
#import! ⇒ Object
Triggers the srcaping
46 47 48 49 |
# File 'lib/rwanda_national_bank/historic_rates.rb', line 46 def import! @rates = scrape(@as_of) has_rates? end |
#rate(iso_from, iso_to) ⇒ Object
16 17 18 19 20 21 22 23 24 25 26 |
# File 'lib/rwanda_national_bank/historic_rates.rb', line 16 def rate(iso_from, iso_to) fail MissingRates unless has_rates? if iso_from == 'RWF' rates[iso_to] ? 1/rates[iso_to] : nil elsif iso_to == 'RWF' rates[iso_from] else nil end end |
#rates ⇒ Object
Returns all rates returned
35 36 37 38 |
# File 'lib/rwanda_national_bank/historic_rates.rb', line 35 def rates fail MissingRates unless has_rates? @rates end |
#scrape(as_of) ⇒ Object
51 52 53 |
# File 'lib/rwanda_national_bank/historic_rates.rb', line 51 def scrape as_of self.class.scrape(@as_of) end |