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'
24 # A resource that has been described by a discovery document.
28 # Creates a description of a particular version of a resource.
30 # @param [Addressable::URI] base
31 # The base URI for the service.
32 # @param [String] resource_name
33 # The identifier for the resource.
34 # @param [Hash] resource_description
35 # The section of the discovery document that applies to this resource.
37 # @return [Google::APIClient::Resource] The constructed resource object.
38 def initialize(api, method_base, resource_name, discovery_document)
40 @method_base = method_base
42 @discovery_document = discovery_document
43 metaclass = (class <<self; self; end)
44 self.resources.each do |resource|
45 method_name = Google::INFLECTOR.underscore(resource.name).to_sym
46 if !self.respond_to?(method_name)
47 metaclass.send(:define_method, method_name) { resource }
50 self.methods.each do |method|
51 method_name = Google::INFLECTOR.underscore(method.name).to_sym
52 if !self.respond_to?(method_name)
53 metaclass.send(:define_method, method_name) { method }
59 # Returns the identifier for the resource.
61 # @return [String] The resource identifier.
65 # Returns the parsed section of the discovery document that applies to
68 # @return [Hash] The resource description.
69 attr_reader :description
72 # Returns the base URI for this resource.
74 # @return [Addressable::URI] The base URI that methods are joined to.
75 attr_reader :method_base
78 # Updates the hierarchy of resources and methods with the new base.
80 # @param [Addressable::URI, #to_str, String] new_base
81 # The new base URI to use for the resource.
82 def method_base=(new_method_base)
83 @method_base = Addressable::URI.parse(new_method_base)
84 self.resources.each do |resource|
85 resource.method_base = @method_base
87 self.methods.each do |method|
88 method.method_base = @method_base
93 # A list of sub-resources available on this resource.
95 # @return [Array] A list of {Google::APIClient::Resource} objects.
97 return @resources ||= (
98 (@discovery_document['resources'] || []).inject([]) do |accu, (k, v)|
99 accu << Google::APIClient::Resource.new(
100 @api, self.method_base, k, v
108 # A list of methods available on this resource.
110 # @return [Array] A list of {Google::APIClient::Method} objects.
112 return @methods ||= (
113 (@discovery_document['methods'] || []).inject([]) do |accu, (k, v)|
114 accu << Google::APIClient::Method.new(@api, self.method_base, k, v)
121 # Converts the resource to a flat mapping of RPC names and method
124 # @return [Hash] All methods available on the resource.
126 return @hash ||= (begin
128 self.methods.each do |method|
129 methods_hash[method.id] = method
131 self.resources.each do |resource|
132 methods_hash.merge!(resource.to_h)
139 # Returns a <code>String</code> representation of the resource's state.
141 # @return [String] The resource's state, as a <code>String</code>.
144 "#<%s:%#0x NAME:%s>", self.class.to_s, self.object_id, self.name