Class: Hcp::Lead

Inherits:
Resource show all
Defined in:
lib/hcp/lead.rb

Defined Under Namespace

Classes: Pipeline

Instance Attribute Summary collapse

Attributes inherited from Resource

#id

Instance Method Summary collapse

Constructor Details

#initialize(id: nil, customer_id: nil, key:, company_id:) ⇒ Lead

Returns a new instance of Lead.



5
6
7
8
9
10
# File 'lib/hcp/lead.rb', line 5

def initialize(id: nil, customer_id: nil, key:, company_id:)
  @id = id
  @key = key
  @company_id = company_id
  @customer_id = customer_id
end

Instance Attribute Details

#customer_idObject (readonly)

Returns the value of attribute customer_id.



3
4
5
# File 'lib/hcp/lead.rb', line 3

def customer_id
  @customer_id
end

Instance Method Details

#create(params = {}) ⇒ Object



12
13
14
15
16
17
18
19
# File 'lib/hcp/lead.rb', line 12

def create(params = {})
  response = Net::HTTP.post uri, lead_for(params).to_json, headers
  raise Error, response.body unless response.is_a? Net::HTTPOK
  body = JSON response.body
  @id, @customer_id = body['id'], body.dig('customer', 'id')
rescue Errno::ECONNREFUSED => error
  raise Error, error
end

#customer_for(params = {}) ⇒ Object



28
29
30
31
32
33
# File 'lib/hcp/lead.rb', line 28

def customer_for(params = {})
  {
    first_name: params[:name], email: params[:email],
    mobile_number: params[:phone], lead_source: params[:source],
  }.compact_blank
end

#lead_for(params = {}) ⇒ Object



21
22
23
24
25
26
# File 'lib/hcp/lead.rb', line 21

def lead_for(params = {})
  {
    customer: customer_for(params), address: params[:address],
    lead_source: params[:source], note: params[:note]
  }.compact_blank
end

#uriObject



35
# File 'lib/hcp/lead.rb', line 35

def uri = URI 'https://api.housecallpro.com/leads'