Class: MarketoAPI::Lead

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Includes:
Enumerable
Defined in:
lib/marketo_api/lead.rb

Overview

An object representing a Marketo Lead record.

Constant Summary collapse

NAMED_KEYS =

:nodoc:

{ #:nodoc:
  id:                         :IDNUM,
  cookie:                     :COOKIE,
  email:                      :EMAIL,
  lead_owner_email:           :LEADOWNEREMAIL,
  salesforce_account_id:      :SFDCACCOUNTID,
  salesforce_contact_id:      :SFDCCONTACTID,
  salesforce_lead_id:         :SFDCLEADID,
  salesforce_lead_owner_id:   :SFDCLEADOWNERID,
  salesforce_opportunity_id:  :SFDCOPPTYID
}.freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) {|_self| ... } ⇒ Lead

:method: values :call-seq:

values() -> array

Returns the attribute values.

Yields:

  • (_self)

Yield Parameters:



87
88
89
90
91
92
93
94
95
# File 'lib/marketo_api/lead.rb', line 87

def initialize(options = {})
  @id          = options[:id]
  @attributes  = {}
  @types       = {}
  @foreign     = {}
  self[:Email] = options[:email]
  self.proxy   = options[:proxy]
  yield self if block_given?
end

Instance Attribute Details

#attributesObject (readonly)

The attributes for the Lead.



29
30
31
# File 'lib/marketo_api/lead.rb', line 29

def attributes
  @attributes
end

The Marketo tracking cookie. Optional.



26
27
28
# File 'lib/marketo_api/lead.rb', line 26

def cookie
  @cookie
end

#idObject (readonly)

The Marketo ID. This value cannot be set by consumers.



24
25
26
# File 'lib/marketo_api/lead.rb', line 24

def id
  @id
end

#proxyObject

The proxy object for this class.



33
34
35
# File 'lib/marketo_api/lead.rb', line 33

def proxy
  @proxy
end

#typesObject (readonly)

The types for the Lead attributes.



31
32
33
# File 'lib/marketo_api/lead.rb', line 31

def types
  @types
end

Class Method Details

.from_soap_hash(hash) {|lead| ... } ⇒ Object

Creates a new Lead from a SOAP response hash (from Leads#get, Leads#get_multiple, Leads#sync, or Leads#sync_multiple).

Yields:

  • (lead)


218
219
220
221
222
223
224
225
226
227
228
229
230
231
# File 'lib/marketo_api/lead.rb', line 218

def from_soap_hash(hash) #:nodoc:
  lead = new(id: hash[:id].to_i, email: hash[:email]) do |lr|
    if type = hash[:foreign_sys_type]
      lr.foreign(type, hash[:foreign_sys_person_id])
    end
    hash[:lead_attribute_list][:attribute].each do |attribute|
      name = attribute[:attr_name].to_sym
      lr.attributes[name] = attribute[:attr_value]
      lr.types[name] = attribute[:attr_type]
    end
  end
  yield lead if block_given?
  lead
end

.key(key, value) ⇒ Object

Creates a new Lead key hash suitable for use in a number of Marketo API calls.



235
236
237
238
239
240
241
242
# File 'lib/marketo_api/lead.rb', line 235

def key(key, value)
  {
    leadKey: {
      keyType:  key_type(key),
      keyValue: value
    }
  }
end

Instance Method Details

#==(other) ⇒ Object



257
258
259
260
# File 'lib/marketo_api/lead.rb', line 257

def ==(other)
  id == other.id && cookie == other.cookie && foreign == other.foreign &&
    attributes == other.attributes && types == other.types
end

#[]=(key, value) ⇒ Object

:method: []=(hash, value) :call-seq:

lead[key] = value -> value

Looks up the provided attribute.



103
104
105
106
# File 'lib/marketo_api/lead.rb', line 103

def []=(key, value)
  @attributes[key] = value
  @types[key] ||= infer_value_type(value)
end

#emailObject

:attr_reader: email



141
142
143
# File 'lib/marketo_api/lead.rb', line 141

def email
  self[:Email]
end

#email=(value) ⇒ Object

:attr_writer: email



146
147
148
# File 'lib/marketo_api/lead.rb', line 146

def email=(value)
  self[:Email] = value
end

#foreign(type = nil, id = nil) ⇒ Object

:call-seq:

lead.foreign -> nil
lead.foreign(type, id) -> { type: type, id: id }
lead.foreign -> { type: type, id: id }

Sets or returns the foreign system type and person ID.



135
136
137
138
# File 'lib/marketo_api/lead.rb', line 135

def foreign(type = nil, id = nil)
  @foreign = { type: type.to_sym, id: id } if type and id
  @foreign
end

#inspectObject



262
263
264
# File 'lib/marketo_api/lead.rb', line 262

def inspect
  "#<#{self.class} id=#{id} cookie=#{cookie} foreign=#{foreign.inspect} attributes=#{attributes.inspect} types=#{types.inspect}>"
end

#params_for_getObject

Returns a lead key structure suitable for use with MarketoAPI::Leads#get.



188
189
190
# File 'lib/marketo_api/lead.rb', line 188

def params_for_get
  self.class.key(:IDNUM, id)
end

#params_for_syncObject

Returns the parameters required for use with MarketoAPI::Leads#sync.



193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
# File 'lib/marketo_api/lead.rb', line 193

def params_for_sync
  {
    returnLead:    true,
    marketoCookie: cookie,
    leadRecord:    {
      Email:              email,
      Id:                 id,
      ForeignSysPersonId: foreign[:id],
      ForeignSysType:     foreign[:type],
      leadAttributeList:  {
        attribute: attributes.map { |key, value|
          {
            attrName:  key.to_s,
            attrType:  types[key],
            attrValue: value
          }
        }
      }
    }.delete_if(&MarketoAPI::MINIMIZE_HASH)
  }.delete_if(&MarketoAPI::MINIMIZE_HASH)
end

#syncObject

Performs a Lead sync and returns the new Lead object, or nil if the sync failed.

Raises an ArgumentError if a proxy has not been configured with Lead#proxy=.

Raises:

  • (ArgumentError)


155
156
157
158
# File 'lib/marketo_api/lead.rb', line 155

def sync
  raise ArgumentError, "No proxy configured" unless proxy
  proxy.sync(self)
end

#sync!Object

Performs a Lead sync and updates this Lead object in-place, or nil if the sync failed.

Raises an ArgumentError if a proxy has not been configured with Lead#proxy=.



165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
# File 'lib/marketo_api/lead.rb', line 165

def sync!
  if lead = sync
    @id      = lead.id
    @cookie  = lead.cookie
    @foreign = lead.foreign
    @proxy   = lead.proxy
    removed  = self.keys - lead.keys

    lead.each_pair { |k, v|
      @attributes[k] = v
      @types[k]      = lead.types[k]
    }

    removed.each { |k|
      @attributes.delete(k)
      @types.delete(k)
    }
    self
  end
end