Class: Whisper::EmailSender

Inherits:
Object
  • Object
show all
Includes:
Loggy
Defined in:
lib/whisper/email_sender.rb

Defined Under Namespace

Classes: Error

Instance Method Summary collapse

Constructor Details

#initialize(entryset, commentset, router, authors, entry_template, comment_template, config) ⇒ EmailSender

Returns a new instance of EmailSender.



14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/whisper/email_sender.rb', line 14

def initialize entryset, commentset, router, authors, entry_template, comment_template, config
  @entryset = entryset
  @commentset = commentset
  @authors = authors
  @router = router
  @entry_template = entry_template
  @comment_template = comment_template
  @config = config
  @hostname = Socket.gethostname
  @map = TimedMap.new
  @q = Queue.new
end

Instance Method Details

#blog_titleObject



44
# File 'lib/whisper/email_sender.rb', line 44

def blog_title; @config.title end

#done?Boolean

Returns:

  • (Boolean)


27
# File 'lib/whisper/email_sender.rb', line 27

def done?; @q.empty? end

#from_addressObject



43
# File 'lib/whisper/email_sender.rb', line 43

def from_address; @config.from_email_address end

#full_url_for(*a) ⇒ Object



45
# File 'lib/whisper/email_sender.rb', line 45

def full_url_for(*a); @config.public_url_root + @router.url_for(*a) end

#handle_send_request(id, params) ⇒ Object

Raises:



67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/whisper/email_sender.rb', line 67

def handle_send_request id, params
  email = params["email"]
  raise Error, "invalid email address" unless email =~ /^\S+@\S+$/

  if params["comment-id"]
    c = @commentset.comments_by_id[params["comment-id"]]
    raise Error, "can't find comment" unless c
    @q.push [c, email, true]
  else
    e = @entryset.entries_by_id[id]
    raise Error, "can't find entry" unless e
    @q.push [e, email, true]
  end
end

#processObject



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/whisper/email_sender.rb', line 47

def process
  thing, to_address, web_request = @q.pop

  @map.prune!
  content, timestamp = @map[thing.id]

  if timestamp.nil? || @entry_template.timestamp > timestamp || @comment_template.timestamp > timestamp
    content = case thing
      when Entry; get_content_for_entry thing, to_address, web_request
      when Comment; get_content_for_comment thing, to_address, web_request
      else raise "wtf: #{thing.inspect}"
    end
    @map[thing.id] = [content, timestamp]
  end

  debug "sending content to #{to_address} from #{from_address} via #{@config.sendmail}"
  IO.popen(@config.sendmail, "w") { |sm| sm.puts content; true }
  debug "done sending content to #{to_address}"
end

#q_sizeObject



28
# File 'lib/whisper/email_sender.rb', line 28

def q_size; @q.size end

#send_comment(comment, to_addr) ⇒ Object



82
# File 'lib/whisper/email_sender.rb', line 82

def send_comment comment, to_addr; @q.push [comment, to_addr, false] end

#start!Object



30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/whisper/email_sender.rb', line 30

def start!
  Thread.new do
    while true
      begin
        process
      rescue Exception => e
        warn e.message
        warn e.backtrace.join("\n")
      end
    end
  end
end