Class: CzechExchangeRates
- Inherits:
-
Object
- Object
- CzechExchangeRates
- Defined in:
- lib/czech_exchange_rates.rb
Constant Summary collapse
- PAGE_URI =
'http://www.cnb.cz/cs/financni_trhy/devizovy_trh/kurzy_devizoveho_trhu/denni_kurz.txt?date='
Instance Attribute Summary collapse
-
#currency ⇒ Object
readonly
Returns the value of attribute currency.
-
#date ⇒ Object
readonly
Returns the value of attribute date.
-
#exchange_rate ⇒ Object
readonly
Returns the value of attribute exchange_rate.
Instance Method Summary collapse
- #get_exchange_rate(currency, date = Date.new) ⇒ Object
- #get_table(date = Date.new) ⇒ Object
-
#initialize ⇒ CzechExchangeRates
constructor
A new instance of CzechExchangeRates.
Constructor Details
#initialize ⇒ CzechExchangeRates
Returns a new instance of CzechExchangeRates.
10 11 12 |
# File 'lib/czech_exchange_rates.rb', line 10 def initialize @exchange_rate = 0 end |
Instance Attribute Details
#currency ⇒ Object (readonly)
Returns the value of attribute currency.
8 9 10 |
# File 'lib/czech_exchange_rates.rb', line 8 def currency @currency end |
#date ⇒ Object (readonly)
Returns the value of attribute date.
8 9 10 |
# File 'lib/czech_exchange_rates.rb', line 8 def date @date end |
#exchange_rate ⇒ Object (readonly)
Returns the value of attribute exchange_rate.
8 9 10 |
# File 'lib/czech_exchange_rates.rb', line 8 def exchange_rate @exchange_rate end |
Instance Method Details
#get_exchange_rate(currency, date = Date.new) ⇒ Object
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
# File 'lib/czech_exchange_rates.rb', line 16 def get_exchange_rate(currency, date = Date.new) table = get_table(date) first_line = table.split("\n").first table.each_line do |line| if line.match(currency) l = line.split('|') @exchange_rate = l[4].strip.gsub(',','.').to_f @currency = l[3] @date = Date.parse(first_line) return true end end end |
#get_table(date = Date.new) ⇒ Object
34 35 36 37 38 39 |
# File 'lib/czech_exchange_rates.rb', line 34 def get_table(date = Date.new) uri = "#{PAGE_URI}#{date.day}.#{date.month}.#{date.year}" response = Net::HTTP.get_response(URI.parse(uri)) return response.body end |