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