Class: Chimps::Response
Overview
A class to wrap responses from the Infochimps API.
Instance Attribute Summary collapse
-
#body ⇒ Object
readonly
The response body.
-
#data ⇒ Object
readonly
The parsed data of the 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. -
#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 from the
body
. - #method_missing(name, *args, &block) ⇒ Object
-
#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 from the body
.
If :error
is passed then this response is is considered an error with the given message.
31 32 33 34 |
# File 'lib/chimps/response.rb', line 31 def initialize body, ={} @body = body @error = [:error] end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(name, *args, &block) ⇒ Object
135 136 137 |
# File 'lib/chimps/response.rb', line 135 def method_missing name, *args, &block data.send(name, *args, &block) end |
Instance Attribute Details
#body ⇒ Object (readonly)
The response body.
10 11 12 |
# File 'lib/chimps/response.rb', line 10 def body @body end |
#data ⇒ Object (readonly)
The parsed data of the body
13 14 15 |
# File 'lib/chimps/response.rb', line 13 def data @data 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.
20 21 22 |
# File 'lib/chimps/response.rb', line 20 def error @error end |
Instance Method Details
#code ⇒ Integer
The HTTP status code of the response.
39 40 41 |
# File 'lib/chimps/response.rb', line 39 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
.
56 57 58 59 60 61 62 |
# File 'lib/chimps/response.rb', line 56 def content_type @content_type ||= case headers[:content_type] when /json/ then :json when /yaml/ then :yaml else headers[:content_type] end end |
#error? ⇒ true, false
Was this response an error??
90 91 92 |
# File 'lib/chimps/response.rb', line 90 def error? !! @error end |
#headers ⇒ Hash
The HTTP headers of the response.
46 47 48 |
# File 'lib/chimps/response.rb', line 46 def headers @headers ||= body.headers end |
#parse ⇒ Object
Parse the response from Infochimps – will do nothing if the response has already been parsed.
75 76 77 78 |
# File 'lib/chimps/response.rb', line 75 def parse return if @parsed parse! end |
#parse! ⇒ Chimps::Response
Parse the response from Infochimps.
67 68 69 70 71 |
# File 'lib/chimps/response.rb', line 67 def parse! @data = parse_response_body @parsed = true self end |
#print(options = {}) ⇒ Object
Print this response.
98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 |
# File 'lib/chimps/response.rb', line 98 def print ={} $stderr.puts(diagnostic_line) if error? || Chimps.verbose? output = ([:to] || $stdout) if error? parse! output.puts data['errors'] if data['errors'] output.puts data['message'] if data['message'] else case when [:yaml] parse! output.puts data.to_yaml when [:json] && [:pretty] parse! if [:pretty] output.puts JSON.pretty_generate(data) else output.puts data.to_json end when headers[:content_type] =~ /json/i && [:pretty] parse! output.puts JSON.pretty_generate(data) 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
128 129 130 131 132 133 |
# File 'lib/chimps/response.rb', line 128 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?
83 84 85 |
# File 'lib/chimps/response.rb', line 83 def success? ! error? end |