Class: Exacto::Subscriber
- Defined in:
- lib/exacto_subscriber/subscriber.rb
Instance Attribute Summary collapse
-
#attributes ⇒ Object
Returns the value of attribute attributes.
-
#email ⇒ Object
readonly
Returns the value of attribute email.
-
#list_id ⇒ Object
Returns the value of attribute list_id.
-
#status ⇒ Object
Returns the value of attribute status.
-
#subscriber_id ⇒ Object
readonly
Returns the value of attribute subscriber_id.
Class Method Summary collapse
- .find_by_email_and_list_id(email, list_id) ⇒ Object
- .normalize(resulting_hash) ⇒ Object
- .system_name ⇒ Object
Instance Method Summary collapse
- #create ⇒ Object
- #destroy ⇒ Object
-
#initialize(options = {}) ⇒ Subscriber
constructor
A new instance of Subscriber.
- #resource_xml(xml) ⇒ Object
- #subscribe_to(list_id) ⇒ Object
- #unsubscribe_from(list_id) ⇒ Object
- #update ⇒ Object
- #update_from_options(options) ⇒ Object
Constructor Details
#initialize(options = {}) ⇒ Subscriber
Returns a new instance of Subscriber.
6 7 8 |
# File 'lib/exacto_subscriber/subscriber.rb', line 6 def initialize( = {}) () end |
Instance Attribute Details
#attributes ⇒ Object
Returns the value of attribute attributes.
4 5 6 |
# File 'lib/exacto_subscriber/subscriber.rb', line 4 def attributes @attributes end |
#email ⇒ Object (readonly)
Returns the value of attribute email.
3 4 5 |
# File 'lib/exacto_subscriber/subscriber.rb', line 3 def email @email end |
#list_id ⇒ Object
Returns the value of attribute list_id.
4 5 6 |
# File 'lib/exacto_subscriber/subscriber.rb', line 4 def list_id @list_id end |
#status ⇒ Object
Returns the value of attribute status.
4 5 6 |
# File 'lib/exacto_subscriber/subscriber.rb', line 4 def status @status end |
#subscriber_id ⇒ Object (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
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 |
# File 'lib/exacto_subscriber/subscriber.rb', line 84 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
104 105 106 107 108 109 110 111 112 113 114 115 116 117 |
# File 'lib/exacto_subscriber/subscriber.rb', line 104 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_name ⇒ Object
119 120 121 |
# File 'lib/exacto_subscriber/subscriber.rb', line 119 def self.system_name "subscriber" end |
Instance Method Details
#create ⇒ Object
36 37 38 39 40 41 42 43 44 45 |
# File 'lib/exacto_subscriber/subscriber.rb', line 36 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 |
#destroy ⇒ Object
68 69 70 71 72 73 74 |
# File 'lib/exacto_subscriber/subscriber.rb', line 68 def destroy issue_request do |xml| xml.action "delete" xml.search_type "subid" xml.search_value subscriber_id end end |
#resource_xml(xml) ⇒ Object
58 59 60 61 62 63 64 65 66 |
# File 'lib/exacto_subscriber/subscriber.rb', line 58 def resource_xml(xml) xml.values do xml.email__address @email 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 |
# 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, list_id) item.list_id = list_id item.status = @status item.attributes = attributes item.update else create end end |
#unsubscribe_from(list_id) ⇒ Object
23 24 25 26 27 28 29 30 31 32 33 34 |
# File 'lib/exacto_subscriber/subscriber.rb', line 23 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.attributes = attributes item.update else create end end |
#update ⇒ Object
47 48 49 50 51 52 53 54 55 56 |
# File 'lib/exacto_subscriber/subscriber.rb', line 47 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
76 77 78 79 80 81 82 |
# File 'lib/exacto_subscriber/subscriber.rb', line 76 def () @email = [:email] @status = [:status] @subscriber_id = [:subscriber_id] @list_id = [:list_id] @attributes = [:attributes] || {} end |