Class: CzechExchangeRates

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

Constructor Details

#initializeCzechExchangeRates

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

#currencyObject (readonly)

Returns the value of attribute currency.



8
9
10
# File 'lib/czech_exchange_rates.rb', line 8

def currency
  @currency
end

#dateObject (readonly)

Returns the value of attribute date.



8
9
10
# File 'lib/czech_exchange_rates.rb', line 8

def date
  @date
end

#exchange_rateObject (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