Module: Exchanges::Rates
- Included in:
- Nbp
- Defined in:
- lib/exchanges/rates.rb
Constant Summary collapse
- NBP =
'http://www.nbp.pl/kursy/xml/'
Instance Attribute Summary collapse
-
#date ⇒ Object
Returns the value of attribute date.
-
#selected_currencies ⇒ Object
Returns the value of attribute selected_currencies.
Instance Method Summary collapse
- #codes ⇒ Object
- #filename ⇒ Object
- #published_at ⇒ Object
- #rates(currency) ⇒ Object
- #url ⇒ Object
- #xml ⇒ Object
Instance Attribute Details
#date ⇒ Object
Returns the value of attribute date.
10 11 12 |
# File 'lib/exchanges/rates.rb', line 10 def date @date end |
#selected_currencies ⇒ Object
Returns the value of attribute selected_currencies.
10 11 12 |
# File 'lib/exchanges/rates.rb', line 10 def selected_currencies @selected_currencies end |
Instance Method Details
#codes ⇒ Object
14 15 16 17 |
# File 'lib/exchanges/rates.rb', line 14 def codes res = xml.xpath("//pozycja/kod_waluty/text()").map {|c| c.to_s } @selected_currencies.empty? ? res : res.select {|c| @selected_currencies.include?(c)} end |
#filename ⇒ Object
32 33 34 35 36 37 38 39 40 |
# File 'lib/exchanges/rates.rb', line 32 def filename # according to: http://www.nbp.pl/home.aspx?f=/kursy/instrukcja_pobierania_kursow_walut.html index = Net::HTTP.get(URI.parse(NBP + 'dir.txt')) while index[/(a[0-9][0-9][0-9]z#{@date.strftime("%y%m%d")}+)/, 1].nil? do @date -= 1 end index[/(a[0-9][0-9][0-9]z#{@date.strftime("%y%m%d")}+)/, 1] end |
#published_at ⇒ Object
28 29 30 |
# File 'lib/exchanges/rates.rb', line 28 def published_at Date.parse(xml.xpath("//tabela_kursow/data_publikacji/text()").to_s) end |
#rates(currency) ⇒ Object
19 20 21 22 23 24 25 26 |
# File 'lib/exchanges/rates.rb', line 19 def rates(currency) rate = {} rate[:symbol] = xml.xpath("//pozycja[kod_waluty='#{currency}']/kod_waluty/text()").to_s rate[:name] = xml.xpath("//pozycja[kod_waluty='#{currency}']/nazwa_waluty/text()").to_s.force_encoding('iso-8859-2').encode('utf-8') rate[:base] = xml.xpath("//pozycja[kod_waluty='#{currency}']/przelicznik/text()").to_s.gsub(',', '.').to_f rate[:average_rate] = xml.xpath("//pozycja[kod_waluty='#{currency}']/kurs_sredni/text()").to_s.gsub(',', '.').to_f rate end |
#url ⇒ Object
42 43 44 |
# File 'lib/exchanges/rates.rb', line 42 def url File.join(NBP, filename + '.xml') if filename end |
#xml ⇒ Object
46 47 48 49 50 51 52 53 54 55 |
# File 'lib/exchanges/rates.rb', line 46 def xml begin doc = open(url) rescue Exception => e puts e. puts e.backtrace.inspect end Nokogiri::HTML(doc) if doc end |