Class: EuCentralBank::RatesDocument

Inherits:
Nokogiri::XML::SAX::Document
  • Object
show all
Defined in:
lib/eu_central_bank/rates_document.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeRatesDocument

Returns a new instance of RatesDocument.



7
8
9
10
11
12
# File 'lib/eu_central_bank/rates_document.rb', line 7

def initialize
  @rates = {}
  @errors = []
  @updated_at = nil
  @current_date = nil
end

Instance Attribute Details

#errorsObject (readonly)

Returns the value of attribute errors.



4
5
6
# File 'lib/eu_central_bank/rates_document.rb', line 4

def errors
  @errors
end

#ratesObject (readonly)

Returns the value of attribute rates.



3
4
5
# File 'lib/eu_central_bank/rates_document.rb', line 3

def rates
  @rates
end

#updated_atObject (readonly)

Returns the value of attribute updated_at.



5
6
7
# File 'lib/eu_central_bank/rates_document.rb', line 5

def updated_at
  @updated_at
end

Instance Method Details

#end_documentObject

Raises:

  • (Nokogiri::XML::XPath::SyntaxError)


41
42
43
# File 'lib/eu_central_bank/rates_document.rb', line 41

def end_document
  raise Nokogiri::XML::XPath::SyntaxError if @rates.empty? || @updated_at.nil?
end

#error(msg) ⇒ Object



14
15
16
17
18
19
20
# File 'lib/eu_central_bank/rates_document.rb', line 14

def error(msg)
  # TODO: remove this workaround after
  # https://github.com/sparklemotion/nokogiri/pull/1872
  # is released
  @errors << msg
  super
end

#start_element(name, attributes = []) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/eu_central_bank/rates_document.rb', line 22

def start_element(name, attributes=[])
  return if name != 'Cube' || attributes.empty?
  begin
    first_name, first_value = attributes[0]
    case first_name
    when 'time'
      @current_date = Time.parse(first_value).to_date
      @updated_at ||= @current_date
      @rates[@current_date] = []
    when 'currency'
      currency = first_value
      _, rate = attributes[1]
      @rates[@current_date] << [currency, rate]
    end
  rescue StandardError => e
    raise Nokogiri::XML::XPath::SyntaxError, e.message
  end
end