1 # Copyright 2013 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.
19 # Auxiliary mixin to generate resource and method stubs.
20 # Used by the Service and Service::Resource classes to generate both
21 # top-level and nested resources and methods.
23 def generate_call_stubs(service, root)
24 metaclass = (class << self; self; end)
27 root.discovered_resources.each do |resource|
28 method_name = Google::INFLECTOR.underscore(resource.name).to_sym
29 if !self.respond_to?(method_name)
30 metaclass.send(:define_method, method_name) do
31 Google::APIClient::Service::Resource.new(service, resource)
37 root.discovered_methods.each do |method|
38 method_name = Google::INFLECTOR.underscore(method.name).to_sym
39 if !self.respond_to?(method_name)
40 metaclass.send(:define_method, method_name) do |*args|
43 "wrong number of arguments (#{args.length} for 1)"
44 elsif !args.first.respond_to?(:to_hash) && !args.first.nil?
46 "expected parameter Hash, got #{args.first.class}"
48 return Google::APIClient::Service::Request.new(
49 service, method, args.first