Better way to handle automatic parsing using the schemas in the discovery document.
[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 module Google
22   class APIClient
23     ##
24     # A resource that has been described by a discovery document.
25     class Resource
26
27       ##
28       # Creates a description of a particular version of a resource.
29       #
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.
36       #
37       # @return [Google::APIClient::Resource] The constructed resource object.
38       def initialize(api, method_base, resource_name, discovery_document)
39         @api = api
40         @method_base = method_base
41         @name = resource_name
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 }
48           end
49         end
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 }
54           end
55         end
56       end
57
58       ##
59       # Returns the identifier for the resource.
60       #
61       # @return [String] The resource identifier.
62       attr_reader :name
63
64       ##
65       # Returns the parsed section of the discovery document that applies to
66       # this resource.
67       #
68       # @return [Hash] The resource description.
69       attr_reader :description
70
71       ##
72       # Returns the base URI for this resource.
73       #
74       # @return [Addressable::URI] The base URI that methods are joined to.
75       attr_reader :method_base
76
77       ##
78       # Updates the hierarchy of resources and methods with the new base.
79       #
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
86         end
87         self.methods.each do |method|
88           method.method_base = @method_base
89         end
90       end
91
92       ##
93       # A list of sub-resources available on this resource.
94       #
95       # @return [Array] A list of {Google::APIClient::Resource} objects.
96       def resources
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
101             )
102             accu
103           end
104         )
105       end
106
107       ##
108       # A list of methods available on this resource.
109       #
110       # @return [Array] A list of {Google::APIClient::Method} objects.
111       def methods
112         return @methods ||= (
113           (@discovery_document['methods'] || []).inject([]) do |accu, (k, v)|
114             accu << Google::APIClient::Method.new(@api, self.method_base, k, v)
115             accu
116           end
117         )
118       end
119
120       ##
121       # Converts the resource to a flat mapping of RPC names and method
122       # objects.
123       #
124       # @return [Hash] All methods available on the resource.
125       def to_h
126         return @hash ||= (begin
127           methods_hash = {}
128           self.methods.each do |method|
129             methods_hash[method.id] = method
130           end
131           self.resources.each do |resource|
132             methods_hash.merge!(resource.to_h)
133           end
134           methods_hash
135         end)
136       end
137
138       ##
139       # Returns a <code>String</code> representation of the resource's state.
140       #
141       # @return [String] The resource's state, as a <code>String</code>.
142       def inspect
143         sprintf(
144           "#<%s:%#0x NAME:%s>", self.class.to_s, self.object_id, self.name
145         )
146       end
147     end
148   end
149 end