Class: AmazonRuby::Response
- Inherits:
-
Object
- Object
- AmazonRuby::Response
- Defined in:
- lib/amazon-ruby/response.rb
Instance Method Summary collapse
- #error_count ⇒ Object
- #errors ⇒ Object
- #find(attribute) ⇒ Object (also: #[])
-
#initialize(response_body, response_code) ⇒ Response
constructor
A new instance of Response.
-
#parse_xml(xml_input) ⇒ Object
Such an ugly hacky mess…
- #response_body ⇒ Object
- #response_code ⇒ Object
- #valid_response ⇒ Object
-
#xml ⇒ Object
The XML document.
Constructor Details
#initialize(response_body, response_code) ⇒ Response
Returns a new instance of Response.
12 13 14 15 16 |
# File 'lib/amazon-ruby/response.rb', line 12 def initialize(response_body, response_code) @response_body = response_body @response_code = response_code.to_i @response_xml = nil end |
Instance Method Details
#error_count ⇒ Object
27 28 29 |
# File 'lib/amazon-ruby/response.rb', line 27 def error_count errors.count end |
#errors ⇒ Object
18 19 20 |
# File 'lib/amazon-ruby/response.rb', line 18 def errors find('Error') end |
#find(attribute) ⇒ Object Also known as: []
22 23 24 |
# File 'lib/amazon-ruby/response.rb', line 22 def find(attribute) xml.xpath("//xmlns:#{attribute}").map { |e| parse_xml(e) } end |
#parse_xml(xml_input) ⇒ Object
Such an ugly hacky mess…
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 |
# File 'lib/amazon-ruby/response.rb', line 41 def parse_xml(xml_input) case xml_input when Nokogiri::XML::Document parse_xml(xml_input.root) when Nokogiri::XML::Element hsh = {} xml_input.attributes.each_pair do |key, attr| hsh[key] = attr.value end xml_input.children.each do |child| result = parse_xml(child) if child.name == 'text' if hsh.empty? return result else hsh['__content__'] = result end elsif hsh[child.name] case hsh[child.name] when Array hsh[child.name] << result else hsh[child.name] = [hsh[child.name]] << result end else hsh[child.name] = result end end hsh else xml_input.content.to_s end end |
#response_body ⇒ Object
4 5 6 |
# File 'lib/amazon-ruby/response.rb', line 4 def response_body @response_body end |
#response_code ⇒ Object
8 9 10 |
# File 'lib/amazon-ruby/response.rb', line 8 def response_code @response_code end |
#valid_response ⇒ Object
31 32 33 |
# File 'lib/amazon-ruby/response.rb', line 31 def valid_response response_body == 200 ? true : false; end |
#xml ⇒ Object
The XML document.
36 37 38 |
# File 'lib/amazon-ruby/response.rb', line 36 def xml Nokogiri::XML(@response_body) end |