class MarketoAPI::ClientProxy

The ClientProxy is the base class for implementing Marketo APIs in a fluent manner. When a descendant class is implemented, a method will be added to MarketoAPI::Client based on the descendant class name that will provide an initialized instance of the descendant class using the MarketoAPI::Client instance.

This base class does not provide any useful functionality for consumers of MarketoAPI, and is primarily provided for common functionality.

As an example, MarketoAPI::Client#campaigns was generated when MarketoAPI::Campaigns was inherited from MarketoAPI::ClientProxy. It returns an instance of MarketoAPI::Campaigns intialized with the MarketoAPI::Client that generated it.

Attributes

error[R]

Reads the error attribute from the proxied client.

Public Class Methods

inherited(klass) click to toggle source

Generates a new method on MarketoAPI::Client based on the inherited class name.

# File lib/marketo_api/client_proxy.rb, line 22
    def inherited(klass)
      name = klass.name.split(/::/).last.downcase.to_sym

      MarketoAPI::Client.class_eval <<-EOS
        def #{name}
          @#{name} ||= #{klass}.new(self)
        end
      EOS
    end
new(client) click to toggle source
# File lib/marketo_api/client_proxy.rb, line 33
def initialize(client)
  @client = client
end

Public Instance Methods

error?() click to toggle source

Reads the presence of an error from the proxied client.

# File lib/marketo_api/client_proxy.rb, line 47
def_delegators :@client, :error, :error?

Private Instance Methods

call(method, param, &block) click to toggle source

Performs a SOAP call and extracts the result from a successful result.

The Marketo SOAP API always returns results in a fairly deep structure. For example, the SOAP response for requestCampaign looks something like:

&lt;successRequestCampaign&gt;
  &lt;result&gt;
  &lt;/result&gt;
&lt;/successRequestCampaign&gt;
# File lib/marketo_api/client_proxy.rb, line 59
def call(method, param, &block)
  extract_from_response(
    @client.call(method, param),
    :"success_#{method}",
    :result,
    &block
  )
end
extract_from_response(response, *paths) { |response| ... } click to toggle source

Given a response hash (which is deeply nested), follows the key path down.

# File lib/marketo_api/client_proxy.rb, line 99
def extract_from_response(response, *paths)
  paths.each { |path| response &&= response[path] }
  response = yield response if response and block_given?
  response
end
transform_param(method, param, override = nil) click to toggle source

Takes a provided a parameter and transforms it. If the parameter is a Hash or nil, there is no transformation performed; if it responds to a method params_for_#{method}, that method is called to transform the object.

If neither of these is true, an ArgumentError is raised.

# File lib/marketo_api/client_proxy.rb, line 82
def transform_param(method, param, override = nil)
  method = if override
             override
           else
             :"params_for_#{method}"
           end
  if param.kind_of? Hash or param.nil?
    param
  elsif param.respond_to? method
    param.send(method)
  else
    raise ArgumentError, "Invalid parameter: #{param.inspect}"
  end
end
transform_param_list(method, param_list) click to toggle source

Takes a parameter list and calls transform_param on each parameter.

# File lib/marketo_api/client_proxy.rb, line 69
def transform_param_list(method, param_list)
  method = :"params_for_#{method}"
  param_list.map { |param|
    transform_param(nil, param, method)
  }.compact
end