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 self.media_type == 'application/json'
61 return @data ||= (begin
62 media_type = self.media_type
65 when 'application/json'
66 data = MultiJson.load(data)
67 # Strip data wrapper, if present
68 data = data['data'] if data.has_key?('data')
71 "Content-Type not supported for parsing: #{media_type}"
73 if @reference.api_method && @reference.api_method.response_schema
74 # Automatically parse using the schema designated for the
75 # response of this API method.
76 data = @reference.api_method.response_schema.new(data)
79 # Otherwise, return the raw unparsed value.
80 # This value must be indexable like a Hash.
95 if self.data.respond_to?(:next_page_token)
96 return self.data.next_page_token
97 elsif self.data.respond_to?(:[])
98 return self.data["nextPageToken"]
100 raise TypeError, "Data object did not respond to #next_page_token."
105 merged_parameters = Hash[self.reference.parameters].merge({
106 self.page_token_param => self.next_page_token
108 # Because References can be coerced to Hashes, we can merge them,
109 # preserving all context except the API method parameters that we're
110 # using for pagination.
111 return Google::APIClient::Reference.new(
112 Hash[self.reference].merge(:parameters => merged_parameters)
117 if self.data.respond_to?(:prev_page_token)
118 return self.data.prev_page_token
119 elsif self.data.respond_to?(:[])
120 return self.data["prevPageToken"]
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.prev_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)