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(method_base, resource_name, discovery_document)
39 @method_base = method_base
41 @discovery_document = discovery_document
42 metaclass = (class <<self; self; end)
43 self.resources.each do |resource|
44 method_name = Google::INFLECTOR.underscore(resource.name).to_sym
45 if !self.respond_to?(method_name)
46 metaclass.send(:define_method, method_name) { resource }
49 self.methods.each do |method|
50 method_name = Google::INFLECTOR.underscore(method.name).to_sym
51 if !self.respond_to?(method_name)
52 metaclass.send(:define_method, method_name) { method }
58 # Returns the identifier for the resource.
60 # @return [String] The resource identifier.
64 # Returns the parsed section of the discovery document that applies to
67 # @return [Hash] The resource description.
68 attr_reader :description
71 # Returns the base URI for this resource.
73 # @return [Addressable::URI] The base URI that methods are joined to.
74 attr_reader :method_base
77 # Updates the hierarchy of resources and methods with the new base.
79 # @param [Addressable::URI, #to_str, String] new_base
80 # The new base URI to use for the resource.
81 def method_base=(new_method_base)
82 @method_base = Addressable::URI.parse(new_method_base)
83 self.resources.each do |resource|
84 resource.method_base = @method_base
86 self.methods.each do |method|
87 method.method_base = @method_base
92 # A list of sub-resources available on this resource.
94 # @return [Array] A list of {Google::APIClient::Resource} objects.
96 return @resources ||= (
97 (@discovery_document['resources'] || []).inject([]) do |accu, (k, v)|
98 accu << Google::APIClient::Resource.new(self.method_base, k, v)
105 # A list of methods available on this resource.
107 # @return [Array] A list of {Google::APIClient::Method} objects.
109 return @methods ||= (
110 (@discovery_document['methods'] || []).inject([]) do |accu, (k, v)|
111 accu << Google::APIClient::Method.new(self.method_base, k, v)
118 # Converts the resource to a flat mapping of RPC names and method
121 # @return [Hash] All methods available on the resource.
123 return @hash ||= (begin
125 self.methods.each do |method|
126 methods_hash[method.id] = method
128 self.resources.each do |resource|
129 methods_hash.merge!(resource.to_h)
136 # Returns a <code>String</code> representation of the resource's state.
138 # @return [String] The resource's state, as a <code>String</code>.
141 "#<%s:%#0x NAME:%s>", self.class.to_s, self.object_id, self.name