Sha256: 8d8476d1374102cf03bc6dae6b8c64c08e0782df593456fa16e274f6b72cccfb

Contents?: true

Size: 1.21 KB

Versions: 3

Compression:

Stored size: 1.21 KB

Contents

require 'nokogiri'

module RTurk
  class Response
    include RTurk::XmlUtilities
    
    #
    # In some cases we want more than just a hash parsed from the returned
    # XML. This class is our response object, and it can be extended for more
    # functionality.
    #
    
    attr_reader :xml, :raw_xml
    
    def initialize(response)
      @raw_xml = response
      @xml = Nokogiri::XML(@raw_xml)
      raise_errors
    end
    
    def success?
      @xml.xpath('//Request/IsValid').inner_text.strip == "True"
    end
    
    def errors
      errors = []
      @xml.xpath('//Errors').each do |error|
        errors << {:code => error.xpath('Error/Code').inner_text,
          :message => error.xpath('Error/Message').inner_text}
      end
      errors
    end
    
    def humanized_errors
      string = self.errors.inject('') { |str, error|
        str + "#{error[:code]}: #{error[:message]}"
      }
      string
    end
    
    def raise_errors
      raise InvalidRequest, self.humanized_errors unless self.success?
    end
    
    def [](element_name)
      self.elements[element_name]
    end
    
    def xpath(*args)
      self.xml.xpath(*args)
    end
    
    def elements
      xml_to_hash(@xml)
    end
    
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
rturk-2.0.2 lib/rturk/response.rb
rturk-2.0.1 lib/rturk/response.rb
rturk-2.0.0 lib/rturk/response.rb