Class: Rapleaf::Marketo::Client

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

Overview

The client for talking to marketo

based on the SOAP wsdl file: http://app.marketo.com/soap/mktows/1_4?WSDL

Usage:

client = Rapleaf::Marketo.new_client(<access_key>, <secret_key>, api_subdomain = ‘na-i’, api_version = ‘1_5’, document_version = ‘1_4’)

get_lead_by_email:

lead_record = client.get_lead_by_email(‘[email protected]’)

puts lead_record.idnum

puts lead_record.get_attribute(‘FirstName’)

puts lead_record.get_attribute(‘LastName’)

sync_lead: (update)

lead_record = client.sync_lead(‘[email protected]’, ‘Joe’, ‘Smith’, ‘Company 1’, ‘415 911’)

sync_lead_record: (update with custom fields)

lead_record = Rapleaf::Marketo::LeadRecord.new(‘[email protected]’)

lead_record.set_attribute(‘FirstName’, ‘harry’)

lead_record.set_attribute(‘LastName’, ‘smith’)

lead_record.set_attribute(‘Email’, ‘[email protected]’)

lead_record.set_attribute(‘Company’, ‘Rapleaf’)

lead_record.set_attribute(‘MobilePhone’, ‘123 456’)

response = client.sync_lead_record(lead_record)

sync_lead_record_on_id: (update with custom fields, ensuring the sync is id based)

similarly, you can force a sync via id instead of email by calling client.sync_lead_record_on_id(lead_record)

Instance Method Summary collapse

Constructor Details

#initialize(savon_client, authentication_header) ⇒ Client

This constructor is used internally, create your client with *Rapleaf::Marketo.new_client(<access_key>, <secret_key>)*



60
61
62
63
# File 'lib/marketo/client.rb', line 60

def initialize(savon_client, authentication_header)
  @client = savon_client
  @header = authentication_header
end

Instance Method Details

#add_to_list(list_key, email) ⇒ Object



145
146
147
# File 'lib/marketo/client.rb', line 145

def add_to_list(list_key, email)
  list_operation(list_key, ListOperationType::ADD_TO, email)
end

#get_lead_by_email(email) ⇒ Object



72
73
74
# File 'lib/marketo/client.rb', line 72

def get_lead_by_email(email)
  get_lead(LeadKey.new(LeadKeyType::EMAIL, email))
end

#get_lead_by_idnum(idnum) ⇒ Object



67
68
69
# File 'lib/marketo/client.rb', line 67

def get_lead_by_idnum(idnum)
  get_lead(LeadKey.new(LeadKeyType::IDNUM, idnum))
end

#is_member_of_list?(list_key, email) ⇒ Boolean

Returns:

  • (Boolean)


153
154
155
# File 'lib/marketo/client.rb', line 153

def is_member_of_list?(list_key, email)
  list_operation(list_key, ListOperationType::IS_MEMBER_OF, email)
end

#remove_from_list(list_key, email) ⇒ Object



149
150
151
# File 'lib/marketo/client.rb', line 149

def remove_from_list(list_key, email)
  list_operation(list_key, ListOperationType::REMOVE_FROM, email)
end

#set_logger(logger) ⇒ Object



76
77
78
# File 'lib/marketo/client.rb', line 76

def set_logger(logger)
  @logger = logger
end

#sync_lead(email, first, last, company, mobile) ⇒ Object

create (if new) or update (if existing) a lead

  • email - email address of lead

  • first - first name of lead

  • last - surname/last name of lead

  • company - company the lead is associated with

  • mobile - mobile/cell phone number

returns the LeadRecord instance on success otherwise nil



89
90
91
92
93
94
95
96
97
# File 'lib/marketo/client.rb', line 89

def sync_lead(email, first, last, company, mobile)
  lead_record = LeadRecord.new(email)
  lead_record.set_attribute('FirstName', first)
  lead_record.set_attribute('LastName', last)
  lead_record.set_attribute('Email', email)
  lead_record.set_attribute('Company', company)
  lead_record.set_attribute('MobilePhone', mobile)
  sync_lead_record(lead_record)
end

#sync_lead_record(lead_record) ⇒ Object



99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/marketo/client.rb', line 99

def sync_lead_record(lead_record)
  begin
    attributes = []
    lead_record.each_attribute_pair do |name, value|
      attributes << {:attr_name => name, :attr_type => 'string', :attr_value => value}
    end

    response = send_request("ns1:paramsSyncLead", {
        :return_lead => true,
        :lead_record =>
            {:email               => lead_record.email,
             :lead_attribute_list => {
                 :attribute => attributes}}})
    return LeadRecord.from_hash(response[:success_sync_lead][:result][:lead_record])
  rescue Exception => e
    @logger.log(e) if @logger
    return nil
  end
end

#sync_lead_record_on_id(lead_record) ⇒ Object



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
# File 'lib/marketo/client.rb', line 119

def sync_lead_record_on_id(lead_record)
  idnum = lead_record.idnum
  raise 'lead record id not set' if idnum.nil?

  begin
    attributes = []
    lead_record.each_attribute_pair do |name, value|
        attributes << {:attr_name => name, :attr_type => 'string', :attr_value => value}
    end

    attributes << {:attr_name => 'Id', :attr_type => 'string', :attr_value => idnum.to_s}

    response = send_request("ns1:paramsSyncLead", {
        :return_lead => true,
        :lead_record =>
            {
              :lead_attribute_list => { :attribute => attributes},
              :id => idnum
            }})
    return LeadRecord.from_hash(response[:success_sync_lead][:result][:lead_record])
  rescue Exception => e
    @logger.log(e) if @logger
    return nil
  end
end