Class: RwandaNationalBank::HistoricRates

Inherits:
Object
  • Object
show all
Defined in:
lib/rwanda_national_bank/historic_rates.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

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 options = {}
  @as_of = options[:as_of] || Date.yesterday
end

Instance Attribute Details

#as_ofObject (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

#currenciesObject

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

Returns:

  • (Boolean)


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

#ratesObject

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