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 'google/inflection'
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.
33 # The identifier for the service. Note that while this frequently
34 # matches the first segment of all of the service's RPC names, this
35 # should not be assumed. There is no requirement that these match.
36 # @param [String] version
37 # The identifier for the service version.
38 # @param [Hash] api_description
39 # The section of the discovery document that applies to this service
42 # @return [Google::APIClient::API] The constructed service object.
43 def initialize(document_base, discovery_document)
44 @document_base = Addressable::URI.parse(document_base)
45 @discovery_document = discovery_document
46 metaclass = (class << self; self; end)
47 self.discovered_resources.each do |resource|
48 method_name = Google::INFLECTOR.underscore(resource.name).to_sym
49 if !self.respond_to?(method_name)
50 metaclass.send(:define_method, method_name) { resource }
53 self.discovered_methods.each do |method|
54 method_name = Google::INFLECTOR.underscore(method.name).to_sym
55 if !self.respond_to?(method_name)
56 metaclass.send(:define_method, method_name) { method }
62 # Returns the id of the service.
64 # @return [String] The service id.
67 @discovery_document['id'] ||
68 "#{self.name}:#{self.version}"
73 # Returns the identifier for the service.
75 # @return [String] The service identifier.
77 return @discovery_document['name']
81 # Returns the version of the service.
83 # @return [String] The service version.
85 return @discovery_document['version']
89 # Returns a human-readable title for the API.
91 # @return [Hash] The API title.
93 return @discovery_document['title']
97 # Returns a human-readable description of the API.
99 # @return [Hash] The API description.
101 return @discovery_document['description']
105 # Returns a URI for the API documentation.
107 # @return [Hash] The API documentation.
109 return Addressable::URI.parse(@discovery_document['documentationLink'])
113 # Returns true if this is the preferred version of this API.
115 # @return [TrueClass, FalseClass]
116 # Whether or not this is the preferred version of this API.
118 return !!@discovery_document['preferred']
122 # Returns the list of API features.
125 # The features supported by this API.
127 return @discovery_document['features'] || []
131 # Returns true if this API uses a data wrapper.
133 # @return [TrueClass, FalseClass]
134 # Whether or not this API uses a data wrapper.
136 return self.features.include?('dataWrapper')
140 # Returns the base URI for the discovery document.
142 # @return [Addressable::URI] The base URI.
143 attr_reader :document_base
146 # Returns the base URI for this version of the service.
148 # @return [Addressable::URI] The base URI that methods are joined to.
150 if @discovery_document['basePath']
151 return @method_base ||= (
152 self.document_base.join(Addressable::URI.parse(@discovery_document['basePath']))
160 # Updates the hierarchy of resources and methods with the new base.
162 # @param [Addressable::URI, #to_str, String] new_base
163 # The new base URI to use for the service.
164 def method_base=(new_method_base)
165 @method_base = Addressable::URI.parse(new_method_base)
166 self.discovered_resources.each do |resource|
167 resource.method_base = @method_base
169 self.discovered_methods.each do |method|
170 method.method_base = @method_base
175 # Returns the base URI for batch calls to this service.
177 # @return [Addressable::URI] The base URI that methods are joined to.
179 if @discovery_document['batchPath']
180 return @batch_path ||= (
181 self.document_base.join(Addressable::URI.parse('/' +
182 @discovery_document['batchPath']))
190 # A list of schemas available for this version of the API.
192 # @return [Hash] A list of {Google::APIClient::Schema} objects.
194 return @schemas ||= (
195 (@discovery_document['schemas'] || []).inject({}) do |accu, (k, v)|
196 accu[k] = Google::APIClient::Schema.parse(self, v)
203 # Returns a schema for a kind value.
205 # @return [Google::APIClient::Schema] The associated Schema object.
206 def schema_for_kind(kind)
207 api_name, schema_name = kind.split('#', 2)
208 if api_name != self.name
210 "The kind does not match this API. " +
211 "Expected '#{self.name}', got '#{api_name}'."
213 for k, v in self.schemas
214 return v if k.downcase == schema_name.downcase
220 # A list of resources available at the root level of this version of the
223 # @return [Array] A list of {Google::APIClient::Resource} objects.
224 def discovered_resources
225 return @discovered_resources ||= (
226 (@discovery_document['resources'] || []).inject([]) do |accu, (k, v)|
227 accu << Google::APIClient::Resource.new(
228 self, self.method_base, k, v
236 # A list of methods available at the root level of this version of the
239 # @return [Array] A list of {Google::APIClient::Method} objects.
240 def discovered_methods
241 return @discovered_methods ||= (
242 (@discovery_document['methods'] || []).inject([]) do |accu, (k, v)|
243 accu << Google::APIClient::Method.new(self, self.method_base, k, v)
250 # Allows deep inspection of the discovery document.
252 return @discovery_document[key]
256 # Converts the service to a flat mapping of RPC names and method objects.
258 # @return [Hash] All methods available on the service.
261 # # Discover available methods
262 # method_names = client.discovered_api('buzz').to_h.keys
264 return @hash ||= (begin
266 self.discovered_methods.each do |method|
267 methods_hash[method.id] = method
269 self.discovered_resources.each do |resource|
270 methods_hash.merge!(resource.to_h)
277 # Returns a <code>String</code> representation of the service's state.
279 # @return [String] The service's state, as a <code>String</code>.
282 "#<%s:%#0x ID:%s>", self.class.to_s, self.object_id, self.id