Class: RHapi::Lead

Inherits:
Object
  • Object
show all
Extended by:
Connection::ClassMethods
Includes:
Connection
Defined in:
lib/r_hapi/lead.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Connection::ClassMethods

append_options, get, url_for

Methods included from Connection

#put

Constructor Details

#initialize(data) ⇒ Lead

Returns a new instance of Lead.



14
15
16
17
# File 'lib/r_hapi/lead.rb', line 14

def initialize(data)
  self.attributes = data
  self.changed_attributes = {}
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

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

Work with data in the data hash



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/r_hapi/lead.rb', line 65

def method_missing(method, *args, &block)
  
  attribute = ActiveSupport::Inflector.camelize(method.to_s, false)
  
  if attribute =~ /=$/
    attribute = attribute.chop
    return super unless self.attributes.include?(attribute)
    self.changed_attributes[attribute] = args[0]
    self.attributes[attribute] = args[0]
  else
    return super unless self.attributes.include?(attribute)
    self.attributes[attribute]
  end 
        
end

Instance Attribute Details

#attributesObject

Returns the value of attribute attributes.



12
13
14
# File 'lib/r_hapi/lead.rb', line 12

def attributes
  @attributes
end

#changed_attributesObject

Returns the value of attribute changed_attributes.



12
13
14
# File 'lib/r_hapi/lead.rb', line 12

def changed_attributes
  @changed_attributes
end

Class Method Details

.find(search = nil, options = {}) ⇒ Object

Finds leads and returns an array of leads. An optional string value that is used to search several basic lead fields: first name, last name, email address, and company name. According to HubSpot docs, a more advanced search is coming in the future. The default value for is nil, meaning return all leads.



25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/r_hapi/lead.rb', line 25

def self.find(search=nil, options={})  
  options[:search] = search unless search.nil?
  response = get(url_for("list", nil, options))
 
  lead_data = JSON.parse(response.body_str)
  leads = []
  lead_data.each do |data|
    lead = Lead.new(data)
    leads << lead
  end
  leads
end

.find_by_guid(guid) ⇒ Object

Finds specified lead by the guid.



39
40
41
42
43
# File 'lib/r_hapi/lead.rb', line 39

def self.find_by_guid(guid)
  response = get(url_for("lead", guid))
  lead_data = JSON.parse(response.body_str)
  Lead.new(lead_data)
end

Instance Method Details

#update(params = {}) ⇒ Object

Instance methods ——————————————————-



46
47
48
49
50
# File 'lib/r_hapi/lead.rb', line 46

def update(params={})
  update_attributes(params) unless params.empty?
  response = put(Lead.url_for("lead", self.guid), self.changed_attributes)
  true
end

#update_attributes(params) ⇒ Object



52
53
54
55
56
57
58
59
60
# File 'lib/r_hapi/lead.rb', line 52

def update_attributes(params)
  raise(RHapi::AttributeError, "The params must be a hash.") unless params.is_a?(Hash)
  params.each do |key, value|
    attribute = ActiveSupport::Inflector.camelize(key.to_s, false)
    raise(RHapi::AttributeError, "No Hubspot attribute with the name #{attribute}.") unless self.attributes.include?(attribute)
    self.changed_attributes[attribute] = value
    self.attributes[attribute] = value
  end
end