EGP_Rates 0.1.0 → 0.2.0

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: 226e463d104ebab0ea975d16d7c26ed424e32bd8
4
- data.tar.gz: cb8050bc07da0baaaf33eae5d72beac437eb3265
3
+ metadata.gz: 179ed0bbc6e04914fdf9fec1e526270aaefe14ff
4
+ data.tar.gz: 43296feb1231e90af389eb3372f07135211ef3e6
5
5
  SHA512:
6
- metadata.gz: 85c46b4e3f81dcb391025a11c385cbf7b6d4d43e41183e35c8dca094d9ab3c3a605bf1abca1660ee6c7272603d286b90dd0633fe1cb2985d9e265cdfb2d652c7
7
- data.tar.gz: 08e538c1e7f2daf8f9f82353a84da28aee54245a78a01dd9454407619ba358e3bb2db14688364866cb0ed58a8a5d2f5b46ff00900454f4cb61fbc91b4f07b57a
6
+ metadata.gz: 88e624e998f66d0c356e9ce7142a6dc9b45c31d4376e2ccfd0c1a443e77e2d85abfa94f70aed37c7f2eb5be25e1c13028a019c1d1b0b2f9860cd54e1785cf9a8
7
+ data.tar.gz: a8dac0a7afb3b9f422edbca668a7d7d8e7c744ce70081fca02fbdb06a4d5a72922b45f74e89bd614586a7f08f0738440cbc05ea9c300ee592fe79ce7c74bcfe4
@@ -0,0 +1,88 @@
1
+ # frozen_string_literal: true
2
+ module EGPRates
3
+ # Arab African International Bank
4
+ class AAIB < EGPRates::Bank
5
+ def initialize
6
+ @sym = :AAIB
7
+ @uri = URI.parse('http://aaib.com/services/rates')
8
+ end
9
+
10
+ # @return [Hash] of exchange rates for selling and buying
11
+ # {
12
+ # { sell: { SYM: rate }, { SYM: rate }, ... },
13
+ # { buy: { SYM: rate }, { SYM: rate }, ... }
14
+ # }
15
+ def exchange_rates
16
+ @exchange_rates ||= parse(raw_exchange_rates)
17
+ end
18
+
19
+ private
20
+
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
23
+ # [
24
+ # [
25
+ # "\r\n ",
26
+ # "US DOLLAR"
27
+ # "\r\n "
28
+ # "15.9"
29
+ # "\r\n "
30
+ # "16.05"
31
+ # "\r\n"
32
+ # ], [
33
+ # "\r\n "
34
+ # "EURO CURRENCY"
35
+ # "\r\n "
36
+ # "17.2213"
37
+ # "\r\n "
38
+ # "17.5314"
39
+ # "\r\n "
40
+ # ], [
41
+ # ...
42
+ # ]
43
+ def raw_exchange_rates
44
+ response = Net::HTTP.get_response(@uri)
45
+ fail ResponseError, response.code unless response.is_a? Net::HTTPSuccess
46
+ response = response.body&.gsub("\u0000", '')
47
+ # AAIB provide 7 currencies only
48
+ table_rows = Oga.parse_html(response).css('#rates-table tr')
49
+ fail ResponseError, 'Unknown HTML' unless table_rows&.size == 7
50
+ table_rows.lazy.map(&:children).map { |cell| cell.map(&:text) }
51
+ end
52
+
53
+ # Convert currency string to ISO symbol
54
+ # @param currency [String] "US Dollar"
55
+ # @return [Symbol] :USD ISO currency name
56
+ # rubocop:disable Metrics/CyclomaticComplexity
57
+ def currency_symbol(currency)
58
+ case currency
59
+ when /US/ then :USD
60
+ when /EURO/ then :EUR
61
+ when /STERLING/ then :GBP
62
+ when /SWISS/ then :CHF
63
+ when /SAUDI/ then :SAR
64
+ when /KUWAIT/ then :KWD
65
+ when /DIRHAM/ then :AED
66
+ else fail ResponseError, "Unknown currency #{currency}"
67
+ end
68
+ end
69
+ # rubocop:enable Metrics/CyclomaticComplexity
70
+
71
+ # Parse the #raw_exchange_rates returned in response
72
+ # @return [Hash] of exchange rates for selling and buying
73
+ # {
74
+ # { sell: { SYM: rate }, { SYM: rate }, ... },
75
+ # { buy: { SYM: rate }, { SYM: rate }, ... }
76
+ # }
77
+ def parse(raw_data)
78
+ raw_data.each_with_object(sell: {}, buy: {}) do |row, result|
79
+ sell_rate = row[5].to_f
80
+ buy_rate = row[3].to_f
81
+ currency = currency_symbol(row[1])
82
+
83
+ result[:sell][currency] = sell_rate
84
+ result[:buy][currency] = buy_rate
85
+ end
86
+ end
87
+ end
88
+ end
data/lib/egp_rates/cbe.rb CHANGED
@@ -39,7 +39,6 @@ module EGPRates
39
39
  # @param currency [String] "US Dollar"
