1 # Copyright 2010 Google Inc.
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
7 # http://www.apache.org/licenses/LICENSE-2.0
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
19 # This class wraps a result returned by an API call.
21 def initialize(reference, request, response)
22 @reference = reference
27 attr_reader :reference
34 return @response.status
38 return @response.headers
46 @media_upload ||= Google::APIClient::ResumableUpload.new(self, reference.media, self.headers['location'])
50 _, content_type = self.headers.detect do |h, v|
51 h.downcase == 'Content-Type'.downcase
53 content_type[/^([^;]*);?.*$/, 1].strip.downcase
57 return self.response.status >= 400
66 if self.data.respond_to?(:error) &&
67 self.data.error.respond_to?(:message)
68 # You're going to get a terrible error message if the response isn't
69 # parsed successfully as an error.
70 return self.data.error.message
71 elsif self.data['error'] && self.data['error']['message']
72 return self.data['error']['message']
79 self.media_type == 'application/json'
83 return @data ||= (begin
84 media_type = self.media_type
87 when 'application/json'
88 data = MultiJson.load(data)
89 # Strip data wrapper, if present
90 data = data['data'] if data.has_key?('data')
93 "Content-Type not supported for parsing: #{media_type}"
95 if @reference.api_method && @reference.api_method.response_schema
96 # Automatically parse using the schema designated for the
97 # response of this API method.
98 data = @reference.api_method.response_schema.new(data)
101 # Otherwise, return the raw unparsed value.
102 # This value must be indexable like a Hash.
117 if self.data.respond_to?(:next_page_token)
118 return self.data.next_page_token
119 elsif self.data.respond_to?(:[])
120 return self.data["nextPageToken"]
122 raise TypeError, "Data object did not respond to #next_page_token."
127 merged_parameters = Hash[self.reference.parameters].merge({
128 self.page_token_param => self.next_page_token
130 # Because References can be coerced to Hashes, we can merge them,
131 # preserving all context except the API method parameters that we're
132 # using for pagination.
133 return Google::APIClient::Reference.new(
134 Hash[self.reference].merge(:parameters => merged_parameters)
139 if self.data.respond_to?(:prev_page_token)
140 return self.data.prev_page_token
141 elsif self.data.respond_to?(:[])
142 return self.data["prevPageToken"]
144 raise TypeError, "Data object did not respond to #next_page_token."
149 merged_parameters = Hash[self.reference.parameters].merge({
150 self.page_token_param => self.prev_page_token
152 # Because References can be coerced to Hashes, we can merge them,
153 # preserving all context except the API method parameters that we're
154 # using for pagination.
155 return Google::APIClient::Reference.new(
156 Hash[self.reference].merge(:parameters => merged_parameters)