Class: EGPRates::SuezCanalBank

Inherits:
Bank
  • Object
show all
Defined in:
lib/egp_rates/suez_canal_bank.rb

Overview

Suez Canal Bank

Instance Attribute Summary

Attributes inherited from Bank

#sym

Instance Method Summary collapse

Constructor Details

#initializeSuezCanalBank

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_ratesHash

Returns of exchange rates for selling and buying {

{ sell: { SYM: rate }, { SYM: rate }, ... },
{ buy:  { SYM: rate }, { SYM: rate }, ... }

}.

Returns:

  • (Hash)

    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

Parameters:

  • of (Array)

    the raw_data scraped

Returns:

  • (Hash)

    of exchange rates for selling and buying {

    { sell: { SYM: rate }, { SYM: rate }, ... },
    { buy:  { SYM: rate }, { SYM: rate }, ... }
    

    }



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_ratesEnumerator::Lazy

Send the request to the URL and retrun raw data of the response rubocop:disable Style/MultilineMethodCallIndentation

Returns:

  • (Enumerator::Lazy)

    with the table row in HTML that evaluates to [

    "\r\n ",  "\r\n ", "\r\n ", "\r\n US Dollar ", "\r\n ",
    "\r\n 15.1500", "\r\n ", "\r\n 15.8000", "\r\n ", ...
    

    ], [

    "\r\n ", "\r\n ", "\r\n ", "\r\n Sterling Pound", "\r\n ",
    "\r\n 18.5933", "\r\n ", "\r\n 20.0448", "\r\n ", ...
    

    ],



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