EGP_Rates 1.0.5 → 1.0.6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/lib/egp_rates/adib.rb +28 -9
- data/lib/egp_rates/bank.rb +18 -18
- data/spec/cassettes/ADIB.gz +0 -0
- data/spec/egp_rates/adib_spec.rb +11 -11
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: f44e100e103d834ee8434b362fb912b4181c82e2
|
4
|
+
data.tar.gz: 78a5784277b79b6716febce6a187a079b3a9d9cd
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 2c8eeb9acc8abbe77130627cdd5f98da7814c0b1fa9cf6e9fbabad877f3bb7e453a67e0957028359fbd84e981ba44984a73987610059134df004e4185fa56a99
|
7
|
+
data.tar.gz: 99f2c7eca30933e121bd8ed0c04ebf1609dfd81338fafaed46af5f2a60cbf80cb71754feb4b174d6795c57579ecd7bacc0ccbb032fe243ddc99e7a028cecc87a
|
data/lib/egp_rates/adib.rb
CHANGED
@@ -19,17 +19,36 @@ module EGPRates
|
|
19
19
|
private
|
20
20
|
|
21
21
|
# Send the request to the URL and retrun raw data of the response
|
22
|
-
# @return [
|
22
|
+
# @return [Array<Array>] containing image url (currency) and exchange rates
|
23
23
|
# [
|
24
|
-
# ["
|
25
|
-
# ["
|
24
|
+
# ["/media/246206/usd.png", [["Buy: 18.1000", "Sell: 18.85"]]],
|
25
|
+
# ["/media/246211/gbp.png", [["Buy: 22.1019", "Sell: 23.1365"]]]
|
26
26
|
# ...
|
27
27
|
# ]
|
28
28
|
def raw_exchange_rates
|
29
|
-
table_rows = Oga.parse_html(response.body).css('
|
29
|
+
table_rows = Oga.parse_html(response.body).css('#ratesContainer li')
|
30
30
|
# ADIB porvide 5 currencies on the home page (and 4 rows of info)
|
31
|
-
fail ResponseError, 'Unknown HTML' unless table_rows&.size ==
|
32
|
-
table_rows
|
31
|
+
fail ResponseError, 'Unknown HTML' unless table_rows&.size == 5
|
32
|
+
currencies(table_rows).zip(rates(table_rows))
|
33
|
+
end
|
34
|
+
|
35
|
+
|
36
|
+
# Extract the currencies from the image components src attribute
|
37
|
+
# @return [Array<String>] containing the URL to image of the currency
|
38
|
+
def currencies(table_rows)
|
39
|
+
table_rows.lazy.map do |e|
|
40
|
+
e.css('img').map { |img| img.attribute('src').value }
|
41
|
+
end.force.flatten
|
42
|
+
end
|
43
|
+
|
44
|
+
# Extract the text descriping the exchange rates from content <p> nodes
|
45
|
+
# @return [Array<Array>] text description for buy/sell rates
|
46
|
+
def rates(table_rows)
|
47
|
+
table_rows.map do |e|
|
48
|
+
e.css('.content').map(&:text).map(&:strip).map do |txt|
|
49
|
+
txt.split("\n").map(&:strip)
|
50
|
+
end
|
51
|
+
end
|
33
52
|
end
|
34
53
|
|
35
54
|
# Parse the #raw_exchange_rates returned in response
|
@@ -41,9 +60,9 @@ module EGPRates
|
|
41
60
|
# }
|
42
61
|
def parse(raw_data)
|
43
62
|
raw_data.each_with_object(sell: {}, buy: {}) do |row, result|
|
44
|
-
|
45
|
-
|
46
|
-
|
63
|
+
currency = currency_symbol(row[0])
|
64
|
+
sell_rate = row[1][0][1][5..-1].to_f
|
65
|
+
buy_rate = row[1][0][0][4..-1].to_f
|
47
66
|
|
48
67
|
result[:sell][currency] = sell_rate
|
49
68
|
result[:buy][currency] = buy_rate
|
data/lib/egp_rates/bank.rb
CHANGED
@@ -30,24 +30,24 @@ module EGPRates
|
|
30
30
|
# rubocop:disable Metrics/MethodLength
|
31
31
|
def currency_symbol(currency)
|
32
32
|
case currency
|
33
|
-
when /UAE|EMIRATES|Dirham/i
|
34
|
-
when /Australian/i
|
35
|
-
when /Bahrain|BHD/i
|
36
|
-
when /Canadian/i
|
37
|
-
when /Swiss|CHF/i
|
38
|
-
when /Chinese/
|
39
|
-
when /Danish/i
|
40
|
-
when /Euro|EUR/i
|
41
|
-
when /British|Sterling|GBP/i
|
42
|
-
when /Jordanian/i
|
43
|
-
when /Japanese|JPY|YEN/i
|
44
|
-
when /Kuwait/i
|
45
|
-
when /Norwegian/i
|
46
|
-
when /Omani/i
|
47
|
-
when /Qatar/i
|
48
|
-
when /SAR|Saudi/i
|
49
|
-
when /Swidish|Swedish/i
|
50
|
-
when /US Dollar|USD/i
|
33
|
+
when /UAE|EMIRATES|Dirham|AED/i then :AED
|
34
|
+
when /Australian/i then :AUD
|
35
|
+
when /Bahrain|BHD/i then :BHD
|
36
|
+
when /Canadian/i then :CAD
|
37
|
+
when /Swiss|CHF/i then :CHF
|
38
|
+
when /Chinese/ then :CNY
|
39
|
+
when /Danish/i then :DKK
|
40
|
+
when /Euro|EUR/i then :EUR
|
41
|
+
when /British|Sterling|GBP/i then :GBP
|
42
|
+
when /Jordanian/i then :JOD
|
43
|
+
when /Japanese|JPY|YEN/i then :JPY
|
44
|
+
when /Kuwait/i then :KWD
|
45
|
+
when /Norwegian/i then :NOK
|
46
|
+
when /Omani/i then :OMR
|
47
|
+
when /Qatar/i then :QAR
|
48
|
+
when /SAR|Saudi/i then :SAR
|
49
|
+
when /Swidish|Swedish/i then :SEK
|
50
|
+
when /US Dollar|USD/i then :USD
|
51
51
|
else fail ResponseError, "Unknown currency #{currency}"
|
52
52
|
end
|
53
53
|
end
|
data/spec/cassettes/ADIB.gz
CHANGED
Binary file
|
data/spec/egp_rates/adib_spec.rb
CHANGED
@@ -44,7 +44,7 @@ describe EGPRates::ADIB do
|
|
44
44
|
|
45
45
|
it 'returns <#Enumerator::Lazy> of 5 rows', vcr: { cassette_name: :ADIB } do
|
46
46
|
lazy_enumerator = bank.send(:raw_exchange_rates)
|
47
|
-
expect(lazy_enumerator).to be_a
|
47
|
+
expect(lazy_enumerator).to be_a Array
|
48
48
|
expect(lazy_enumerator.size).to eq 5
|
49
49
|
end
|
50
50
|
end
|
@@ -54,21 +54,21 @@ describe EGPRates::ADIB do
|
|
54
54
|
|
55
55
|
it 'returns sell: hash of selling prices' do
|
56
56
|
expect(bank.send(:parse, raw_data)[:sell]).to match(
|
57
|
-
AED:
|
58
|
-
EUR: 19.
|
59
|
-
GBP: 23.
|
60
|
-
SAR:
|
61
|
-
USD: 18.
|
57
|
+
AED: 5.132,
|
58
|
+
EUR: 19.7548,
|
59
|
+
GBP: 23.1365,
|
60
|
+
SAR: 5.0264,
|
61
|
+
USD: 18.85
|
62
62
|
)
|
63
63
|
end
|
64
64
|
|
65
65
|
it 'returns buy: hash of buying prices' do
|
66
66
|
expect(bank.send(:parse, raw_data)[:buy]).to match(
|
67
|
-
AED: 4.
|
68
|
-
EUR: 18.
|
69
|
-
GBP: 22.
|
70
|
-
SAR: 4.
|
71
|
-
USD:
|
67
|
+
AED: 4.9279,
|
68
|
+
EUR: 18.8403,
|
69
|
+
GBP: 22.1019,
|
70
|
+
SAR: 4.8042,
|
71
|
+
USD: 18.1
|
72
72
|
)
|
73
73
|
end
|
74
74
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: EGP_Rates
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.0.
|
4
|
+
version: 1.0.6
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Ahmed Abdel-Razzak
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2016-12-
|
11
|
+
date: 2016-12-31 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|