Class: EmailCampaign::Recipient
- Inherits:
-
ActiveRecord::Base
- Object
- ActiveRecord::Base
- EmailCampaign::Recipient
- Defined in:
- app/models/email_campaign/recipient.rb
Class Method Summary collapse
- .per_page ⇒ Object
- .record_click(identifier, params = {}) ⇒ Object
- .record_open(identifier, params = {}) ⇒ Object
- .resubscribe(identifier, params = {}) ⇒ Object
- .unsubscribe(identifier, params = {}) ⇒ Object
Instance Method Summary collapse
- #check_email_address ⇒ Object
- #check_for_duplicates ⇒ Object
- #check_for_unsubscribe ⇒ Object
- #check_name ⇒ Object
- #deliver ⇒ Object
- #generate_identifier(regenerate = false) ⇒ Object
- #queue ⇒ Object
- #record_click(params = nil) ⇒ Object
- #record_open(params = {}) ⇒ Object
-
#requeue ⇒ Object
resets failed and delivered flags…
- #resubscribe(params = nil) ⇒ Object
- #to_s ⇒ Object
- #unsubscribe(params = nil) ⇒ Object
- #valid_email_address?(value) ⇒ Boolean
Class Method Details
.per_page ⇒ Object
17 18 19 |
# File 'app/models/email_campaign/recipient.rb', line 17 def self.per_page 100 end |
.record_click(identifier, params = {}) ⇒ Object
152 153 154 155 |
# File 'app/models/email_campaign/recipient.rb', line 152 def self.record_click(identifier, params = {}) r = find_by_identifier(identifier) r.record_click(params) if r end |
.record_open(identifier, params = {}) ⇒ Object
138 139 140 141 |
# File 'app/models/email_campaign/recipient.rb', line 138 def self.record_open(identifier, params = {}) r = find_by_identifier(identifier) r.record_open(params) if r end |
.resubscribe(identifier, params = {}) ⇒ Object
183 184 185 186 |
# File 'app/models/email_campaign/recipient.rb', line 183 def self.resubscribe(identifier, params = {}) r = find_by_identifier(identifier) r.resubscribe(params) if r end |
.unsubscribe(identifier, params = {}) ⇒ Object
169 170 171 172 |
# File 'app/models/email_campaign/recipient.rb', line 169 def self.unsubscribe(identifier, params = {}) r = find_by_identifier(identifier) r.unsubscribe(params) if r end |
Instance Method Details
#check_email_address ⇒ Object
60 61 62 63 64 65 66 67 68 69 70 71 |
# File 'app/models/email_campaign/recipient.rb', line 60 def check_email_address self.email_address = email_address.blank? ? nil : email_address.strip if valid_email_address?(email_address) self.invalid_email = false else self.ready = false self.invalid_email = true end true end |
#check_for_duplicates ⇒ Object
49 50 51 52 53 54 55 56 57 58 |
# File 'app/models/email_campaign/recipient.rb', line 49 def check_for_duplicates if campaign.recipients.where(:email_address => email_address).count > 0 self.ready = false self.duplicate = true else self.duplicate = false end true end |
#check_for_unsubscribe ⇒ Object
73 74 75 76 77 78 79 80 |
# File 'app/models/email_campaign/recipient.rb', line 73 def check_for_unsubscribe if self.class.where(:email_address => email_address, :unsubscribed => true).count > 0 self.unsubscribed = true self.ready = false end true end |
#check_name ⇒ Object
43 44 45 46 47 |
# File 'app/models/email_campaign/recipient.rb', line 43 def check_name self.name = name.blank? ? nil : name.strip true end |
#deliver ⇒ Object
97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 |
# File 'app/models/email_campaign/recipient.rb', line 97 def deliver if !ready puts "Not in ready state" return false end if delivered puts "Already delivered." return true # not sure yet whether returning true is a good idea, but seems harmless enough end if failed puts "Already failed (reason: #{failure_reason}), not going to try again." update_attributes(:ready => false) return false end unless update_column(:attempted, true) && increment(:attempts) puts "Could not update 'attempted' flag, will not proceed for fear of sending multiple copies" update_attributes(:ready => false, :failed => true, :failed_at => Time.now.utc, :failure_reason => "Could not update 'attempted' flag, will not proceed for fear of sending multiple copies") return false end if !valid_email_address?(email_address) puts "Invalid email address: #{email_address}" update_attributes(:ready => false, :failed => true, :failed_at => Time.now.utc, :failure_reason => "Invalid email address") return false end begin campaign.mailer.constantize.send(campaign.method.to_sym, self).deliver update_attributes(:ready => false, :delivered => true, :delivered_at => Time.now.utc) rescue Exception => e puts e. update_attributes(:ready => false, :failed => true, :failed_at => Time.now.utc, :failure_reason => "#{e.class.name}: #{e.}") return false end true end |
#generate_identifier(regenerate = false) ⇒ Object
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
# File 'app/models/email_campaign/recipient.rb', line 21 def generate_identifier(regenerate = false) return identifier if identifier && !regenerate attempts = 0 new_identifier = nil # 28^8 = 378 billion possibilities validchars = 'ABCDEFGHJKLMNPQRTUVWXY346789' while new_identifier.nil? && attempts < 10 # generate a 8 character identifier new_identifier = '' 8.times { new_identifier << validchars[rand(validchars.length)] } new_identifier = nil if self.class.count > 0 && self.class.where(:identifier => new_identifier).first attempts += 1 end self.identifier = new_identifier end |
#queue ⇒ Object
82 83 84 85 86 87 88 |
# File 'app/models/email_campaign/recipient.rb', line 82 def queue if !duplicate && !invalid_email && !unsubscribed && !failed update_attributes(:ready => true) else false end end |
#record_click(params = nil) ⇒ Object
157 158 159 160 161 162 163 164 165 166 167 |
# File 'app/models/email_campaign/recipient.rb', line 157 def record_click(params = nil) self.class.transaction do self.opened = true self.opened_at ||= Time.now.utc self.opens ||= 1 self.clicked = true self.clicked_at ||= Time.now.utc self.clicks += 1 save end end |
#record_open(params = {}) ⇒ Object
143 144 145 146 147 148 149 150 |
# File 'app/models/email_campaign/recipient.rb', line 143 def record_open(params = {}) self.class.transaction do self.opened = true self.opened_at ||= Time.now.utc self.opens += 1 save end end |
#requeue ⇒ Object
resets failed and delivered flags… use sparingly
91 92 93 94 95 |
# File 'app/models/email_campaign/recipient.rb', line 91 def requeue update_attributes(:failed => false, :failed_at => nil, :failure_reason => nil, :delivered => false, :delivered_at => nil) queue end |
#resubscribe(params = nil) ⇒ Object
188 189 190 191 192 193 194 |
# File 'app/models/email_campaign/recipient.rb', line 188 def resubscribe(params = nil) self.class.transaction do self.class.where(:email_address => email_address, :unsubscribed => true).each do |r| r.update_attributes(:unsubscribed => false) end end end |
#to_s ⇒ Object
198 199 200 |
# File 'app/models/email_campaign/recipient.rb', line 198 def to_s name.blank? ? email_address : "#{name} <#{email_address}>" end |
#unsubscribe(params = nil) ⇒ Object
174 175 176 177 178 179 180 181 |
# File 'app/models/email_campaign/recipient.rb', line 174 def unsubscribe(params = nil) self.class.transaction do self.class.where(:email_address => email_address, :unsubscribed => false).each do |r| r.update_attributes(:unsubscribed => true, :ready => false) end end save end |
#valid_email_address?(value) ⇒ Boolean
202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 |
# File 'app/models/email_campaign/recipient.rb', line 202 def valid_email_address?(value) begin m = Mail::Address.new(value) # We must check that value contains a domain and that value is an email address r = m.domain && m.address == value t = m.__send__(:tree) # We need to dig into treetop # A valid domain must have dot_atom_text elements size > 1 # user@localhost is excluded # treetop must respond to domain # We exclude valid email values like <[email protected]> # Hence we use m.__send__(tree).domain r &&= (t.domain.dot_atom_text.elements.size > 1) rescue Exception => e false end end |