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-type
of 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 – will do nothing if the response has already been parsed.
-
#parse! ⇒ Chimps::Response
Parse the response from Infochimps.
-
#print(options = {}) ⇒ Object
Print this response.
- #print_headers(options = {}) ⇒ Object
-
#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.
29 30 31 32 33 |
# File 'lib/chimps/response.rb', line 29 def initialize body, ={} super() @body = body @error = [:error] end |
Instance Attribute Details
#body ⇒ Object (readonly)
The response body.
10 11 12 |
# File 'lib/chimps/response.rb', line 10 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.
17 18 19 |
# File 'lib/chimps/response.rb', line 17 def error @error end |
Instance Method Details
#code ⇒ Integer
The HTTP status code of the response.
38 39 40 |
# File 'lib/chimps/response.rb', line 38 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
.
55 56 57 58 59 60 61 |
# File 'lib/chimps/response.rb', line 55 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.
107 108 109 110 111 112 113 |
# File 'lib/chimps/response.rb', line 107 def data {}.tap do |d| each_pair do |key, value| d[key] = value end end end |
#error? ⇒ true, false
Was this response an error??
97 98 99 |
# File 'lib/chimps/response.rb', line 97 def error? !! @error end |
#headers ⇒ Hash
The HTTP headers of the response.
45 46 47 |
# File 'lib/chimps/response.rb', line 45 def headers @headers ||= body.headers end |
#parse ⇒ Object
Parse the response from Infochimps – will do nothing if the response has already been parsed.
82 83 84 85 |
# File 'lib/chimps/response.rb', line 82 def parse return if @parsed parse! end |
#parse! ⇒ Chimps::Response
Parse the response from Infochimps.
66 67 68 69 70 71 72 73 74 75 76 77 78 |
# File 'lib/chimps/response.rb', line 66 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 when String then self[:string] = data else nil end @parsed = true self end |
#print(options = {}) ⇒ Object
Print this response.
119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 |
# File 'lib/chimps/response.rb', line 119 def print ={} $stderr.puts(diagnostic_line) if error? || Chimps.verbose? output = ([:to] || $stdout) if error? parse! output.puts self['errors'] if self['errors'] output.puts self['message'] if self['message'] else case when [:yaml] parse! output.puts self.to_yaml when [:json] && [:pretty] parse! if [:pretty] output.puts JSON.pretty_generate(self) else output.puts self.to_json end when headers[:content_type] =~ /json/i && [:pretty] parse! output.puts JSON.pretty_generate(self) when headers[:content_type] =~ /tab/i && [:pretty] Utils::Typewriter.new(self).print else output.puts body unless body.chomp.strip.size == 0 end end end |
#print_headers(options = {}) ⇒ Object
149 150 151 152 153 154 |
# File 'lib/chimps/response.rb', line 149 def print_headers ={} output = ([:output] || $stdout) self.body.raw_headers.each_pair do |name, value| output.puts "#{name}: #{value}" end end |
#success? ⇒ true, false
Was this response a success?
90 91 92 |
# File 'lib/chimps/response.rb', line 90 def success? ! error? end |