Class: Vertigo::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/vertigo/client.rb

Overview

Simple wrapper around the VerticalResponse SOAP API.

API Versioning

As of 2011.08.15, VerticalResponse changes and remove API calls, but still call it API “v1.0” each time. These mandatory API changes could break your code (although we have never experienced that personally).

Inquiring about their versioning policy got this response: “For a variety of internal reasons, this is still considered the 1.0 release of our API. We’re currently exploring adding real versioning to our API to avoid these kinds of issues going forward.”

Because of this, Vertigo::Client regenerates the RPC driver when instantiated as the generated code for VRAPI.wsdl becomes stale over time. Generating the driver incurs a startup cost, but it’s minimal.

Author:

Constant Summary collapse

DURATION_MINUTES =
4
WSDL_URL =
'https://api.verticalresponse.com/partner-wsdl/1.0/VRAPI.wsdl'

Instance Method Summary collapse

Constructor Details

#initialize(username, password, opts = {}) ⇒ Client

Returns a new instance of Client.

Examples:

Connect using session defaults

client = Vertigo::Client.new('[email protected]', 'your password')

Connect using the specified session options

client = Vertigo::Client.new('[email protected]', 'your password', :duration_minutes => 5)

Parameters:

  • username (String)

    Account username

  • password (String)

    Account password

  • opts (Hash) (defaults to: {})

    Session options

Options Hash (opts):

  • :duration_minutes (Fixnum)

    Duration of session in minutes

  • :wsdl_url (String)

    Alternative WSDL URL

Raises:

  • (SOAP::FaultError)

    when an API call fails



31
32
33
34
35
36
37
38
39
40
41
# File 'lib/vertigo/client.rb', line 31

def initialize(username, password, opts = {})
  session_duration_minutes = opts[:duration_minutes] || DURATION_MINUTES
  wsdl_url = opts[:wsdl_url] || WSDL_URL

  @api = SOAP::WSDLDriverFactory.new(wsdl_url).create_rpc_driver
  @session_id = @api.(
    'username' => username.to_s,
    'password' => password.to_s,
    'session_duration_minutes' => session_duration_minutes
  )
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args, &block) ⇒ Object

Note:

camelCase method names are still supported.

This modification lets us write API calls in a more “Rubyish” manner.

Examples:

Unlaunching an email campaign

client.unlaunch_email_campaign(:campaign_id => 1)
# Without Vertigo::Client, it would be:
vrapi.unlaunchEmailCampaign('session_id' => session_id, 'campaign_id' => 1)

Raises:

  • (ArgumentError)

    when too many arguments or unsupported arguments

  • (NoMethodError)

    when the API call is not defined



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/vertigo/client.rb', line 54

def method_missing(name, *args, &block)
  options = args[0]
  vr_name = camelize_meth(name).intern

  if args.length > 1
    raise ArgumentError, 'Unexpected number of arguments (not 0 or 1)'
  elsif options && !options.respond_to?(:keys)
    raise ArgumentError, "options does not respond to :keys (options.class: #{options.class}, options: #{options.inspect})"
  end

  if @api.respond_to?(vr_name)
    vr_args = options ? stringify_keys(options) : {}
    @api.send(vr_name, {'session_id' => @session_id}.merge(vr_args))
  else
    raise NoMethodError, "Undefined VerticalResponse API call: #{vr_name.inspect}"
  end
end