Module: Google::APIClient::Service::StubGenerator

Included in:
Google::APIClient::Service, Resource
Defined in:
lib/google/api_client/service/stub_generator.rb

Overview

Auxiliary mixin to generate resource and method stubs. Used by the Service and Service::Resource classes to generate both top-level and nested resources and methods.

Instance Method Summary collapse

Instance Method Details

#generate_call_stubs(service, root) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/google/api_client/service/stub_generator.rb', line 25

def generate_call_stubs(service, root)
  metaclass = (class << self; self; end)

  # Handle resources.
  root.discovered_resources.each do |resource|
    method_name = ActiveSupport::Inflector.underscore(resource.name).to_sym
    if !self.respond_to?(method_name)
      metaclass.send(:define_method, method_name) do
        Google::APIClient::Service::Resource.new(service, resource)
      end
    end
  end

  # Handle methods.
  root.discovered_methods.each do |method|
    method_name = ActiveSupport::Inflector.underscore(method.name).to_sym
    if !self.respond_to?(method_name)
      metaclass.send(:define_method, method_name) do |*args|
        if args.length > 1
          raise ArgumentError,
            "wrong number of arguments (#{args.length} for 1)"
        elsif !args.first.respond_to?(:to_hash) && !args.first.nil?
          raise ArgumentError,
            "expected parameter Hash, got #{args.first.class}"
        else
          return Google::APIClient::Service::Request.new(
            service, method, args.first
          )
        end
      end
    end
  end
end