21700: Install Bundler system-wide in Rails postinst
[arvados.git] / sdk / ruby-google-api-client / lib / google / api_client / discovery / api.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
16 require 'addressable/uri'
17 require 'multi_json'
18 require 'active_support/inflector'
19 require 'google/api_client/discovery/resource'
20 require 'google/api_client/discovery/method'
21 require 'google/api_client/discovery/media'
22
23 module Google
24   class APIClient
25     ##
26     # A service that has been described by a discovery document.
27     class API
28
29       ##
30       # Creates a description of a particular version of a service.
31       #
32       # @param [String] document_base
33       #   Base URI for the discovery document.
34       # @param [Hash] discovery_document
35       #   The section of the discovery document that applies to this service
36       #   version.
37       #
38       # @return [Google::APIClient::API] The constructed service object.
39       def initialize(document_base, discovery_document)
40         @document_base = Addressable::URI.parse(document_base)
41         @discovery_document = discovery_document
42         metaclass = (class << self; self; end)
43         self.discovered_resources.each do |resource|
44           method_name = ActiveSupport::Inflector.underscore(resource.name).to_sym
45           if !self.respond_to?(method_name)
46             metaclass.send(:define_method, method_name) { resource }
47           end
48         end
49         self.discovered_methods.each do |method|
50           method_name = ActiveSupport::Inflector.underscore(method.name).to_sym
51           if !self.respond_to?(method_name)
52             metaclass.send(:define_method, method_name) { method }
53           end
54         end
55       end
56       
57       # @return [String] unparsed discovery document for the API
58       attr_reader :discovery_document
59
60       ##
61       # Returns the id of the service.
62       #
63       # @return [String] The service id.
64       def id
65         return (
66           @discovery_document['id'] ||
67           "#{self.name}:#{self.version}"
68         )
69       end
70
71       ##
72       # Returns the identifier for the service.
73       #
74       # @return [String] The service identifier.
75       def name
76         return @discovery_document['name']
77       end
78
79       ##
80       # Returns the version of the service.
81       #
82       # @return [String] The service version.
83       def version
84         return @discovery_document['version']
85       end
86
87       ##
88       # Returns a human-readable title for the API.
89       #
90       # @return [Hash] The API title.
91       def title
92         return @discovery_document['title']
93       end
94
95       ##
96       # Returns a human-readable description of the API.
97       #
98       # @return [Hash] The API description.
99       def description
100         return @discovery_document['description']
101       end
102
103       ##
104       # Returns a URI for the API documentation.
105       #
106       # @return [Hash] The API documentation.
107       def documentation
108         return Addressable::URI.parse(@discovery_document['documentationLink'])
109       end
110
111       ##
112       # Returns true if this is the preferred version of this API.
113       #
114       # @return [TrueClass, FalseClass]
115       #   Whether or not this is the preferred version of this API.
116       def preferred
117         return !!@discovery_document['preferred']
118       end
119
120       ##
121       # Returns the list of API features.
122       #
123       # @return [Array]
124       #   The features supported by this API.
125       def features
126         return @discovery_document['features'] || []
127       end
128
129       ##
130       # Returns the root URI for this service.
131       #
132       # @return [Addressable::URI] The root URI.
133       def root_uri
134         return @root_uri ||= (
135           Addressable::URI.parse(self.discovery_document['rootUrl'])
136         )
137       end
138
139       ##
140       # Returns true if this API uses a data wrapper.
141       #
142       # @return [TrueClass, FalseClass]
143       #   Whether or not this API uses a data wrapper.
144       def data_wrapper?
145         return self.features.include?('dataWrapper')
146       end
147
148       ##
149       # Returns the base URI for the discovery document.
150       #
151       # @return [Addressable::URI] The base URI.
152       attr_reader :document_base
153
154       ##
155       # Returns the base URI for this version of the service.
156       #
157       # @return [Addressable::URI] The base URI that methods are joined to.
158       def method_base
159         if @discovery_document['basePath']
160           return @method_base ||= (
161             self.root_uri.join(Addressable::URI.parse(@discovery_document['basePath']))
162           ).normalize
163         else
164           return nil
165         end
166       end
167
168       ##
169       # Updates the hierarchy of resources and methods with the new base.
170       #
171       # @param [Addressable::URI, #to_str, String] new_method_base
172       #   The new base URI to use for the service.
173       def method_base=(new_method_base)
174         @method_base = Addressable::URI.parse(new_method_base)
175         self.discovered_resources.each do |resource|
176           resource.method_base = @method_base
177         end
178         self.discovered_methods.each do |method|
179           method.method_base = @method_base
180         end
181       end
182
183       ##
184       # Returns the base URI for batch calls to this service.
185       #
186       # @return [Addressable::URI] The base URI that methods are joined to.
187       def batch_path
188         if @discovery_document['batchPath']
189           return @batch_path ||= (
190             self.document_base.join(Addressable::URI.parse('/' +
191                 @discovery_document['batchPath']))
192           ).normalize
193         else
194           return nil
195         end
196       end
197
198       ##
199       # A list of schemas available for this version of the API.
200       #
201       # @return [Hash] A list of {Google::APIClient::Schema} objects.
202       def schemas
203         return @schemas ||= (
204           (@discovery_document['schemas'] || []).inject({}) do |accu, (k, v)|
205             accu[k] = Google::APIClient::Schema.parse(self, v)
206             accu
207           end
208         )
209       end
210
211       ##
212       # Returns a schema for a kind value.
213       #
214       # @return [Google::APIClient::Schema] The associated Schema object.
215       def schema_for_kind(kind)
216         api_name, schema_name = kind.split('#', 2)
217         if api_name != self.name
218           raise ArgumentError,
219             "The kind does not match this API. " +
220             "Expected '#{self.name}', got '#{api_name}'."
221         end
222         for k, v in self.schemas
223           return v if k.downcase == schema_name.downcase
224         end
225         return nil
226       end
227
228       ##
229       # A list of resources available at the root level of this version of the
230       # API.
231       #
232       # @return [Array] A list of {Google::APIClient::Resource} objects.
233       def discovered_resources
234         return @discovered_resources ||= (
235           (@discovery_document['resources'] || []).inject([]) do |accu, (k, v)|
236             accu << Google::APIClient::Resource.new(
237               self, self.method_base, k, v
238             )
239             accu
240           end
241         )
242       end
243
244       ##
245       # A list of methods available at the root level of this version of the
246       # API.
247       #
248       # @return [Array] A list of {Google::APIClient::Method} objects.
249       def discovered_methods
250         return @discovered_methods ||= (
251           (@discovery_document['methods'] || []).inject([]) do |accu, (k, v)|
252             accu << Google::APIClient::Method.new(self, self.method_base, k, v)
253             accu
254           end
255         )
256       end
257
258       ##
259       # Allows deep inspection of the discovery document.
260       def [](key)
261         return @discovery_document[key]
262       end
263
264       ##
265       # Converts the service to a flat mapping of RPC names and method objects.
266       #
267       # @return [Hash] All methods available on the service.
268       #
269       # @example
270       #   # Discover available methods
271       #   method_names = client.discovered_api('buzz').to_h.keys
272       def to_h
273         return @hash ||= (begin
274           methods_hash = {}
275           self.discovered_methods.each do |method|
276             methods_hash[method.id] = method
277           end
278           self.discovered_resources.each do |resource|
279             methods_hash.merge!(resource.to_h)
280           end
281           methods_hash
282         end)
283       end
284
285       ##
286       # Returns a <code>String</code> representation of the service's state.
287       #
288       # @return [String] The service's state, as a <code>String</code>.
289       def inspect
290         sprintf(
291           "#<%s:%#0x ID:%s>", self.class.to_s, self.object_id, self.id
292         )
293       end
294       
295       ##
296       # Marshalling support - serialize the API to a string (doc base + original 
297       # discovery document).
298       def _dump(level)
299         MultiJson.dump([@document_base.to_s, @discovery_document])
300       end
301       
302       ##
303       # Marshalling support - Restore an API instance from serialized form
304       def self._load(obj)
305         new(*MultiJson.load(obj)) 
306       end
307
308     end
309   end
310 end