Added support for API feature lists.
[arvados.git] / lib / google / api_client / result.rb
1 # Copyright 2010 Google Inc.
2 #
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
6 #
7 #      http://www.apache.org/licenses/LICENSE-2.0
8 #
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.
14
15 require 'google/api_client/parsers/json_parser'
16
17 module Google
18   class APIClient
19     ##
20     # This class wraps a result returned by an API call.
21     class Result
22       def initialize(reference, request, response)
23         @reference = reference
24         @request = request
25         @response = response
26       end
27
28       attr_reader :reference
29
30       attr_reader :request
31
32       attr_reader :response
33
34       def status
35         return @response[0]
36       end
37
38       def headers
39         return @response[1]
40       end
41
42       def body
43         return @body ||= (begin
44           response_body = @response[2]
45           merged_body = (response_body.inject(StringIO.new) do |accu, chunk|
46             accu.write(chunk)
47             accu
48           end).string
49         end)
50       end
51
52       def data
53         return @data ||= (begin
54           _, content_type = self.headers.detect do |h, v|
55             h.downcase == 'Content-Type'.downcase
56           end
57           parser_type =
58             Google::APIClient::Parser.match_content_type(content_type)
59           parser_type.parse(self.body)
60         end)
61       end
62
63       def pagination_type
64         return :token
65       end
66
67       def page_token_param
68         return "pageToken"
69       end
70
71       def next_page_token
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"]
76         else
77           raise TypeError, "Data object did not respond to #next_page_token."
78         end
79       end
80
81       def next_page
82         merged_parameters = Hash[self.reference.parameters].merge({
83           self.page_token_param => self.next_page_token
84         })
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)
90         )
91       end
92
93       def prev_page_token
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"]
98         else
99           raise TypeError, "Data object did not respond to #next_page_token."
100         end
101       end
102
103       def prev_page
104         merged_parameters = Hash[self.reference.parameters].merge({
105           self.page_token_param => self.prev_page_token
106         })
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)
112         )
113       end
114     end
115   end
116 end