class MarketoAPI::Lead

An object representing a Marketo Lead record.

Attributes

attributes[R]

The attributes for the Lead.

id[R]

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

proxy[R]

The proxy object for this class.

types[R]

The types for the Lead attributes.

Public Class Methods

key(key, value) click to toggle source

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

# File lib/marketo_api/lead.rb, line 235
def key(key, value)
  {
    leadKey: {
      keyType:  key_type(key),
      keyValue: value
    }
  }
end
values() → array click to toggle source

:method: values

Returns the attribute values.

# 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

Private Class Methods

key_type(key) click to toggle source
# File lib/marketo_api/lead.rb, line 245
def key_type(key)
  key = key.to_sym
  res = if KEY_TYPES.include? key
           key
         else
           NAMED_KEYS[key]
         end
  raise ArgumentError, "Invalid key #{key}" unless res
  res
end

Public Instance Methods

==(other) click to toggle source
# 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
lead[attribute_key] click to toggle source

Looks up the provided attribute.

# File lib/marketo_api/lead.rb, line 39
  
lead[key] = value → value click to toggle source

:method: []=(hash, value)

Looks up the provided attribute.

# File lib/marketo_api/lead.rb, line 103
def []=(key, value)
  @attributes[key] = value
  @types[key] ||= infer_value_type(value)
end
each { |key, value| block } click to toggle source

Iterates over the attributes.

# File lib/marketo_api/lead.rb, line 46
  
each_key { |key| block } click to toggle source

Iterates over the attribute keys.

# File lib/marketo_api/lead.rb, line 60
  
each_pair { |key, value| block } click to toggle source

Iterates over the attributes.

# File lib/marketo_api/lead.rb, line 53
  
each_value { |value| block } click to toggle source

Iterates over the attribute values.

# File lib/marketo_api/lead.rb, line 67
  
email() click to toggle source

:attr_reader: email

# File lib/marketo_api/lead.rb, line 141
def email
  self[:Email]
end
email=(value) click to toggle source

:attr_writer: email

# File lib/marketo_api/lead.rb, line 146
def email=(value)
  self[:Email] = value
end
foreign → nil click to toggle source
foreign(type, id) → { type: type, id: id }
foreign → { type: type, id: id }

Sets or returns the foreign system type and person ID.

# 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
inspect() click to toggle source
# 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
keys() → array click to toggle source

Returns the attribute keys.

# File lib/marketo_api/lead.rb, line 74
  
params_for_get() click to toggle source

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

# File lib/marketo_api/lead.rb, line 188
def params_for_get
  self.class.key(:IDNUM, id)
end
params_for_sync() click to toggle source

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

# 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
proxy = proxy → proxy click to toggle source

:attr_writer:

Assign a proxy object. Once set, the proxy cannot be unset, but it can be changed.

# File lib/marketo_api/lead.rb, line 114
def proxy=(value)
  @proxy = case value
           when nil
             defined?(@proxy) && @proxy
           when MarketoAPI::Leads
             value
           when MarketoAPI::ClientProxy
             value.instance_variable_get(:@client).leads
           when MarketoAPI::Client
             value.leads
           else
             raise ArgumentError, "Invalid proxy type"
           end
end
sync() click to toggle source

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=.

# File lib/marketo_api/lead.rb, line 155
def sync
  raise ArgumentError, "No proxy configured" unless proxy
  proxy.sync(self)
end
sync!() click to toggle source

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=.

# 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

Private Instance Methods

infer_value_type(value) click to toggle source
# File lib/marketo_api/lead.rb, line 267
def infer_value_type(value)
  case value
  when Integer
    'integer'
  when Time, DateTime
    'datetime'
  else
    'string'
  end
end