Class: Hubspot::ContactList

Inherits:
Object
  • Object
show all
Defined in:
lib/hubspot/contact_list.rb

Overview

HubSpot Contact lists API

Constant Summary collapse

LISTS_PATH =
'/contacts/v1/lists'
LIST_PATH =
'/contacts/v1/lists/:list_id'
LIST_BATCH_PATH =
LISTS_PATH + '/batch'
CONTACTS_PATH =
LIST_PATH + '/contacts/all'
RECENT_CONTACTS_PATH =
LIST_PATH + '/contacts/recent'
ADD_CONTACT_PATH =
LIST_PATH + '/add'
REMOVE_CONTACT_PATH =
LIST_PATH + '/remove'

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(hash) ⇒ ContactList

Returns a new instance of ContactList.



58
59
60
# File 'lib/hubspot/contact_list.rb', line 58

def initialize(hash)
  self.send(:assign_properties, hash)
end

Instance Attribute Details

#dynamicObject (readonly)

Returns the value of attribute dynamic.



55
56
57
# File 'lib/hubspot/contact_list.rb', line 55

def dynamic
  @dynamic
end

#idObject (readonly)

Returns the value of attribute id.



52
53
54
# File 'lib/hubspot/contact_list.rb', line 52

def id
  @id
end

#nameObject (readonly)

Returns the value of attribute name.



54
55
56
# File 'lib/hubspot/contact_list.rb', line 54

def name
  @name
end

#portal_idObject (readonly)

Returns the value of attribute portal_id.



53
54
55
# File 'lib/hubspot/contact_list.rb', line 53

def portal_id
  @portal_id
end

#propertiesObject (readonly)

Returns the value of attribute properties.



56
57
58
# File 'lib/hubspot/contact_list.rb', line 56

def properties
  @properties
end

Class Method Details

.all(opts = {}) ⇒ Object



27
28
29
30
31
32
33
34
35
# File 'lib/hubspot/contact_list.rb', line 27

def all(opts={})
  static = opts.delete(:static) { false }
  dynamic = opts.delete(:dynamic) { false }

  # NOTE: As opposed of what the documentation says, getting the static or dynamic lists returns all the lists, not only 20 lists
  path = LISTS_PATH + (static ? '/static' : dynamic ? '/dynamic' : '')
  response = Hubspot::Connection.get_json(path, opts)
  response['lists'].map { |l| new(l) }
end

.create!(opts = {}) ⇒ Object



16
17
18
19
20
21
22
# File 'lib/hubspot/contact_list.rb', line 16

def create!(opts={})
  dynamic = opts.delete(:dynamic) { false }
  portal_id = opts.delete(:portal_id) { Hubspot::Config.portal_id }

  response = Hubspot::Connection.post_json(LISTS_PATH, params: {}, body: opts.merge({ dynamic: dynamic, portal_id: portal_id}) )
  new(response)
end

.find(ids) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
# File 'lib/hubspot/contact_list.rb', line 39

def find(ids)
  batch_mode, path, params = case ids
  when Integer then [false, LIST_PATH, { list_id: ids }]
  when String then [false, LIST_PATH, { list_id: ids.to_i }]
  when Array then [true, LIST_BATCH_PATH, { batch_list_id: ids.map(&:to_i) }]
  else raise Hubspot::InvalidParams, 'expecting Integer or Array of Integers parameter'
  end

  response = Hubspot::Connection.get_json(path, params)
  batch_mode ? response['lists'].map { |l| new(l) } : new(response)
end

Instance Method Details

#add(contacts) ⇒ Object



95
96
97
98
99
# File 'lib/hubspot/contact_list.rb', line 95

def add(contacts)
  contact_ids = [contacts].flatten.uniq.compact.map(&:id)
  response = Hubspot::Connection.post_json(ADD_CONTACT_PATH, params: { list_id: @id }, body: { vids: contact_ids })
  response['updated'].sort == contact_ids.sort
end

#contacts(opts = {}) ⇒ Object



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/hubspot/contact_list.rb', line 76

def contacts(opts={})
  # NOTE: caching functionality can be dependant of the nature of the list, if dynamic or not ...
  bypass_cache = opts.delete(:bypass_cache) { false }
  recent = opts.delete(:recent) { false }
  paged = opts.delete(:paged) { false }

  if bypass_cache || @contacts.nil?
    path = recent ? RECENT_CONTACTS_PATH : CONTACTS_PATH
    opts[:list_id] = @id

    response = Hubspot::Connection.get_json(path, Hubspot::ContactProperties.add_default_parameters(opts))
    @contacts = response['contacts'].map! { |c| Hubspot::Contact.from_result(c) }
    paged ? response : @contacts
  else
    @contacts
  end
end

#destroy!Object



70
71
72
73
# File 'lib/hubspot/contact_list.rb', line 70

def destroy!
  response = Hubspot::Connection.delete_json(LIST_PATH, { list_id: @id })
  @destroyed = (response.code == 204)
end

#destroyed?Boolean

Returns:

  • (Boolean)


108
109
110
# File 'lib/hubspot/contact_list.rb', line 108

def destroyed?
  !!@destroyed
end

#remove(contacts) ⇒ Object



102
103
104
105
106
# File 'lib/hubspot/contact_list.rb', line 102

def remove(contacts)
  contact_ids = [contacts].flatten.uniq.compact.map(&:id)
  response = Hubspot::Connection.post_json(REMOVE_CONTACT_PATH, params: { list_id: @id }, body: { vids: contact_ids })
  response['updated'].sort == contact_ids.sort
end

#update!(opts = {}) ⇒ Object



63
64
65
66
67
# File 'lib/hubspot/contact_list.rb', line 63

def update!(opts={})
  response = Hubspot::Connection.post_json(LIST_PATH, params: { list_id: @id }, body: opts)
  self.send(:assign_properties, response)
  self
end