40
40
  # @return [Symbol] :USD ISO currency name
41
41
  # rubocop:disable Metrics/CyclomaticComplexity
42
- # rubocop:disable Metrics/MethodLength
43
42
  def currency_symbol(currency)
44
43
  case currency
45
44
  when /US/ then :USD
@@ -55,7 +54,6 @@ module EGPRates
55
54
  end
56
55
  end
57
56
  # rubocop:enable Metrics/CyclomaticComplexity
58
- # rubocop:enable Metrics/MethodLength
59
57
 
60
58
  # Parse the #raw_exchange_rates returned in response
61
59
  # @return [Hash] of exchange rates for selling and buying
@@ -0,0 +1,76 @@
1
+ # frozen_string_literal: true
2
+ module EGPRates
3
+ # National Bank of Egypt
4
+ class CIB < EGPRates::Bank
5
+ def initialize
6
+ @sym = :CIB
7
+ @uri = URI.parse('http://www.cibeg.com/_layouts/15/LINKDev.CIB.CurrenciesFunds/FundsCurrencies.aspx/GetCurrencies')
8
+ end
9
+
10
+ # @return [Hash] of exchange rates for selling and buying
11
+ # {
12
+ # { sell: { SYM: rate }, { SYM: rate }, ... },
13
+ # { buy: { SYM: rate }, { SYM: rate }, ... }
14
+ # }
15
+ def exchange_rates
16
+ @exchange_rates ||= parse(raw_exchange_rates)
17
+ end
18
+
19
+ private
20
+
21
+ # Send the request to URL and return the JSON response
22
+ # @return [Hash] JSON response of the exchange rates
23
+ # {
24
+ # "d"=> [
25
+ # {
26
+ # "__type"=>"LINKDev.CIB.CurrenciesFunds.CIBFund.CurrencyObject",
27
+ # "CurrencyID"=>"USD",
28
+ # "BuyRate"=>15.9,
29
+ # "SellRate"=>16.05
30
+ # }, {
31
+ # "__type"=>"LINKDev.CIB.CurrenciesFunds.CIBFund.CurrencyObject",
32
+ # "CurrencyID"=>"EUR",
33
+ # "BuyRate"=>17.1904,
34
+ # "SellRate"=>17.5234
35
+ # }, {
36
+ # ...
37
+ # }
38
+ # ]
39
+ # }
40
+ def raw_exchange_rates
41
+ req = Net::HTTP::Post.new(@uri, 'Content-Type' => 'application/json')
42
+ req.body = { lang: :en }.to_json
43
+ response = Net::HTTP.start(@uri.hostname, @uri.port) do |http|
44
+ http.request(req)
45
+ end
46
+ fail ResponseError, response.code unless response.is_a? Net::HTTPSuccess
47
+ response = JSON.parse(response.body)
48
+
49
+ # CIB provide 6 currencies only
50
+ unless response['d'] && response['d'].size >= 6
51
+ fail ResponseError, "Unknown JSON #{response}"
52
+ end
53
+
54
+ response
55
+ rescue JSON::ParserError
56
+ raise ResponseError, "Unknown JSON: #{response.body}"
57
+ end
58
+
59
+ # Parse the #raw_exchange_rates returned in response
60
+ # @return [Hash] of exchange rates for selling and buying
61
+ # {
62
+ # { sell: { SYM: rate }, { SYM: rate }, ... },
63
+ # { buy: { SYM: rate }, { SYM: rate }, ... }
64
+ # }
65
+ def parse(raw_data)
66
+ raw_data['d'].each_with_object(sell: {}, buy: {}) do |obj, result|
67
+ sell_rate = obj['SellRate']
68
+ buy_rate = obj['BuyRate']
69
+ currency = obj['CurrencyID'].to_sym
70
+
71
+ result[:sell][currency] = sell_rate
72
+ result[:buy][currency] = buy_rate
73
+ end
74
+ end
75
+ end
76
+ end
data/lib/egp_rates.rb CHANGED
@@ -1,9 +1,12 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require 'oga'
4
+ require 'json'
4
5
  require 'egp_rates/bank'
5
6
  require 'egp_rates/cbe'
6
7
  require 'egp_rates/nbe'
8
+ require 'egp_rates/cib'
9
+ require 'egp_rates/aaib'
7
10
 
8
11
  # Base Module
9
12
  module EGPRates