Fixed some YARD documentation issues and changed how the user-agent is built.
[arvados.git] / lib / google / api_client / discovery / resource.rb
1 # Copyright 2010 Google Inc.
2 #
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
6 #
7 #      http://www.apache.org/licenses/LICENSE-2.0
8 #
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.
14
15
16 require 'addressable/uri'
17
18 require 'google/inflection'
19 require 'google/api_client/discovery/method'
20
21
22 module Google
23   class APIClient
24     ##
25     # A resource that has been described by a discovery document.
26     class Resource
27
28       ##
29       # Creates a description of a particular version of a resource.
30       #
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.
37       #
38       # @return [Google::APIClient::Resource] The constructed resource object.
39       def initialize(api, method_base, resource_name, discovery_document)
40         @api = api
41         @method_base = method_base
42         @name = resource_name
43         @discovery_document = discovery_document
44         metaclass = (class <<self; self; end)
45         self.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 }
49           end
50         end
51         self.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 }
55           end
56         end
57       end
58
59       ##
60       # Returns the identifier for the resource.
61       #
62       # @return [String] The resource identifier.
63       attr_reader :name
64
65       ##
66       # Returns the parsed section of the discovery document that applies to
67       # this resource.
68       #
69       # @return [Hash] The resource description.
70       attr_reader :description
71
72       ##
73       # Returns the base URI for this resource.
74       #
75       # @return [Addressable::URI] The base URI that methods are joined to.
76       attr_reader :method_base
77
78       ##
79       # Updates the hierarchy of resources and methods with the new base.
80       #
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.resources.each do |resource|
86           resource.method_base = @method_base
87         end
88         self.methods.each do |method|
89           method.method_base = @method_base
90         end
91       end
92
93       ##
94       # A list of sub-resources available on this resource.
95       #
96       # @return [Array] A list of {Google::APIClient::Resource} objects.
97       def resources
98         return @resources ||= (
99           (@discovery_document['resources'] || []).inject([]) do |accu, (k, v)|
100             accu << Google::APIClient::Resource.new(
101               @api, self.method_base, k, v
102             )
103             accu
104           end
105         )
106       end
107
108       ##
109       # A list of methods available on this resource.
110       #
111       # @return [Array] A list of {Google::APIClient::Method} objects.
112       def methods
113         return @methods ||= (
114           (@discovery_document['methods'] || []).inject([]) do |accu, (k, v)|
115             accu << Google::APIClient::Method.new(@api, self.method_base, k, v)
116             accu
117           end
118         )
119       end
120
121       ##
122       # Converts the resource to a flat mapping of RPC names and method
123       # objects.
124       #
125       # @return [Hash] All methods available on the resource.
126       def to_h
127         return @hash ||= (begin
128           methods_hash = {}
129           self.methods.each do |method|
130             methods_hash[method.id] = method
131           end
132           self.resources.each do |resource|
133             methods_hash.merge!(resource.to_h)
134           end
135           methods_hash
136         end)
137       end
138
139       ##
140       # Returns a <code>String</code> representation of the resource's state.
141       #
142       # @return [String] The resource's state, as a <code>String</code>.
143       def inspect
144         sprintf(
145           "#<%s:%#0x NAME:%s>", self.class.to_s, self.object_id, self.name
146         )
147       end
148     end
149   end
150 end