Class: NbpExchange::CurrencyXml

Inherits:
Object
  • Object
show all
Defined in:
lib/nbp_exchange/currency_xml.rb

Constant Summary collapse

DIR_URL =
"http://nbp.pl/kursy/xml/dir.txt"
XML_URL =
"http://rss.nbp.pl/kursy/xml2/%{year}/a/%{xml_name}"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(date) ⇒ CurrencyXml

Returns a new instance of CurrencyXml.



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

def initialize(date)
  @date = date
end

Instance Attribute Details

#dateObject

Returns the value of attribute date.



7
8
9
# File 'lib/nbp_exchange/currency_xml.rb', line 7

def date
  @date
end

Instance Method Details

#open_xmlObject



12
13
14
# File 'lib/nbp_exchange/currency_xml.rb', line 12

def open_xml
  open(xml_url).read
end

#xml_nameObject

Raises:



16
17
18
19
20
21
22
23
24
25
26
# File 'lib/nbp_exchange/currency_xml.rb', line 16

def xml_name
  item = nil
  xml_names_list.each_line do |line|
    if line.match /a([0-9]+)z#{@date.strftime '%y%m%d'}/
      item = line
    end
  end
  raise NoXMLForThisDate unless item

  @date.strftime '%y' + item[0, 4] + '.xml'
end

#xml_names_listObject



28
29
30
# File 'lib/nbp_exchange/currency_xml.rb', line 28

def xml_names_list
  @xml_names_list ||= open(DIR_URL).read
end

#xml_urlObject



32
33
34
# File 'lib/nbp_exchange/currency_xml.rb', line 32

def xml_url
  XML_URL % {:year => @date.year, :xml_name => xml_name}
end