Class: RTurk::Response

Inherits:
Parser
  • Object
show all
Defined in:
lib/rturk/parsers/response.rb

Direct Known Subclasses

CreateHITResponse, GetAccountBalanceResponse, GetAssignmentsForHITResponse, GetBonusPaymentsResponse, GetHITResponse, GetQualificationTypeResponse, GetQualificationsForQualificationTypeResponse, GetReviewableHITsResponse, RegisterHITTypeResponse, SearchHITsResponse

Instance Attribute Summary (collapse)

Instance Method Summary (collapse)

Methods included from XMLUtilities

#map_content, #normalize_nested_params, #xml_to_hash

Constructor Details

- (Response) initialize(response)

Returns a new instance of Response



14
15
16
17
18
# File 'lib/rturk/parsers/response.rb', line 14

def initialize(response)
  @raw_xml = response.body
  @xml = Nokogiri::XML(@raw_xml)
  raise_errors
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

- (Object) method_missing(method)



56
57
58
59
60
# File 'lib/rturk/parsers/response.rb', line 56

def method_missing(method)
  if @attributes && @attributes.include?(method)
    @attributes[method]
  end
end

Instance Attribute Details

- (Object) raw_xml (readonly)

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.



12
13
14
# File 'lib/rturk/parsers/response.rb', line 12

def raw_xml
  @raw_xml
end

- (Object) xml (readonly)

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.



12
13
14
# File 'lib/rturk/parsers/response.rb', line 12

def xml
  @xml
end

Instance Method Details

- (Object) [](element_name)



44
45
46
# File 'lib/rturk/parsers/response.rb', line 44

def [](element_name)
  self.elements[element_name]
end

- (Object) elements



52
53
54
# File 'lib/rturk/parsers/response.rb', line 52

def elements
  xml_to_hash(@xml)
end

- (Object) errors



24
25
26
27
28
29
30
31
# File 'lib/rturk/parsers/response.rb', line 24

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

- (Object) humanized_errors



33
34
35
36
37
38
# File 'lib/rturk/parsers/response.rb', line 33

def humanized_errors
  string = self.errors.inject('') { |str, error|
    str + "#{error[:code]}: #{error[:message]}"
  }
  string
end

- (Object) raise_errors

Raises:



40
41
42
# File 'lib/rturk/parsers/response.rb', line 40

def raise_errors
  raise InvalidRequest, self.humanized_errors unless self.success?
end

- (Boolean) success?

Returns:

  • (Boolean)


20
21
22
# File 'lib/rturk/parsers/response.rb', line 20

def success?
  @xml.xpath('//Request/IsValid').inner_text.strip == "True"
end

- (Object) xpath(*args)



48
49
50
# File 'lib/rturk/parsers/response.rb', line 48

def xpath(*args)
  self.xml.xpath(*args)
end