class Hubspot::ContactList

HubSpot Contact lists API

Constants

ADD_CONTACT_PATH
CONTACTS_PATH
LISTS_PATH
LIST_BATCH_PATH
LIST_PATH
RECENT_CONTACTS_PATH
REFRESH_PATH
REMOVE_CONTACT_PATH

Attributes

dynamic[R]
id[R]
name[R]
portal_id[R]
properties[R]

Public Class Methods

all(opts={}) click to toggle source

{developers.hubspot.com/docs/methods/lists/get_lists} {developers.hubspot.com/docs/methods/lists/get_static_lists} {developers.hubspot.com/docs/methods/lists/get_dynamic_lists}

# 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={}) click to toggle source

{developers.hubspot.com/docs/methods/lists/create_list}

# 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) click to toggle source

{developers.hubspot.com/docs/methods/lists/get_list} {developers.hubspot.com/docs/methods/lists/get_batch_lists}

# 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
new(hash) click to toggle source
# File lib/hubspot/contact_list.rb, line 59
def initialize(hash)
  self.send(:assign_properties, hash)
end

Public Instance Methods

add(contacts) click to toggle source

{developers.hubspot.com/docs/methods/lists/add_contact_to_list}

# 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={}) click to toggle source

{developers.hubspot.com/docs/methods/lists/get_list_contacts}

# 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!() click to toggle source

{developers.hubspot.com/docs/methods/lists/delete_list}

# 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?() click to toggle source
# File lib/hubspot/contact_list.rb, line 115
def destroyed?
  !!@destroyed
end
refresh() click to toggle source

{developers.hubspot.com/docs/methods/lists/refresh_list}

# 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) click to toggle source

{developers.hubspot.com/docs/methods/lists/remove_contact_from_list}

# 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={}) click to toggle source

{developers.hubspot.com/docs/methods/lists/update_list}

# 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

Private Instance Methods

assign_properties(hash) click to toggle source
# File lib/hubspot/contact_list.rb, line 121
def assign_properties(hash)
  @id = hash['listId']
  @portal_id = hash['portalId']
  @name = hash['name']
  @dynamic = hash['dynamic']
  @properties = hash
end