Class NewsletterPostman
In: lib/newsletter_postman.rb
Parent: Struct.new(:newsletter_issue_id, :title, :subject, :header_image_tag, :header, :footer, :body, :url )

Methods

perform  

Public Instance methods

[Source]

    # File lib/newsletter_postman.rb, line 2
 2:   def perform
 3:     @issue = NewsletterIssue.find(newsletter_issue_id)
 4:     @issue.newsletter.newsletter_subscriptions.each do |subscription|
 5:       if NewsletterBlacklist.find_by_mail(subscription.mail).nil?  && @issue.newsletter_deliveries.find(:all,
 6:                    :include => :newsletter_subscription, 
 7:                    :conditions => ['newsletter_subscriptions.mail = ?',subscription.mail]
 8:         ).empty? 
 9:         # only if address not blacklisted and no delivery for this issue and mail pair exists
10:         begin
11:           UserMailer.deliver_newsletter(
12:             @issue.newsletter.reply_to, 
13:             subscription.mail, 
14:             title, 
15:             subject, 
16:             header_image_tag, 
17:             header.gsub(/SUBSCRIPTION_URL/, 
18:                      "<a href='#{ROOT_URL}/subscriptions/#{encode64(subscription.mail)}/#{subscription.token}'>Your Subscriptions</a>"
19:                    ).gsub(/NEWLETTER_MAIL/, subscription.mail
20:                    ).gsub(/BLOCK_MAIL_URL/, 
21:                      "<a href='#{ROOT_URL}/subscriptions/#{encode64(subscription.mail)}/#{subscription.token}'>Your Subscriptions</a>"
22:                    ), 
23:                     
24:             footer.gsub(/SUBSCRIPTION_URL/, 
25:                      "<a href='#{ROOT_URL}/subscriptions/#{encode64(subscription.mail)}/#{subscription.token}'>Your Subscriptions</a>"
26:                    ).gsub(/NEWLETTER_MAIL/, subscription.mail
27:                    ).gsub(/BLOCK_MAIL_URL/, 
28:                      "<a href='#{ROOT_URL}/subscriptions/#{encode64(subscription.mail)}/#{subscription.token}'>Your Subscriptions</a>"
29:                    ), 
30:             body, 
31:             url
32:           )
33:           d = @issue.newsletter_deliveries.create(:newsletter_subscription_id => subscription.id, :delivered_at => Time::now() )
34:           d.save!
35:           sleep(3) # give other smpt-tasks a chance to deliver their mails ;-)
36:         rescue => e
37:           d = @issue.newsletter_deliveries.create(:newsletter_subscription_id => subscription.id, :delivered_at => Time::now(),
38:                 :failure_messages => e.to_s )
39:           d.save!
40:         end
41:       else
42:         if (bl = NewsletterBlacklist.find_by_mail(subscription.mail))
43:           subscription.destroy
44:         end
45:       end
46:     end
47:   end

[Validate]