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 return @data ||= (begin
47 _, content_type = self.headers.detect do |h, v|
48 h.downcase == 'Content-Type'.downcase
50 media_type = content_type[/^([^;]*);?.*$/, 1].strip.downcase
53 when 'application/json'
54 data = MultiJson.decode(data)
55 # Strip data wrapper, if present
56 data = data['data'] if data.has_key?('data')
59 "Content-Type not supported for parsing: #{media_type}"
61 if @reference.api_method && @reference.api_method.response_schema
62 # Automatically parse using the schema designated for the
63 # response of this API method.
64 data = @reference.api_method.response_schema.new(data)
67 # Otherwise, return the raw unparsed value.
68 # This value must be indexable like a Hash.
83 if self.data.respond_to?(:next_page_token)
84 return self.data.next_page_token
85 elsif self.data.respond_to?(:[])
86 return self.data["nextPageToken"]
88 raise TypeError, "Data object did not respond to #next_page_token."
93 merged_parameters = Hash[self.reference.parameters].merge({
94 self.page_token_param => self.next_page_token
96 # Because References can be coerced to Hashes, we can merge them,
97 # preserving all context except the API method parameters that we're
98 # using for pagination.
99 return Google::APIClient::Reference.new(
100 Hash[self.reference].merge(:parameters => merged_parameters)
105 if self.data.respond_to?(:prev_page_token)
106 return self.data.prev_page_token
107 elsif self.data.respond_to?(:[])
108 return self.data["prevPageToken"]
110 raise TypeError, "Data object did not respond to #next_page_token."
115 merged_parameters = Hash[self.reference.parameters].merge({
116 self.page_token_param => self.prev_page_token
118 # Because References can be coerced to Hashes, we can merge them,
119 # preserving all context except the API method parameters that we're
120 # using for pagination.
121 return Google::APIClient::Reference.new(
122 Hash[self.reference].merge(:parameters => merged_parameters)