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.
15 require 'google/api_client/parsers/json_parser'
20 # This class wraps a result returned by an API call.
22 def initialize(reference, request, response)
23 @reference = reference
28 attr_reader :reference
43 return @body ||= (begin
44 response_body = @response[2]
45 merged_body = (response_body.inject(StringIO.new) do |accu, chunk|
53 return @data ||= (begin
54 _, content_type = self.headers.detect do |h, v|
55 h.downcase == 'Content-Type'.downcase
58 Google::APIClient::Parser.match_content_type(content_type)
59 parser_type.parse(self.body)
72 if self.data.respond_to?(:next_page_token)
73 return self.data.next_page_token
74 elsif self.data.respond_to?(:[])
75 return self.data["nextPageToken"]
77 raise TypeError, "Data object did not respond to #next_page_token."
82 merged_parameters = Hash[self.reference.parameters].merge({
83 self.page_token_param => self.next_page_token
85 # Because References can be coerced to Hashes, we can merge them,
86 # preserving all context except the API method parameters that we're
87 # using for pagination.
88 return Google::APIClient::Reference.new(
89 Hash[self.reference].merge(:parameters => merged_parameters)
94 if self.data.respond_to?(:prev_page_token)
95 return self.data.prev_page_token
96 elsif self.data.respond_to?(:[])
97 return self.data["prevPageToken"]
99 raise TypeError, "Data object did not respond to #next_page_token."
104 merged_parameters = Hash[self.reference.parameters].merge({
105 self.page_token_param => self.prev_page_token
107 # Because References can be coerced to Hashes, we can merge them,
108 # preserving all context except the API method parameters that we're
109 # using for pagination.
110 return Google::APIClient::Reference.new(
111 Hash[self.reference].merge(:parameters => merged_parameters)