class PriceGrabber::Response

Represents a response from the PriceGrabber API.

Public Class Methods

new(response, wants) click to toggle source

@param response [HTTPI::Response] The XML response from Pricegrabber’s API @param wants [Array<String>] Attributes from the response that should be made available within this response

# File lib/pricegrabber/response.rb, line 10
def initialize(response, wants)
  @status = response.code
  resp_hash = Hash.from_xml(response.body)
  @error = resp_hash["document"]["error"]
  @results = []
  [resp_hash["document"]["product"]].flatten.each do |product|
    curr_result = ResponseItem.with_attributes(wants)
    wants.map do |want|
      parts = want.split(".")
      curr = parts.shift
      curr_value = product
      while curr && curr_value
        curr_value = curr_value[curr]
        curr = parts.shift
      end

      curr_result.public_send(:"#{want.tr(".", "_")}=", curr_value)
    end
    @results << curr_result unless curr_result.empty?
  end
end

Public Instance Methods

each(*args, &block) click to toggle source
# File lib/pricegrabber/response.rb, line 32
def each(*args, &block)
  @results.each(*args, &block)
end
successful?() click to toggle source
# File lib/pricegrabber/response.rb, line 36
def successful?
  @status < 300 && @status >= 200 && @error == nil
end