Class: Bronto::List

Inherits:
Base show all
Defined in:
lib/bronto/list.rb

Instance Attribute Summary collapse

Attributes inherited from Base

#errors, #id

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Base

api, api_key, api_key=, #create, create, destroy, #destroy, find, plural_class_name, #reload, request, #request, save, #save, soap_header, update, #update

Constructor Details

#initialize(options = {}) ⇒ List

Returns a new instance of List.



18
19
20
21
# File 'lib/bronto/list.rb', line 18

def initialize(options = {})
  super(options)
  self.active_count ||= 0
end

Instance Attribute Details

#active_countObject

Returns the value of attribute active_count.



3
4
5
# File 'lib/bronto/list.rb', line 3

def active_count
  @active_count
end

#labelObject

Returns the value of attribute label.



3
4
5
# File 'lib/bronto/list.rb', line 3

def label
  @label
end

#nameObject

Returns the value of attribute name.



3
4
5
# File 'lib/bronto/list.rb', line 3

def name
  @name
end

#statusObject

Returns the value of attribute status.



3
4
5
# File 'lib/bronto/list.rb', line 3

def status
  @status
end

#visibilityObject

Returns the value of attribute visibility.



3
4
5
# File 'lib/bronto/list.rb', line 3

def visibility
  @visibility
end

Class Method Details

.clear_lists(*lists) ⇒ Object

Removes all contacts from the given lists.



6
7
8
9
10
11
12
13
14
15
16
# File 'lib/bronto/list.rb', line 6

def self.clear_lists(*lists)
  lists = lists.flatten

  resp = request(:clear) do
    soap.body = {
      list: lists.map { |l| { id: l.id } }
    }
  end

  Array.wrap(resp[:return][:results]).select { |r| r[:is_error] }.count == 0
end

Instance Method Details

#add_to_list(*contacts) ⇒ Object

Adds the given contacts to this list.



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/bronto/list.rb', line 24

def add_to_list(*contacts)
  return false if !self.id.present?
  contacts = contacts.flatten

  # The block below is evaluated in a weird scope so we need to capture self as _self for use inside the block.
  _self = self

  resp = request("add_to_list") do
    soap.body = {
      list: { id: _self.id },
      contacts: contacts.map { |c| { id: c.id } }
    }
  end

  Array.wrap(resp[:return][:results]).select { |r| r[:is_error] }.count == 0
end

#remove_from_list(*contacts) ⇒ Object

Removes the given contacts from this list.



42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/bronto/list.rb', line 42

def remove_from_list(*contacts)
  return false if !self.id.present?
  contacts = contacts.flatten

  resp = request("remove_from_list") do
    soap.body = {
      list: self.to_hash,
      contacts: contacts.map(&:to_hash)
    }
  end

  Array.wrap(resp[:return][:results]).select { |r| r[:is_error] }.count == 0
end

#to_hashObject



60
61
62
63
64
# File 'lib/bronto/list.rb', line 60

def to_hash
  hash = { name: name, label: label, status: status, visibility: visibility }
  hash[:id] = id if id.present?
  hash
end