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/'

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data = {}) ⇒ Contact

Returns a new instance of Contact.

Parameters:

  • data (Hash<Symbol, String>) (defaults to: {})

    the options to initialize a resource.

Options Hash (data):

  • :object_id (String)

    The unique ID of a Vonigo resource.

  • :fields (Array)

    The fields of a Vonigo resource.



14
15
16
# File 'lib/vng/contact.rb', line 14

def initialize(data = {})
  @data = data
end

Class Method Details

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



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

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
  new data['Contact']
end

.edited_since(timestamp) ⇒ Object



57
58
59
60
61
62
63
# File 'lib/vng/contact.rb', line 57

def self.edited_since(timestamp)
  contacts = where isCompleteObject: 'true', dateStart: timestamp.to_i, dateMode: 2
  # TODO: select should accept sub-hash, e.g.
  # .select :date_last_edited, fields: [134, 1036], relations: ['client']
  contacts = contacts.select 'dateLastEdited', 'Fields', 'Relations'
  contacts.lazy.filter(&:active?)
end

.where(conditions = {}) ⇒ Vng::Relation

Returns the resources matching the conditions.

Returns:



52
53
54
55
# File 'lib/vng/contact.rb', line 52

def self.where(conditions = {})
  @where ||= Relation.new
  @where.where conditions
end

Instance Method Details

#active?Boolean

Returns:

  • (Boolean)


23
24
25
# File 'lib/vng/contact.rb', line 23

def active?
  @data['isActive'] != 'false'
end

#client_idObject



43
44
45
# File 'lib/vng/contact.rb', line 43

def client_id
  self.class.value_for_relation @data, 'client'
end

#edited_atObject



47
48
49
# File 'lib/vng/contact.rb', line 47

def edited_at
  Time.at Integer(@data['dateLastEdited']), in: 'UTC'
end

#emailObject



35
36
37
# File 'lib/vng/contact.rb', line 35

def email
  self.class.value_for_field @data, 97
end

#first_nameObject



27
28
29
# File 'lib/vng/contact.rb', line 27

def first_name
  self.class.value_for_field @data, 127
end

#idString

Returns the resource’s unique ID.

Returns:

  • (String)

    the resource’s unique ID.



19
20
21
# File 'lib/vng/contact.rb', line 19

def id
  @data['objectID']
end

#last_nameObject



31
32
33
# File 'lib/vng/contact.rb', line 31

def last_name
  self.class.value_for_field @data, 128
end

#phoneObject



39
40
41
# File 'lib/vng/contact.rb', line 39

def phone
  self.class.value_for_field @data, 96
end