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 [Google::APIClient::API] api
32 # The API this resource belongs to.
33 # @param [Addressable::URI] method_base
34 # The base URI for the service.
35 # @param [String] resource_name
36 # The identifier for the resource.
37 # @param [Hash] discovery_document
38 # The section of the discovery document that applies to this resource.
40 # @return [Google::APIClient::Resource] The constructed resource object.
41 def initialize(api, method_base, resource_name, discovery_document)
43 @method_base = method_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 identifier for the resource.
64 # @return [String] The resource identifier.
68 # Returns the parsed section of the discovery document that applies to
71 # @return [Hash] The resource description.
72 attr_reader :description
75 # Returns the base URI for this resource.
77 # @return [Addressable::URI] The base URI that methods are joined to.
78 attr_reader :method_base
81 # Updates the hierarchy of resources and methods with the new base.
83 # @param [Addressable::URI, #to_str, String] new_method_base
84 # The new base URI to use for the resource.
85 def method_base=(new_method_base)
86 @method_base = Addressable::URI.parse(new_method_base)
87 self.discovered_resources.each do |resource|
88 resource.method_base = @method_base
90 self.discovered_methods.each do |method|
91 method.method_base = @method_base
96 # A list of sub-resources available on this resource.
98 # @return [Array] A list of {Google::APIClient::Resource} objects.
99 def discovered_resources
100 return @discovered_resources ||= (
101 (@discovery_document['resources'] || []).inject([]) do |accu, (k, v)|
102 accu << Google::APIClient::Resource.new(
103 @api, self.method_base, k, v
111 # A list of methods available on this resource.
113 # @return [Array] A list of {Google::APIClient::Method} objects.
114 def discovered_methods
115 return @discovered_methods ||= (
116 (@discovery_document['methods'] || []).inject([]) do |accu, (k, v)|
117 accu << Google::APIClient::Method.new(@api, self.method_base, k, v)
124 # Converts the resource to a flat mapping of RPC names and method
127 # @return [Hash] All methods available on the resource.
129 return @hash ||= (begin
131 self.discovered_methods.each do |method|
132 methods_hash[method.id] = method
134 self.discovered_resources.each do |resource|
135 methods_hash.merge!(resource.to_h)
142 # Returns a <code>String</code> representation of the resource's state.
144 # @return [String] The resource's state, as a <code>String</code>.
147 "#<%s:%#0x NAME:%s>", self.class.to_s, self.object_id, self.name