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/method'
25 # A resource that has been described by a discovery document.
29 # Creates a description of a particular version of a resource.
31 # @param [Addressable::URI] base
32 # The base URI for the service.
33 # @param [String] resource_name
34 # The identifier for the resource.
35 # @param [Hash] resource_description
36 # The section of the discovery document that applies to this resource.
38 # @return [Google::APIClient::Resource] The constructed resource object.
39 def initialize(api, method_base, resource_name, discovery_document)
41 @method_base = method_base
43 @discovery_document = discovery_document
44 metaclass = (class <<self; self; end)
45 self.discovered_resources.each do |resource|
46 method_name = Google::INFLECTOR.underscore(resource.name).to_sym
47 if !self.respond_to?(method_name)
48 metaclass.send(:define_method, method_name) { resource }
51 self.discovered_methods.each do |method|
52 method_name = Google::INFLECTOR.underscore(method.name).to_sym
53 if !self.respond_to?(method_name)
54 metaclass.send(:define_method, method_name) { method }
60 # Returns the identifier for the resource.
62 # @return [String] The resource identifier.
66 # Returns the parsed section of the discovery document that applies to
69 # @return [Hash] The resource description.
70 attr_reader :description
73 # Returns the base URI for this resource.
75 # @return [Addressable::URI] The base URI that methods are joined to.
76 attr_reader :method_base
79 # Updates the hierarchy of resources and methods with the new base.
81 # @param [Addressable::URI, #to_str, String] new_base
82 # The new base URI to use for the resource.
83 def method_base=(new_method_base)
84 @method_base = Addressable::URI.parse(new_method_base)
85 self.discovered_resources.each do |resource|
86 resource.method_base = @method_base
88 self.discovered_methods.each do |method|
89 method.method_base = @method_base
94 # A list of sub-resources available on this resource.
96 # @return [Array] A list of {Google::APIClient::Resource} objects.
97 def discovered_resources
98 return @discovered_resources ||= (
99 (@discovery_document['resources'] || []).inject([]) do |accu, (k, v)|
100 accu << Google::APIClient::Resource.new(
101 @api, self.method_base, k, v
109 # A list of methods available on this resource.
111 # @return [Array] A list of {Google::APIClient::Method} objects.
112 def discovered_methods
113 return @discovered_methods ||= (
114 (@discovery_document['methods'] || []).inject([]) do |accu, (k, v)|
115 accu << Google::APIClient::Method.new(@api, self.method_base, k, v)
122 # Converts the resource to a flat mapping of RPC names and method
125 # @return [Hash] All methods available on the resource.
127 return @hash ||= (begin
129 self.discovered_methods.each do |method|
130 methods_hash[method.id] = method
132 self.discovered_resources.each do |resource|
133 methods_hash.merge!(resource.to_h)
140 # Returns a <code>String</code> representation of the resource's state.
142 # @return [String] The resource's state, as a <code>String</code>.
145 "#<%s:%#0x NAME:%s>", self.class.to_s, self.object_id, self.name