Class: Chimps::Response
Overview
A class to wrap responses from the Infochimps API.
Instance Attribute Summary collapse
-
#body ⇒ Object
readonly
The response body.
-
#error ⇒ Object
readonly
The error message for this response, if it was an error.
Instance Method Summary collapse
-
#code ⇒ Integer
The HTTP status code of the response.
-
#content_type ⇒ Symbol, String
The
Content-typeof the response. -
#data ⇒ Hash
Return a new Hash consisting of the data from this response.
-
#error? ⇒ true, false
Was this response an error??.
-
#headers ⇒ Hash
The HTTP headers of the response.
-
#initialize(body, options = {}) ⇒ Chimps::Response
constructor
Return a response built from a String with the RestClient::Response module mixed-in.
-
#parse! ⇒ Object
Parse the response from Infochimps.
-
#print(options = {}) ⇒ Object
Print this response.
-
#success? ⇒ true, false
Was this response a success?.
Constructor Details
#initialize(body, options = {}) ⇒ Chimps::Response
Return a response built from a String with the RestClient::Response module mixed-in.
If :error is passed then this response is is considered an error with the given message.
26 27 28 29 30 31 |
# File 'lib/chimps/response.rb', line 26 def initialize body, ={} super() @body = body @error = [:error] parse! end |
Instance Attribute Details
#body ⇒ Object (readonly)
The response body.
7 8 9 |
# File 'lib/chimps/response.rb', line 7 def body @body end |
#error ⇒ Object (readonly)
The error message for this response, if it was an error.
This is actually generated within RestClient from the HTTP status code and attached to the response. It is passed in when initializing a Chimps::Response by a Chimps::Request.
14 15 16 |
# File 'lib/chimps/response.rb', line 14 def error @error end |
Instance Method Details
#code ⇒ Integer
The HTTP status code of the response.
36 37 38 |
# File 'lib/chimps/response.rb', line 36 def code @code ||= body.to_i end |
#content_type ⇒ Symbol, String
The Content-type of the response.
Will return :yaml or :json if possible, else just the raw Content-type.
53 54 55 56 57 58 59 |
# File 'lib/chimps/response.rb', line 53 def content_type @content_type ||= case headers[:content_type] when /json/ then :json when /yaml/ then :yaml else headers[:content_type] end end |
#data ⇒ Hash
Return a new Hash consisting of the data from this response.
FIXME This is used when pretty printing – though it shouldn’t be necessary.
93 94 95 96 97 98 99 |
# File 'lib/chimps/response.rb', line 93 def data returning({}) do |d| each_pair do |key, value| d[key] = value end end end |
#error? ⇒ true, false
Was this response an error??
83 84 85 |
# File 'lib/chimps/response.rb', line 83 def error? !! @error end |
#headers ⇒ Hash
The HTTP headers of the response.
43 44 45 |
# File 'lib/chimps/response.rb', line 43 def headers @headers ||= body.headers end |
#parse! ⇒ Object
Parse the response from Infochimps.
62 63 64 65 66 67 68 69 70 71 |
# File 'lib/chimps/response.rb', line 62 def parse! data = parse_response_body case data # hack...sometimes we get back an array instead of a # hash...should change the API at Chimps end when Hash then merge!(data) when Array then self[:array] = data # see Chimps::Typewriter#accumulate when String then self[:string] = data end end |
#print(options = {}) ⇒ Object
Print this response.
Options are also passed to Chimps::Typewriter.new; consult for details.
108 109 110 111 112 113 |
# File 'lib/chimps/response.rb', line 108 def print ={} out = [:to] || [:out] || $stdout err = [:err] || $stderr err.puts(diagnostic_line) if error? || Chimps.verbose? Typewriter.new(self, ).print(out) end |
#success? ⇒ true, false
Was this response a success?
76 77 78 |
# File 'lib/chimps/response.rb', line 76 def success? ! error? end |