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, response)
22 @reference = reference
26 attr_reader :reference
31 return @response.status
35 return @response.headers
43 @media_upload ||= Google::APIClient::ResumableUpload.new(self, reference.media, self.headers['location'])
47 _, content_type = self.headers.detect do |h, v|
48 h.downcase == 'Content-Type'.downcase
50 content_type[/^([^;]*);?.*$/, 1].strip.downcase
54 return self.response.status >= 400
63 if self.data.respond_to?(:error) &&
64 self.data.error.respond_to?(:message)
65 # You're going to get a terrible error message if the response isn't
66 # parsed successfully as an error.
67 return self.data.error.message
68 elsif self.data['error'] && self.data['error']['message']
69 return self.data['error']['message']
76 self.media_type == 'application/json'
80 return @data ||= (begin
81 media_type = self.media_type
84 when 'application/json'
85 data = MultiJson.load(data)
86 # Strip data wrapper, if present
87 data = data['data'] if data.has_key?('data')
90 "Content-Type not supported for parsing: #{media_type}"
92 if @reference.api_method && @reference.api_method.response_schema
93 # Automatically parse using the schema designated for the
94 # response of this API method.
95 data = @reference.api_method.response_schema.new(data)
98 # Otherwise, return the raw unparsed value.
99 # This value must be indexable like a Hash.
114 if self.data.respond_to?(:next_page_token)
115 return self.data.next_page_token
116 elsif self.data.respond_to?(:[])
117 return self.data["nextPageToken"]
119 raise TypeError, "Data object did not respond to #next_page_token."
124 merged_parameters = Hash[self.reference.parameters].merge({
125 self.page_token_param => self.next_page_token
127 # Because References can be coerced to Hashes, we can merge them,
128 # preserving all context except the API method parameters that we're
129 # using for pagination.
130 return Google::APIClient::Reference.new(
131 Hash[self.reference].merge(:parameters => merged_parameters)
136 if self.data.respond_to?(:prev_page_token)
137 return self.data.prev_page_token
138 elsif self.data.respond_to?(:[])
139 return self.data["prevPageToken"]
141 raise TypeError, "Data object did not respond to #next_page_token."
146 merged_parameters = Hash[self.reference.parameters].merge({
147 self.page_token_param => self.prev_page_token
149 # Because References can be coerced to Hashes, we can merge them,
150 # preserving all context except the API method parameters that we're
151 # using for pagination.
152 return Google::APIClient::Reference.new(
153 Hash[self.reference].merge(:parameters => merged_parameters)