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

#emailObject (readonly)

Returns the value of attribute email.



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

def email
  @email
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, list_id) ⇒ Object



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/exacto_subscriber/subscriber.rb', line 78

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

.normalize(resulting_hash) ⇒ Object



98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/exacto_subscriber/subscriber.rb', line 98

def self.normalize(resulting_hash)
  new_hash = {}
  resulting_hash.each do |key, value|
    if key =~ /subid/
      new_hash[:subscriber_id] = value
    elsif key =~ /Email\_\_Address/
      new_hash[:email] = value
    else
      new_hash[key.gsub(/\_\_/, "_").gsub(/id/, "_id").downcase.to_sym] = value
    end
  end
  
  new_hash
end

.system_nameObject



113
114
115
# File 'lib/exacto_subscriber/subscriber.rb', line 113

def self.system_name
  "subscriber"
end

Instance Method Details

#createObject



32
33
34
35
36
37
38
39
40
# File 'lib/exacto_subscriber/subscriber.rb', line 32

def create
  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



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

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

#resource_xml(xml) ⇒ Object



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

def resource_xml(xml)
  xml.values do
    xml.email__address @email
    xml.status @status || "active"
    (self.attributes || {}).each do |field, val|
      xml.send(field, val) 
    end
  end
end

#subscribe_to(list_id) ⇒ Object



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

def subscribe_to(list_id)
  @list_id = list_id
  if item = self.class.find_by_email_and_list_id(email, list_id)
    item.list_id = list_id
    item.update
  else
    create
  end
end

#unsubscribe_from(list_id) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
# File 'lib/exacto_subscriber/subscriber.rb', line 20

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

#updateObject



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

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

#update_from_options(options) ⇒ Object



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

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