Class: Exacto::Subscriber

Inherits:
Base
  • Object
show all
Defined in:
lib/exacto_subscriber/subscriber.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Subscriber

Returns a new instance of Subscriber.



6
7
8
# File 'lib/exacto_subscriber/subscriber.rb', line 6

def initialize(options = {})
  update_from_options(options)
end

Instance Attribute Details

#attributesObject

Returns the value of attribute attributes.



4
5
6
# File 'lib/exacto_subscriber/subscriber.rb', line 4

def attributes
  @attributes
end

#email_addressObject (readonly)

Returns the value of attribute email_address.



3
4
5
# File 'lib/exacto_subscriber/subscriber.rb', line 3

def email_address
  @email_address
end

#list_idObject

Returns the value of attribute list_id.



4
5
6
# File 'lib/exacto_subscriber/subscriber.rb', line 4

def list_id
  @list_id
end

#statusObject

Returns the value of attribute status.



4
5
6
# File 'lib/exacto_subscriber/subscriber.rb', line 4

def status
  @status
end

#subscriber_idObject (readonly)

Returns the value of attribute subscriber_id.



3
4
5
# File 'lib/exacto_subscriber/subscriber.rb', line 3

def subscriber_id
  @subscriber_id
end

Class Method Details

.find_by_email_and_list_id(email_address, list_id) ⇒ Object



88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/exacto_subscriber/subscriber.rb', line 88

def self.find_by_email_and_list_id(email_address, list_id)
  begin
    result = issue_request do |xml|
      xml.action "retrieve"
      xml.search_type "listid"
      xml.search_value list_id
      xml.search_value2 email_address
      xml.showChannelID nil
    end
  rescue Error => e
    if e.code == "1"
      result = nil
    else
      raise e
    end
  end
  
  result.nil? || result.empty? ? nil : new(normalize(result))
end

.system_nameObject



108
109
110
# File 'lib/exacto_subscriber/subscriber.rb', line 108

def self.system_name
  "subscriber"
end

Instance Method Details

#createObject



40
41
42
43
44
45
46
47
48
49
# File 'lib/exacto_subscriber/subscriber.rb', line 40

def create
  @status ||= "active"
  issue_request do |xml|
    xml.action "add"
    xml.search_type "listid"
    xml.search_value @list_id
    xml.search_value2 nil
    resource_xml(xml)
  end
end

#destroyObject



72
73
74
75
76
77
78
# File 'lib/exacto_subscriber/subscriber.rb', line 72

def destroy
  issue_request do |xml|
    xml.action "delete"
    xml.search_type "subid"
    xml.search_value subscriber_id
  end
end

#resource_xml(xml) ⇒ Object



62
63
64
65
66
67
68
69
70
# File 'lib/exacto_subscriber/subscriber.rb', line 62

def resource_xml(xml)
  xml.values do
    xml.email__address @email_address
    xml.status @status if @status
    (self.attributes || {}).each do |field, val|
      xml.send(field.to_s.gsub("_", "__"), val) 
    end
  end
end

#subscribe_to(list_id) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/exacto_subscriber/subscriber.rb', line 10

def subscribe_to(list_id)
  @status = "active"
  @list_id = list_id
  if item = self.class.find_by_email_and_list_id(email_address, list_id)
    item.list_id = list_id
    item.status = @status
    item.attributes = attributes
    item.update
  else
    create
  end
  
  self
end

#unsubscribe_from(list_id) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/exacto_subscriber/subscriber.rb', line 25

def unsubscribe_from(list_id)
  @list_id = list_id
  @status = "Unsubscribed"
  if item = self.class.find_by_email_and_list_id(email_address, list_id)
    item.list_id = list_id
    item.status = status
    item.attributes = attributes
    item.update
  else
    create
  end
  
  self
end

#updateObject



51
52
53
54
55
56
57
58
59
60
# File 'lib/exacto_subscriber/subscriber.rb', line 51

def update
  result = issue_request do |xml|
    xml.action "edit"
    xml.search_type "listid"
    xml.search_value @list_id
    xml.search_value2 email_address
    resource_xml(xml)
    xml.update true
  end
end

#update_from_options(options) ⇒ Object



80
81
82
83
84
85
86
# File 'lib/exacto_subscriber/subscriber.rb', line 80

def update_from_options(options)
  @email_address  = options[:email_address]
  @status = options[:status]
  @subscriber_id = options[:subscriber_id]
  @list_id    = options[:list_id]
  @attributes = options[:attributes] || {}
end