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'
25 # A service that has been described by a discovery document.
29 # Creates a description of a particular version of a service.
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
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 }
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 }
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 ||= (
152 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.resources.each do |resource|
167 resource.method_base = @method_base
169 self.methods.each do |method|
170 method.method_base = @method_base
175 # A list of schemas available for this version of the API.
177 # @return [Hash] A list of {Google::APIClient::Schema} objects.
179 return @schemas ||= (
180 (@discovery_document['schemas'] || []).inject({}) do |accu, (k, v)|
181 accu[k] = Google::APIClient::Schema.parse(self, v)
188 # Returns a schema for a kind value.
190 # @return [Google::APIClient::Schema] The associated Schema object.
191 def schema_for_kind(kind)
192 api_name, schema_name = kind.split('#', 2)
193 if api_name != self.name
195 "The kind does not match this API. " +
196 "Expected '#{self.name}', got '#{api_name}'."
198 for k, v in self.schemas
199 return v if k.downcase == schema_name.downcase
205 # A list of resources available at the root level of this version of the
208 # @return [Array] A list of {Google::APIClient::Resource} objects.
210 return @resources ||= (
211 (@discovery_document['resources'] || []).inject([]) do |accu, (k, v)|
212 accu << Google::APIClient::Resource.new(
213 self, self.method_base, k, v
221 # A list of methods available at the root level of this version of the
224 # @return [Array] A list of {Google::APIClient::Method} objects.
226 return @methods ||= (
227 (@discovery_document['methods'] || []).inject([]) do |accu, (k, v)|
228 accu << Google::APIClient::Method.new(self, self.method_base, k, v)
235 # Allows deep inspection of the discovery document.
237 return @discovery_document[key]
241 # Converts the service to a flat mapping of RPC names and method objects.
243 # @return [Hash] All methods available on the service.
246 # # Discover available methods
247 # method_names = client.discovered_api('buzz').to_h.keys
249 return @hash ||= (begin
251 self.methods.each do |method|
252 methods_hash[method.id] = method
254 self.resources.each do |resource|
255 methods_hash.merge!(resource.to_h)
262 # Returns a <code>String</code> representation of the service's state.
264 # @return [String] The service's state, as a <code>String</code>.
267 "#<%s:%#0x ID:%s>", self.class.to_s, self.object_id, self.id