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.
16 require 'addressable/uri'
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'
26 # A service that has been described by a discovery document.
30 # Creates a description of a particular version of a service.
32 # @param [String] document_base
33 # Base URI for the service
34 # @param [Hash] discovery_document
35 # The section of the discovery document that applies to this service
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 }
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 }
57 # @return [String] unparsed discovery document for the API
58 attr_reader :discovery_document
61 # Returns the id of the service.
63 # @return [String] The service id.
66 @discovery_document['id'] ||
67 "#{self.name}:#{self.version}"
72 # Returns the identifier for the service.
74 # @return [String] The service identifier.
76 return @discovery_document['name']
80 # Returns the version of the service.
82 # @return [String] The service version.
84 return @discovery_document['version']
88 # Returns a human-readable title for the API.
90 # @return [Hash] The API title.
92 return @discovery_document['title']
96 # Returns a human-readable description of the API.
98 # @return [Hash] The API description.
100 return @discovery_document['description']
104 # Returns a URI for the API documentation.
106 # @return [Hash] The API documentation.
108 return Addressable::URI.parse(@discovery_document['documentationLink'])
112 # Returns true if this is the preferred version of this API.
114 # @return [TrueClass, FalseClass]
115 # Whether or not this is the preferred version of this API.
117 return !!@discovery_document['preferred']
121 # Returns the list of API features.
124 # The features supported by this API.
126 return @discovery_document['features'] || []
130 # Returns true if this API uses a data wrapper.
132 # @return [TrueClass, FalseClass]
133 # Whether or not this API uses a data wrapper.
135 return self.features.include?('dataWrapper')
139 # Returns the base URI for the discovery document.
141 # @return [Addressable::URI] The base URI.
142 attr_reader :document_base
145 # Returns the base URI for this version of the service.
147 # @return [Addressable::URI] The base URI that methods are joined to.
149 if @discovery_document['basePath']
150 return @method_base ||= (
151 self.document_base.join(Addressable::URI.parse(@discovery_document['basePath']))
159 # Updates the hierarchy of resources and methods with the new base.
161 # @param [Addressable::URI, #to_str, String] new_method_base
162 # The new base URI to use for the service.
163 def method_base=(new_method_base)
164 @method_base = Addressable::URI.parse(new_method_base)
165 self.discovered_resources.each do |resource|
166 resource.method_base = @method_base
168 self.discovered_methods.each do |method|
169 method.method_base = @method_base
174 # Returns the base URI for batch calls to this service.
176 # @return [Addressable::URI] The base URI that methods are joined to.
178 if @discovery_document['batchPath']
179 return @batch_path ||= (
180 self.document_base.join(Addressable::URI.parse('/' +
181 @discovery_document['batchPath']))
189 # A list of schemas available for this version of the API.
191 # @return [Hash] A list of {Google::APIClient::Schema} objects.
193 return @schemas ||= (
194 (@discovery_document['schemas'] || []).inject({}) do |accu, (k, v)|
195 accu[k] = Google::APIClient::Schema.parse(self, v)
202 # Returns a schema for a kind value.
204 # @return [Google::APIClient::Schema] The associated Schema object.
205 def schema_for_kind(kind)
206 api_name, schema_name = kind.split('#', 2)
207 if api_name != self.name
209 "The kind does not match this API. " +
210 "Expected '#{self.name}', got '#{api_name}'."
212 for k, v in self.schemas
213 return v if k.downcase == schema_name.downcase
219 # A list of resources available at the root level of this version of the
222 # @return [Array] A list of {Google::APIClient::Resource} objects.
223 def discovered_resources
224 return @discovered_resources ||= (
225 (@discovery_document['resources'] || []).inject([]) do |accu, (k, v)|
226 accu << Google::APIClient::Resource.new(
227 self, self.method_base, k, v
235 # A list of methods available at the root level of this version of the
238 # @return [Array] A list of {Google::APIClient::Method} objects.
239 def discovered_methods
240 return @discovered_methods ||= (
241 (@discovery_document['methods'] || []).inject([]) do |accu, (k, v)|
242 accu << Google::APIClient::Method.new(self, self.method_base, k, v)
249 # Allows deep inspection of the discovery document.
251 return @discovery_document[key]
255 # Converts the service to a flat mapping of RPC names and method objects.
257 # @return [Hash] All methods available on the service.
260 # # Discover available methods
261 # method_names = client.discovered_api('buzz').to_h.keys
263 return @hash ||= (begin
265 self.discovered_methods.each do |method|
266 methods_hash[method.id] = method
268 self.discovered_resources.each do |resource|
269 methods_hash.merge!(resource.to_h)
276 # Returns a <code>String</code> representation of the service's state.
278 # @return [String] The service's state, as a <code>String</code>.
281 "#<%s:%#0x ID:%s>", self.class.to_s, self.object_id, self.id
286 # Marshalling support - serialize the API to a string (doc base + original
287 # discovery document).
289 MultiJson.dump([@document_base.to_s, @discovery_document])
293 # Marshalling support - Restore an API instance from serialized form
295 new(*MultiJson.load(obj))