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'
REFRESH_PATH =
LIST_PATH + '/refresh'

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(hash) ⇒ ContactList

Returns a new instance of ContactList.



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

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

Instance Attribute Details

#dynamicObject (readonly)

Returns the value of attribute dynamic.



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

def dynamic
  @dynamic
end

#idObject (readonly)

Returns the value of attribute id.



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

def id
  @id
end

#nameObject (readonly)

Returns the value of attribute name.



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

def name
  @name
end

#portal_idObject (readonly)

Returns the value of attribute portal_id.



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

def portal_id
  @portal_id
end

#propertiesObject (readonly)

Returns the value of attribute properties.



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

def properties
  @properties
end

Class Method Details

.all(opts = {}) ⇒ Object



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

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



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

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



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

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



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

def add(contacts)
  contact_ids = [contacts].flatten.uniq.compact.map(&:vid)
  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



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

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.new(c) }
    paged ? response : @contacts
  else
    @contacts
  end
end

#destroy!Object



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

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

#destroyed?Boolean

Returns:

  • (Boolean)


115
116
117
# File 'lib/hubspot/contact_list.rb', line 115

def destroyed?
  !!@destroyed
end

#refreshObject



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

def refresh
  response = Hubspot::Connection.post_json(REFRESH_PATH, params: { list_id: @id, no_parse: true }, body: {})
  response.code == 204
end

#remove(contacts) ⇒ Object



109
110
111
112
113
# File 'lib/hubspot/contact_list.rb', line 109

def remove(contacts)
  contact_ids = [contacts].flatten.uniq.compact.map(&:vid)
  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



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

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