Class: Vng::Contact

Inherits:
Resource show all
Defined in:
lib/vng/contact.rb

Overview

Provides methods to interact with Vonigo contacts.

Constant Summary collapse

PATH =
'/api/v1/data/Contacts/'

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(id:, first_name:, last_name:, email:, phone:, client_id:) ⇒ Contact

Returns a new instance of Contact.



10
11
12
13
14
15
16
17
# File 'lib/vng/contact.rb', line 10

def initialize(id:, first_name:, last_name:, email:, phone:, client_id:)
  @id = id
  @first_name = first_name
  @last_name = last_name
  @email = email
  @phone = phone
  @client_id = client_id
end

Instance Attribute Details

#client_idObject (readonly)

Returns the value of attribute client_id.



8
9
10
# File 'lib/vng/contact.rb', line 8

def client_id
  @client_id
end

#emailObject (readonly)

Returns the value of attribute email.



8
9
10
# File 'lib/vng/contact.rb', line 8

def email
  @email
end

#first_nameObject (readonly)

Returns the value of attribute first_name.



8
9
10
# File 'lib/vng/contact.rb', line 8

def first_name
  @first_name
end

#idObject (readonly)

Returns the value of attribute id.



8
9
10
# File 'lib/vng/contact.rb', line 8

def id
  @id
end

#last_nameObject (readonly)

Returns the value of attribute last_name.



8
9
10
# File 'lib/vng/contact.rb', line 8

def last_name
  @last_name
end

#phoneObject (readonly)

Returns the value of attribute phone.



8
9
10
# File 'lib/vng/contact.rb', line 8

def phone
  @phone
end

Class Method Details

.allObject

TODO: add (edited_after: param)



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/vng/contact.rb', line 19

def self.all # TODO: add (edited_after: param)
  body = { isCompleteObject: 'true' }

  data = request path: PATH, body: body, returning: 'Contacts'

  data.filter_map do |body|
    next unless body['isActive']

    id = body['objectID']
    first_name = value_for_field body, 127
    last_name = value_for_field body, 128
    email = value_for_field body, 97
    phone = value_for_field body, 96
    client_id = value_for_relation body, 'client'

    new id: id, first_name: first_name, last_name: last_name, email: email, phone: phone, client_id: client_id
  end
end

.create(first_name:, last_name:, email:, phone:, client_id:) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/vng/contact.rb', line 38

def self.create(first_name:, last_name:, email:, phone:, client_id:)
  body = {
    method: '3',
    clientID: client_id,
    Fields: [
       { fieldID: 127, fieldValue: first_name },
       { fieldID: 128, fieldValue: last_name },
       { fieldID: 97, fieldValue: URI.encode_uri_component(email) },
       { fieldID: 96, fieldValue: phone },
    ]
  }

  data = request path: PATH, body: body

  id = data['Contact']['objectID']
  first_name = value_for_field data, 127
  last_name = value_for_field data, 128
  email = value_for_field data, 97
  phone = value_for_field data, 96

  new id: id, first_name: first_name, last_name: last_name, email: email, phone: phone, client_id: client_id
end