Module: AdWords::Generator

Defined in:
lib/adwords4r/generator.rb

Overview

Contains the methods that handle wrapper code generation.

Constant Summary collapse

ARRAY_CLASSNAME =
'SOAP::SOAPArray'

Class Method Summary collapse

Class Method Details

.generate_wrapper_class(version, service) ⇒ Object

Generate the wrapper class for a given service. These classes make it easier to invoke the API methods, by removing the need to instance a <MethodName> object, instead allowing passing of the call parameters directly. Here is an example of a generated wrapper class, with one API method and one extension:

class SampleServiceWrapper

  attr_reader :api

  def initialize(driver, api)
    @driver = driver
    @api = api
  end

  def getSomething(par1, par2)
    begin
      AdWords::Service.validate_param('par1', par1, SOAP::SOAPArray)
      AdWords::Service.validate_param('par2', par2, SOAP::SOAPInt)
      # Construct request object and make API call
      obj = AdWords::V13::GetSomething.new(par1, par2)
      return @driver.getSomething(obj)
    rescue SOAP::FaultError => fault
      raise(AdWords::Error::create_specific_api_error(fault),
          "getSomething Call Failed: " + fault.faultstring.to_s, caller)
    end
  end

  def doSomethingElseExtension(par1, par2)
    return AdWords::Extensions.doSomethingElseExtension(self, par1, par2)
  end
end

Args:

  • version: the API version (as an integer)

  • service: the service name (as a string)

Returns: The Ruby code for the class, as a string.



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
# File 'lib/adwords4r/generator.rb', line 71

def self.generate_wrapper_class(version, service)
  wrapper = service + "ServiceWrapper"
  module_name = AdWords::Service.get_module_name(version, service)
  driver = AdWords::Service.get_interface_name(version, service)
  driver_class = eval(driver)

  registry = eval("#{module_name}::DefaultMappingRegistry::LiteralRegistry")

  class_def = <<-EOS
# This file was automatically generated during the "rake generate" step of
# library setup.
require 'adwords4r/v#{version}/#{service}ServiceDriver.rb'

module AdWords
  module V#{version}
module #{service}Service

  # Wrapper class for the v#{version.to_s} #{service} service.
  # This class is automatically generated.
  class #{wrapper}

    # Holds the AdWords::API object to which the wrapper belongs.
    attr_reader :api

    # Holds a shortcut to the parent module.
    # Use this to avoid typing the full class name when creating classes
    # belonging to this service, e.g.
    #  service_object.module::ClassName
    # instead of
    #  AdWords::V#{version.to_s}::#{service}Service::ClassName
    # This will make it easier to migrate your code between API versions.
    attr_reader :module

    public

    # Constructor for #{wrapper}.
    #
    # Args:
    # - driver: SOAP::RPC::Driver object with the remote SOAP methods for
    #   this service
    # - api: the AdWords::API object to which the wrapper belongs
    #
    def initialize(driver, api)
      @driver = driver
      @api = api
      @module = AdWords::V#{version.to_s}::#{service}Service
    end

  EOS

  # Add service methods
  methods = driver_class::Methods
  module_name = AdWords::Service.get_module_name(version, service)
  methods.each do |method|
    name = method[1]
    doc_link = get_doc_link(version, service, name)
    method_def = <<-EOS
    # Calls the {#{name}}[#{doc_link}] method of the #{service} service.
    # Check {the online documentation for this method}[#{doc_link}].
    EOS

    method_class = eval("#{module_name}::#{fix_case_up(name)}")
    arguments = registry.schema_definition_from_class(method_class).elements

    if arguments.size > 0
      method_def += <<-EOS
    #
    # Args:
      EOS
    end

    # Add list of arguments to the RDoc comment
    arguments.each_with_index do |elem, index|
      if get_type(elem) == ARRAY_CLASSNAME
        method_def += <<-EOS
    # - #{elem.varname}: #{get_type(elem)} of #{elem.mapped_class}
        EOS
      else
        method_def += <<-EOS
    # - #{elem.varname}: #{get_type(elem)}
        EOS
      end
    end

    response_class = eval("#{module_name}::#{fix_case_up(name)}Response")
    returns = registry.schema_definition_from_class(response_class).elements

    if returns.size > 0
      method_def += <<-EOS
    #
    # Returns:
      EOS
    end

    # Add list of returns to the RDoc comment
    returns.each_with_index do |elem, index|
      if get_type(elem) == ARRAY_CLASSNAME
        method_def += <<-EOS
    # - #{elem.varname}: #{get_type(elem)} of #{elem.mapped_class}
        EOS
      else
        method_def += <<-EOS
    # - #{elem.varname}: #{get_type(elem)}
        EOS
      end
    end

    arg_names = []
    arguments.each do |elem|
      arg_names << elem.varname
    end
    arg_list = arg_names.join(', ')

    method_def += <<-EOS
    #
    # Raises:
    # Error::ApiError (or a subclass thereof) if a SOAP fault occurs.
    #
    def #{name}(#{arg_list})
      begin
    EOS

    # Add validation for every argument
    arguments.each_with_index do |elem, index|
      method_def += <<-EOS
        AdWords::Service.validate_param('#{elem.varname}',
            #{arg_names[index]}, #{get_type(elem)})
      EOS
    end

    method_def += <<-EOS
        # Construct request object and make API call
        obj = #{module_name}::#{fix_case_up(name)}.new(#{arg_list})
        return @driver.#{name}(obj)
      rescue SOAP::FaultError => fault
        raise(AdWords::Error::create_specific_api_error(fault),
            "#{name} Call Failed: " + fault.faultstring.to_s, caller)
      end
    end

    EOS
    class_def += method_def
  end

  # Add extension methods, if any
  extensions = AdWords::Extensions::extensions[[version, service]]
  unless extensions.nil?
    extensions.each do |ext|
      params = AdWords::Extensions::methods[ext].join(', ')
      arglist = 'self'
      arglist += ", #{params}" if params != ''
      method_def = <<-EOS
    # <i>Extension method</i> -- Calls the AdWords::Extensions.#{ext} method
    # with +self+ as the first parameter.
    def #{ext}(#{params})
      return AdWords::Extensions.#{ext}(#{arglist})
    end

      EOS
      class_def += method_def
    end
  end

  class_def += <<-EOS
  end
end
  end
end
  EOS
  return class_def
end