This repository has been archived by the owner on Jun 10, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
banque_misr.rb
69 lines (65 loc) · 2.25 KB
/
banque_misr.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# frozen_string_literal: true
module EGPRates
# Banque Misr
class BanqueMisr < EGPRates::Bank
def initialize
@sym = :BanqueMisr
@uri = URI.parse('http://www.banquemisr.com/en/exchangerates')
end
# @return [Hash] of exchange rates for selling and buying
# {
# { sell: { SYM: rate }, { SYM: rate }, ... },
# { buy: { SYM: rate }, { SYM: rate }, ... }
# }
def exchange_rates
@exchange_rates ||= parse(raw_exchange_rates)
end
private
# Send the request to the URL and retrun raw data of the response
# @return [Enumerator::Lazy] with the table row in HTML that evaluates to
# [
# ["US DOLLAR", "15.35", "15.91", "15.35", "15.91"],
# ["EURO", "16.6256", "17.3698", "16.633898", "17.378493"]
# ...
# ]
#
def raw_exchange_rates
# BanqueMisr provide 18 currencies (17 Used and CYPRUS POUND)
# But they have 2 <tr> for headers
table_rows = Oga.parse_html(response.body).css('.exchangeRates tbody tr')
fail ResponseError, 'Unknown HTML' unless table_rows&.size == 20
# remove the first 2 headers of the array and the last element
# which is CYPRUS POUND which is not used anymore
table_rows.lazy.drop(2).take(17).map(&:children).map do |cell|
cell.map(&:text)
end
end
# Convert currency string to ISO symbol
# @param currency [String] "US Dollar"
# @return [Symbol] :USD ISO currency name
# rubocop:disable Metrics/CyclomaticComplexity
def currency_symbol(currency)
case currency
when /UAE DIRHAM/ then :AED
when /AUSTRALIA/ then :AUD
when /BAHRAIN/ then :BHD
when /CANADA/ then :CAD
when /SWISS/ then :CHF
when /DENMARK/ then :DKK
when /EURO/ then :EUR
when /GB POUND/ then :GBP
when /JORDANIAN/ then :JOD
when /JAPAN/ then :JPY
when /KUWAIT/ then :KWD
when /NORWAY/ then :NOK
when /OMAN/ then :OMR
when /QATARI/ then :QAR
when /SAUDI/ then :SAR
when /SWEDISH/ then :SEK
when /US DOLLAR/ then :USD
else fail ResponseError, "Unknown currency #{currency}"
end
end
# rubocop:enable Metrics/CyclomaticComplexity
end
end