Class: PerfectReach::ContactList

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

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.all(options = {}) ⇒ Object



59
60
61
62
63
# File 'lib/perfect_reach/contact_list.rb', line 59

def all(options = {})
  (PerfectReach::Api.instance.get('contact_lists', options)["contact_lists"] || []).map do |result_hash|
    self.new(result_hash)
  end
end

.create(options = {}) ⇒ Object



53
54
55
56
57
# File 'lib/perfect_reach/contact_list.rb', line 53

def create(options = {})
  data = PerfectReach::Api.instance.post('contact_lists', options)
  throw data['error_msg'] unless 'ok'.eql? data['status']
  self.new(options.merge(:id => data["id"].to_s))
end

Instance Method Details

#add_contacts(contacts, options = {}) ⇒ Object



10
11
12
13
14
15
16
17
# File 'lib/perfect_reach/contact_list.rb', line 10

def add_contacts(contacts, options={})
  throw 'Contacts must be an Array' unless contacts.is_a? Array
  contact_data = []
  contacts.each do |p|
    contact_data << (p.is_a?(PerfectReach::Contact) ? {email: p.email.to_s, name: p.name.to_s} : p)
  end
  PerfectReach::Api.instance.post("contact_lists/#{self.id}/add_contacts", options.reverse_merge(:contacts => contacts.to_json))["status"]
end

#contacts(options = {}) ⇒ Object



34
35
36
37
38
# File 'lib/perfect_reach/contact_list.rb', line 34

def contacts(options = {})
  PerfectReach::Api.instance.get("contact_lists/#{self.id}/contacts", options)["contacts"].map do |contact|
    PerfectReach::Contact.new(contact)
  end
end

#delete(options = {}) ⇒ Object



48
49
50
# File 'lib/perfect_reach/contact_list.rb', line 48

def delete(options = {})
  PerfectReach::Api.instance.delete("contact_lists/#{self.id}", options)["status"]
end

#email(options = {}) ⇒ Object



40
41
42
# File 'lib/perfect_reach/contact_list.rb', line 40

def email(options = {})
  PerfectReach::Api.instance.get("contact_lists/#{self.id}/email", options)["email"]
end

#remove_contacts(contacts, options = {}) ⇒ Object



25
26
27
28
29
30
31
32
# File 'lib/perfect_reach/contact_list.rb', line 25

def remove_contacts(contacts, options={})
  throw 'Contacts must be an Array' unless contacts.is_a? Array
  contact_data = []
  contacts.each do |p|
    contact_data << (p.is_a?(PerfectReach::Contact) ? {email: p.email.to_s, name: p.name.to_s} : p)
  end
  PerfectReach::Api.instance.delete("contact_lists/#{self.id}/remove_contacts", options.reverse_merge(:contacts => contacts.to_json))["status"]
end

#statistics(options = {}) ⇒ Object



44
45
46
# File 'lib/perfect_reach/contact_list.rb', line 44

def statistics(options = {})
  PerfectReach::Api.instance.get("contact_lists/#{self.id}/statistics", options)["statistics"]
end

#unsubscribe_contact(*params) ⇒ Object



19
20
21
22
23
# File 'lib/perfect_reach/contact_list.rb', line 19

def unsubscribe_contact(*params)
  options = params.last.is_a?(Hash) ? params.pop : {}
  contact = params.map { |p| p.is_a?(PerfectReach::Contact) ? p.email.to_s : p.to_s }.reject(&:blank?).first
  PerfectReach::Api.instance.post("contact_lists/#{self.id}/unsubscribe_contact", options.reverse_merge(:contact => contact))["status"]
end

#update(options = {}) ⇒ Object



6
7
8
# File 'lib/perfect_reach/contact_list.rb', line 6

def update(options = {})
  PerfectReach::Api.instance.put("contact_lists/#{self.id}", options)["status"]
end