Class: Google::APIClient::Result
- Inherits:
-
Object
- Object
- Google::APIClient::Result
- Extended by:
- Forwardable
- Defined in:
- lib/google/api_client/result.rb
Overview
This class wraps a result returned by an API call.
Direct Known Subclasses
Instance Attribute Summary collapse
-
#body ⇒ String
readonly
HTTP response body.
-
#data ⇒ Object, ...
readonly
Return parsed version of the response body.
-
#data? ⇒ TrueClass, FalseClass
readonly
Check for parsable data in response.
-
#error? ⇒ TrueClass, FalseClass
readonly
Check if request failed.
-
#error_message ⇒ String
readonly
Extracts error messages from the response body.
-
#headers ⇒ Hash
readonly
HTTP response headers.
-
#media_type ⇒ String
readonly
Get the content type of the response.
-
#next_page_token ⇒ String
readonly
Get the token used for requesting the next page of data.
-
#page_token_param ⇒ String
readonly
Name of the field that contains the pagination token.
-
#pagination_type ⇒ Symbol
readonly
Pagination scheme used by this request/response.
-
#prev_page_token ⇒ String
readonly
Get the token used for requesting the previous page of data.
-
#request ⇒ Google::APIClient::Request
(also: #reference)
readonly
Original request object.
-
#response ⇒ Faraday::Response
readonly
HTTP response.
-
#resumable_upload ⇒ Google::APIClient::ResumableUpload
readonly
For resuming media uploads.
-
#status ⇒ Fixnum
readonly
HTTP status code.
-
#success? ⇒ TrueClass, FalseClass
readonly
Check if request was successful.
Instance Method Summary collapse
-
#initialize(request, response) ⇒ Result
constructor
Init the result.
-
#next_page ⇒ Google::APIClient::Request
Build a request for fetching the next page of data.
-
#prev_page ⇒ Google::APIClient::Request
Build a request for fetching the previous page of data.
Constructor Details
#initialize(request, response) ⇒ Result
Init the result
30 31 32 33 34 |
# File 'lib/google/api_client/result.rb', line 30 def initialize(request, response) @request = request @response = response @media_upload = reference if reference.kind_of?(ResumableUpload) end |
Instance Attribute Details
#body ⇒ String (readonly)
Returns HTTP response body.
51 |
# File 'lib/google/api_client/result.rb', line 51 def_delegators :@response, :status, :headers, :body |
#data ⇒ Object, ... (readonly)
Return parsed version of the response body.
137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 |
# File 'lib/google/api_client/result.rb', line 137 def data return @data ||= (begin if self.data? media_type = self.media_type data = self.body case media_type when 'application/json' data = MultiJson.load(data) # Strip data wrapper, if present data = data['data'] if data.has_key?('data') else raise ArgumentError, "Content-Type not supported for parsing: #{media_type}" end if @request.api_method && @request.api_method.response_schema # Automatically parse using the schema designated for the # response of this API method. data = @request.api_method.response_schema.new(data) data else # Otherwise, return the raw unparsed value. # This value must be indexable like a Hash. data end end end) end |
#data? ⇒ TrueClass, FalseClass (readonly)
Check for parsable data in response
127 128 129 |
# File 'lib/google/api_client/result.rb', line 127 def data? !(self.body.nil? || self.body.empty? || self.media_type != 'application/json') end |
#error? ⇒ TrueClass, FalseClass (readonly)
Check if request failed
87 88 89 |
# File 'lib/google/api_client/result.rb', line 87 def error? return self.response.status >= 400 end |
#error_message ⇒ String (readonly)
Extracts error messages from the response body
107 108 109 110 111 112 113 114 115 116 117 118 119 |
# File 'lib/google/api_client/result.rb', line 107 def if self.data? if self.data.respond_to?(:error) && self.data.error.respond_to?(:message) # You're going to get a terrible error message if the response isn't # parsed successfully as an error. return self.data.error. elsif self.data['error'] && self.data['error']['message'] return self.data['error']['message'] end end return self.body end |
#headers ⇒ Hash (readonly)
Returns HTTP response headers.
51 |
# File 'lib/google/api_client/result.rb', line 51 def_delegators :@response, :status, :headers, :body |
#media_type ⇒ String (readonly)
Get the content type of the response
70 71 72 73 74 75 76 77 78 79 |
# File 'lib/google/api_client/result.rb', line 70 def media_type _, content_type = self.headers.detect do |h, v| h.downcase == 'Content-Type'.downcase end if content_type return content_type[/^([^;]*);?.*$/, 1].strip.downcase else return nil end end |
#next_page_token ⇒ String (readonly)
Get the token used for requesting the next page of data
171 172 173 174 175 176 177 178 179 |
# File 'lib/google/api_client/result.rb', line 171 def next_page_token if self.data.respond_to?(:next_page_token) return self.data.next_page_token elsif self.data.respond_to?(:[]) return self.data["nextPageToken"] else raise TypeError, "Data object did not respond to #next_page_token." end end |
#page_token_param ⇒ String (readonly)
Name of the field that contains the pagination token
249 250 251 |
# File 'lib/google/api_client/result.rb', line 249 def page_token_param return "pageToken" end |
#pagination_type ⇒ Symbol (readonly)
Pagination scheme used by this request/response
239 240 241 |
# File 'lib/google/api_client/result.rb', line 239 def pagination_type return :token end |
#prev_page_token ⇒ String (readonly)
Get the token used for requesting the previous page of data
205 206 207 208 209 210 211 212 213 |
# File 'lib/google/api_client/result.rb', line 205 def prev_page_token if self.data.respond_to?(:prev_page_token) return self.data.prev_page_token elsif self.data.respond_to?(:[]) return self.data["prevPageToken"] else raise TypeError, "Data object did not respond to #next_page_token." end end |
#request ⇒ Google::APIClient::Request (readonly) Also known as: reference
Returns Original request object.
37 38 39 |
# File 'lib/google/api_client/result.rb', line 37 def request @request end |
#response ⇒ Faraday::Response (readonly)
Returns HTTP response.
39 40 41 |
# File 'lib/google/api_client/result.rb', line 39 def response @response end |
#resumable_upload ⇒ Google::APIClient::ResumableUpload (readonly)
Returns For resuming media uploads.
55 56 57 58 59 60 61 62 63 |
# File 'lib/google/api_client/result.rb', line 55 def resumable_upload @media_upload ||= ( = self.reference.to_hash.merge( :uri => self.headers['location'], :media => self.reference.media ) Google::APIClient::ResumableUpload.new() ) end |
#status ⇒ Fixnum (readonly)
Returns HTTP status code.
51 |
# File 'lib/google/api_client/result.rb', line 51 def_delegators :@response, :status, :headers, :body |
#success? ⇒ TrueClass, FalseClass (readonly)
Check if request was successful
97 98 99 |
# File 'lib/google/api_client/result.rb', line 97 def success? return !self.error? end |
Instance Method Details
#next_page ⇒ Google::APIClient::Request
Build a request for fetching the next page of data
186 187 188 189 190 191 192 193 194 195 196 197 |
# File 'lib/google/api_client/result.rb', line 186 def next_page return nil unless self.next_page_token merged_parameters = Hash[self.reference.parameters].merge({ self.page_token_param => self.next_page_token }) # Because Requests can be coerced to Hashes, we can merge them, # preserving all context except the API method parameters that we're # using for pagination. return Google::APIClient::Request.new( Hash[self.reference].merge(:parameters => merged_parameters) ) end |
#prev_page ⇒ Google::APIClient::Request
Build a request for fetching the previous page of data
220 221 222 223 224 225 226 227 228 229 230 231 |
# File 'lib/google/api_client/result.rb', line 220 def prev_page return nil unless self.prev_page_token merged_parameters = Hash[self.reference.parameters].merge({ self.page_token_param => self.prev_page_token }) # Because Requests can be coerced to Hashes, we can merge them, # preserving all context except the API method parameters that we're # using for pagination. return Google::APIClient::Request.new( Hash[self.reference].merge(:parameters => merged_parameters) ) end |