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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: bc578abf111e726a7fbf7b3ebd3245adc44841ad
4
- data.tar.gz: 318379903610733e0520ae35d5f6426b454fc80e
3
+ metadata.gz: f44e100e103d834ee8434b362fb912b4181c82e2
4
+ data.tar.gz: 78a5784277b79b6716febce6a187a079b3a9d9cd
5
5
  SHA512:
6
- metadata.gz: d0e65ba51db3d24a150c035be3f19950557cbcbbd1c055e36ac56acc7cbcbb9587608084fd8744fcc185bd2da57d1fcb46ca25b2aee3e61982a6c7df75f07a1d
7
- data.tar.gz: b21f08a05f139043f7c037317d639e56dfae90441c1b7eb3e67e66abacd63330af10b99616c52bce84e67d2e43e4799b2d263352bc83cc7c22384b16a6596545
6
+ metadata.gz: 2c8eeb9acc8abbe77130627cdd5f98da7814c0b1fa9cf6e9fbabad877f3bb7e453a67e0957028359fbd84e981ba44984a73987610059134df004e4185fa56a99
7
+ data.tar.gz: 99f2c7eca30933e121bd8ed0c04ebf1609dfd81338fafaed46af5f2a60cbf80cb71754feb4b174d6795c57579ecd7bacc0ccbb032fe243ddc99e7a028cecc87a
@@ -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 [Enumerator::Lazy] with the table row in HTML that evaluates to
22
+ # @return [Array<Array>] containing image url (currency) and exchange rates
23
23
  # [
24
- # ["\n", "USD", "\n", "17.0000", "\n", "17.5000", "\n"],
25
- # ["\n", "GBP", "\n", "20.9049", "\n", "21.7630", "\n"],
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('.CallUs tbody tr')
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 == 9
32
- table_rows.lazy.drop(4).map(&:children).map { |cell| cell.map(&:text) }
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
- sell_rate = row[5].to_f
45
- buy_rate = row[3].to_f
46
- currency = row[1].to_sym
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
@@ -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 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
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
Binary file
@@ -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 Enumerator::Lazy
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: 4.928,
58
- EUR: 19.3471,
59
- GBP: 23.054,
60
- SAR: 4.8269,
61
- USD: 18.1
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.8055,
68
- EUR: 18.7496,
69
- GBP: 22.1772,
70
- SAR: 4.706,
71
- USD: 17.65
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.5
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-04 00:00:00.000000000 Z
11
+ date: 2016-12-31 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler