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
# File 'lib/hcp/lead.rb', line 12

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

#customer_for(params = {}) ⇒ Object



26
27
28
29
30
31
# File 'lib/hcp/lead.rb', line 26

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



19
20
21
22
23
24
# File 'lib/hcp/lead.rb', line 19

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

#uriObject



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

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