Class: EGPRates::SuezCanalBank
- Defined in:
- lib/egp_rates/suez_canal_bank.rb
Overview
Suez Canal Bank
Instance Attribute Summary
Attributes inherited from Bank
Instance Method Summary collapse
-
#exchange_rates ⇒ Hash
Of exchange rates for selling and buying { { sell: { SYM: rate }, { SYM: rate }, … }, { buy: { SYM: rate }, { SYM: rate }, … } }.
-
#initialize ⇒ SuezCanalBank
constructor
A new instance of SuezCanalBank.
-
#parse(raw_data) ⇒ Hash
Parse the #raw_exchange_rates returned in response.
-
#raw_exchange_rates ⇒ Enumerator::Lazy
Send the request to the URL and retrun raw data of the response rubocop:disable Style/MultilineMethodCallIndentation.
Constructor Details
#initialize ⇒ SuezCanalBank
Returns a new instance of SuezCanalBank.
5 6 7 8 |
# File 'lib/egp_rates/suez_canal_bank.rb', line 5 def initialize @sym = :SuezCanalBank @uri = URI.parse('http://scbank.com.eg/CurrencyAll.aspx') end |
Instance Method Details
#exchange_rates ⇒ Hash
Returns of exchange rates for selling and buying {
{ sell: { SYM: rate }, { SYM: rate }, ... },
{ buy: { SYM: rate }, { SYM: rate }, ... }
}.
15 16 17 |
# File 'lib/egp_rates/suez_canal_bank.rb', line 15 def exchange_rates @exchange_rates ||= parse(raw_exchange_rates) end |
#parse(raw_data) ⇒ Hash
Parse the #raw_exchange_rates returned in response
46 47 48 49 50 51 52 53 54 55 |
# File 'lib/egp_rates/suez_canal_bank.rb', line 46 def parse(raw_data) raw_data.each_with_object(sell: {}, buy: {}) do |row, result| sell_rate = row[7].to_f buy_rate = row[5].to_f currency = currency_symbol(row[3].strip) result[:sell][currency] = sell_rate result[:buy][currency] = buy_rate end end |
#raw_exchange_rates ⇒ Enumerator::Lazy
Send the request to the URL and retrun raw data of the response rubocop:disable Style/MultilineMethodCallIndentation
29 30 31 32 33 34 35 36 |
# File 'lib/egp_rates/suez_canal_bank.rb', line 29 def raw_exchange_rates # Suez Canal Bank provides 13 currencies only table_rows = Oga.parse_html(response.body)\ .css('#Table_01 tr:nth-child(4) > td:nth-child(2) > table tr') # But they have 2 <tr> used for the table headers fail ResponseError, 'Unknown HTML' unless table_rows&.size == 15 table_rows.lazy.drop(2).map(&:children).map { |cell| cell.map(&:text) } end |