Class: AmazonRuby::Response

Inherits:
Object
  • Object
show all
Defined in:
lib/amazon-ruby/response.rb

Instance Method Summary collapse

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_countObject



27
28
29
# File 'lib/amazon-ruby/response.rb', line 27

def error_count
	errors.count
end

#errorsObject



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_bodyObject



4
5
6
# File 'lib/amazon-ruby/response.rb', line 4

def response_body
	@response_body
end

#response_codeObject



8
9
10
# File 'lib/amazon-ruby/response.rb', line 8

def response_code
	@response_code
end

#valid_responseObject



31
32
33
# File 'lib/amazon-ruby/response.rb', line 31

def valid_response
	response_body == 200 ? true : false;
end

#xmlObject

The XML document.



36
37
38
# File 'lib/amazon-ruby/response.rb', line 36

def xml
	Nokogiri::XML(@response_body)
end