Class: Vng::Lead

Inherits:
Object
  • Object
show all
Defined in:
lib/vng/lead.rb

Overview

Provides methods to interact with Vonigo leads.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(id:, name:, email:, phone:) ⇒ Lead

Returns a new instance of Lead.



6
7
8
9
10
11
# File 'lib/vng/lead.rb', line 6

def initialize(id:, name:, email:, phone:)
  @id = id
  @name = name
  @email = email
  @phone = phone
end

Instance Attribute Details

#emailObject (readonly)

Returns the value of attribute email.



4
5
6
# File 'lib/vng/lead.rb', line 4

def email
  @email
end

#idObject (readonly)

Returns the value of attribute id.



4
5
6
# File 'lib/vng/lead.rb', line 4

def id
  @id
end

#nameObject (readonly)

Returns the value of attribute name.



4
5
6
# File 'lib/vng/lead.rb', line 4

def name
  @name
end

#phoneObject (readonly)

Returns the value of attribute phone.



4
5
6
# File 'lib/vng/lead.rb', line 4

def phone
  @phone
end

Class Method Details

.create(name:, email:, phone:) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/vng/lead.rb', line 13

def self.create(name:, email:, phone:)
  body = {
    securityToken: Vng.configuration.security_token,
    method: '3',
    Fields: [
       {fieldID: 121, optionID: '59'},
       {fieldID: 126, fieldValue: name},
       {fieldID: 238, fieldValue: email},
       {fieldID: 1024, fieldValue: phone},
    ]
  }

  uri = URI::HTTPS.build host: 'aussiepetmobileusatraining2.vonigo.com', path: '/api/v1/data/Leads/'

  request = Net::HTTP::Post.new(uri.request_uri)
  request.initialize_http_header 'Content-Type' => 'application/json'
  request.body = body.to_json

  response = Net::HTTP.start(uri.host, uri.port, use_ssl: true) do |http|
    http.request request
  end

  body = JSON response.body

  if body['errNo']
    # 'errNo'=>-600,
    # 'errMsg'=>'Data validation failed.',
    # 'dateNow'=>'1731342482',
    # 'Errors'=>[{"fieldID"=>1024, "fieldName"=>"Phone # to Reach You", "errNo"=>-602, "errMsg"=>"Field data is in incorrect format."}]}

    # raise Vng::Error
  end

  id = body['Client']['objectID']
  name = body['Fields'].find{|field| field['fieldID'] == 126}['fieldValue']
  email = body['Fields'].find{|field| field['fieldID'] == 238}['fieldValue']
  phone = body['Fields'].find{|field| field['fieldID'] == 1024}['fieldValue']

  new id: id, name: name, email: email, phone: phone
end

Instance Method Details

#destroyObject



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/vng/lead.rb', line 54

def destroy
  body = {
    securityToken: Vng.configuration.security_token,
    method: '4',
    objectID: id,
  }

  uri = URI::HTTPS.build host: 'aussiepetmobileusatraining2.vonigo.com', path: '/api/v1/data/Leads/'

  request = Net::HTTP::Post.new(uri.request_uri)
  request.initialize_http_header 'Content-Type' => 'application/json'
  request.body = body.to_json

  response = Net::HTTP.start(uri.host, uri.port, use_ssl: true) do |http|
    http.request request
  end
end