Accept repeated parameters
[arvados.git] / 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
18 require 'google/inflection'
19 require 'google/api_client/discovery/resource'
20 require 'google/api_client/discovery/method'
21
22 module Google
23   class APIClient
24     ##
25     # A service that has been described by a discovery document.
26     class API
27
28       ##
29       # Creates a description of a particular version of a service.
30       #
31       # @param [String] api
32       #   The identifier for the service.  Note that while this frequently
33       #   matches the first segment of all of the service's RPC names, this
34       #   should not be assumed.  There is no requirement that these match.
35       # @param [String] version
36       #   The identifier for the service version.
37       # @param [Hash] api_description
38       #   The section of the discovery document that applies to this service
39       #   version.
40       #
41       # @return [Google::APIClient::API] The constructed service object.
42       def initialize(document_base, discovery_document)
43         @document_base = Addressable::URI.parse(document_base)
44         @discovery_document = discovery_document
45         metaclass = (class <<self; self; end)
46         self.resources.each do |resource|
47           method_name = Google::INFLECTOR.underscore(resource.name).to_sym
48           if !self.respond_to?(method_name)
49             metaclass.send(:define_method, method_name) { resource }
50           end
51         end
52         self.methods.each do |method|
53           method_name = Google::INFLECTOR.underscore(method.name).to_sym
54           if !self.respond_to?(method_name)
55             metaclass.send(:define_method, method_name) { method }
56           end
57         end
58       end
59
60       ##
61       # Returns the id of the service.
62       #
63       # @return [String] The service id.
64       def id
65         return @discovery_document['id']
66       end
67
68       ##
69       # Returns the identifier for the service.
70       #
71       # @return [String] The service identifier.
72       def name
73         return @discovery_document['name']
74       end
75
76       ##
77       # Returns the version of the service.
78       #
79       # @return [String] The service version.
80       def version
81         return @discovery_document['version']
82       end
83
84       ##
85       # Returns a human-readable title for the API.
86       #
87       # @return [Hash] The API title.
88       def title
89         return @discovery_document['title']
90       end
91
92       ##
93       # Returns a human-readable description of the API.
94       #
95       # @return [Hash] The API description.
96       def description
97         return @discovery_document['description']
98       end
99
100       ##
101       # Returns a URI for the API documentation.
102       #
103       # @return [Hash] The API documentation.
104       def documentation
105         return Addressable::URI.parse(@discovery_document['documentationLink'])
106       end
107
108       ##
109       # Returns true if this is the preferred version of this API.
110       #
111       # @return [TrueClass, FalseClass]
112       #   Whether or not this is the preferred version of this API.
113       def preferred
114         return !!@discovery_document['preferred']
115       end
116
117       ##
118       # Returns the list of API features.
119       #
120       # @return [Array]
121       #   The features supported by this API.
122       def features
123         return @discovery_document['features'] || []
124       end
125
126       ##
127       # Returns true if this API uses a data wrapper.
128       #
129       # @return [TrueClass, FalseClass]
130       #   Whether or not this API uses a data wrapper.
131       def data_wrapper?
132         return self.features.include?('dataWrapper')
133       end
134
135       ##
136       # Returns the base URI for the discovery document.
137       #
138       # @return [Addressable::URI] The base URI.
139       attr_reader :document_base
140
141       ##
142       # Returns the base URI for this version of the service.
143       #
144       # @return [Addressable::URI] The base URI that methods are joined to.
145       def method_base
146         if @discovery_document['basePath']
147           return @method_base ||= (
148             self.document_base +
149             Addressable::URI.parse(@discovery_document['basePath'])
150           ).normalize
151         else
152           return nil
153         end
154       end
155
156       ##
157       # Updates the hierarchy of resources and methods with the new base.
158       #
159       # @param [Addressable::URI, #to_str, String] new_base
160       #   The new base URI to use for the service.
161       def method_base=(new_method_base)
162         @method_base = Addressable::URI.parse(new_method_base)
163         self.resources.each do |resource|
164           resource.method_base = @method_base
165         end
166         self.methods.each do |method|
167           method.method_base = @method_base
168         end
169       end
170
171       ##
172       # A list of resources available at the root level of this version of the
173       # service.
174       #
175       # @return [Array] A list of {Google::APIClient::Resource} objects.
176       def resources
177         return @resources ||= (
178           (@discovery_document['resources'] || []).inject([]) do |accu, (k, v)|
179             accu << Google::APIClient::Resource.new(self.method_base, k, v)
180             accu
181           end
182         )
183       end
184
185       ##
186       # A list of methods available at the root level of this version of the
187       # service.
188       #
189       # @return [Array] A list of {Google::APIClient::Method} objects.
190       def methods
191         return @methods ||= (
192           (@discovery_document['methods'] || []).inject([]) do |accu, (k, v)|
193             accu << Google::APIClient::Method.new(self.method_base, k, v)
194             accu
195           end
196         )
197       end
198
199       ##
200       # Allows deep inspection of the discovery document.
201       def [](key)
202         return @discovery_document[key]
203       end
204
205       ##
206       # Converts the service to a flat mapping of RPC names and method objects.
207       #
208       # @return [Hash] All methods available on the service.
209       #
210       # @example
211       #   # Discover available methods
212       #   method_names = client.discovered_api('buzz').to_h.keys
213       def to_h
214         return @hash ||= (begin
215           methods_hash = {}
216           self.methods.each do |method|
217             methods_hash[method.id] = method
218           end
219           self.resources.each do |resource|
220             methods_hash.merge!(resource.to_h)
221           end
222           methods_hash
223         end)
224       end
225
226       ##
227       # Returns a <code>String</code> representation of the service's state.
228       #
229       # @return [String] The service's state, as a <code>String</code>.
230       def inspect
231         sprintf(
232           "#<%s:%#0x ID:%s>", self.class.to_s, self.object_id, self.id
233         )
234       end
235     end
236   end
237 end