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.
15 require 'active_support/inflector'
21 # Auxiliary mixin to generate resource and method stubs.
22 # Used by the Service and Service::Resource classes to generate both
23 # top-level and nested resources and methods.
25 def generate_call_stubs(service, root)
26 metaclass = (class << self; self; end)
29 root.discovered_resources.each do |resource|
30 method_name = ActiveSupport::Inflector.underscore(resource.name).to_sym
31 if !self.respond_to?(method_name)
32 metaclass.send(:define_method, method_name) do
33 Google::APIClient::Service::Resource.new(service, resource)
39 root.discovered_methods.each do |method|
40 method_name = ActiveSupport::Inflector.underscore(method.name).to_sym
41 if !self.respond_to?(method_name)
42 metaclass.send(:define_method, method_name) do |*args|
45 "wrong number of arguments (#{args.length} for 1)"
46 elsif !args.first.respond_to?(:to_hash) && !args.first.nil?
48 "expected parameter Hash, got #{args.first.class}"
50 return Google::APIClient::Service::Request.new(
51 service, method, args.first