Class: RemoteAPI::XML::Response

Inherits:
Object
  • Object
show all
Defined in:
lib/remote_api/xml_response.rb

Instance Method Summary collapse

Constructor Details

#initialize(data) ⇒ Response

Returns a new instance of Response.



5
6
7
# File 'lib/remote_api/xml_response.rb', line 5

def initialize(data)
  @data = REXML::Document.new(data.to_s).root
end

Instance Method Details

#[](xpath) ⇒ Object

equivalent to:

xml.elements[xpath].text


11
12
13
14
15
16
17
# File 'lib/remote_api/xml_response.rb', line 11

def [](xpath)
  if element = @data.elements[xpath]
    element.text
  else
    nil
  end
end

#each(xpath) ⇒ Object

equivalent to:

xml.each_element(xpath) { |node| ... }

Raises:

  • (ArgumentError)


21
22
23
24
25
26
# File 'lib/remote_api/xml_response.rb', line 21

def each(xpath)
  raise ArgumentError, 'You must supply a block to the "each" method' unless block_given?
  @data.each_element(xpath) do |node|
    yield self.class.new(node)
  end
end

#to_formatted_sObject



28
29
30
31
# File 'lib/remote_api/xml_response.rb', line 28

def to_formatted_s
  output = ''
  @data.write(output, 0)
end