Class: EodData::WebResponse
- Inherits:
-
Object
- Object
- EodData::WebResponse
- Defined in:
- lib/eod_data/web_response.rb
Overview
Represents an XML response from EODData, encapsulates the XML root element.
Instance Attribute Summary collapse
-
#root ⇒ Object
writeonly
Sets the attribute root.
Instance Method Summary collapse
-
#elements_at(path, name = path.singularize) ⇒ Object
Collects elements at
path
whose element name matchesname
; where the name defaults to the path in singular, e.g. -
#initialize(root) ⇒ WebResponse
constructor
The XML root element should have the name RESPONSE or LOGINRESPONSE, or for that matter anything ending with RESPONSE.
- #name ⇒ Object
- #respond_to?(symbol) ⇒ Boolean
Constructor Details
#initialize(root) ⇒ WebResponse
The XML root element should have the name RESPONSE or LOGINRESPONSE, or for that matter anything ending with RESPONSE. LOGINRESPONSE applies to token-access responses.
11 12 13 14 |
# File 'lib/eod_data/web_response.rb', line 11 def initialize(root) raise WebError, "Invalid response name #{root.name}" unless root.name.end_with?('RESPONSE') @root = root end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(symbol, *args) ⇒ Object (private)
Sending method something_string
looks for attribute something
and answers its value.
46 47 48 49 50 51 |
# File 'lib/eod_data/web_response.rb', line 46 def method_missing(symbol, *args) return @root.attribute(symbol.to_s.sub(/_string$/, '').capitalize).to_s if symbol.to_s.end_with?('_string') return @root.at(symbol.to_s.sub(/_text$/, '').upcase).text if symbol.to_s.end_with?('_text') return elements_at(symbol.to_s.sub(/_/, '').upcase) if symbol.to_s.end_with?('s') super end |
Instance Attribute Details
#root=(value) ⇒ Object (writeonly)
Sets the attribute root
6 7 8 |
# File 'lib/eod_data/web_response.rb', line 6 def root=(value) @root = value end |
Instance Method Details
#elements_at(path, name = path.singularize) ⇒ Object
Collects elements at path
whose element name matches name
; where the name defaults to the path in singular, e.g. path at ITEMS, collecting elements by name ITEM. The answer is an array of hashes.
23 24 25 26 27 28 29 30 31 32 33 |
# File 'lib/eod_data/web_response.rb', line 23 def elements_at(path, name=path.singularize) @root.at(path).elements.select do |element| element.name == name end.map do |element| hash = {} element.attribute_nodes.each do |attr| hash[attr.name.underscore.to_sym] = attr.content end hash end end |
#name ⇒ Object
16 17 18 |
# File 'lib/eod_data/web_response.rb', line 16 def name @root.name end |
#respond_to?(symbol) ⇒ Boolean
35 36 37 38 39 40 |
# File 'lib/eod_data/web_response.rb', line 35 def respond_to?(symbol) return true if symbol.to_s.end_with?('_string') return true if symbol.to_s.end_with?('_text') return true if symbol.to_s.end_with?('s') super end |