Class: MailRoom::MailboxWatcher
- Inherits:
-
Object
- Object
- MailRoom::MailboxWatcher
- Defined in:
- lib/mail_room/mailbox_watcher.rb
Overview
Watch a Mailbox
Instance Attribute Summary collapse
-
#idling_thread ⇒ Object
Returns the value of attribute idling_thread.
Instance Method Summary collapse
-
#disconnected? ⇒ Boolean
is the imap connection closed?.
-
#handler ⇒ Object
build a handler to process mailbox messages.
-
#idle ⇒ Object
maintain an imap idle connection.
-
#idling? ⇒ Boolean
is the connection blocked idling?.
-
#imap ⇒ Object
build a net/imap connection to google imap.
-
#initialize(mailbox) ⇒ MailboxWatcher
constructor
Watch a new mailbox.
-
#log_in ⇒ Object
send the imap login command to google.
-
#logged_in? ⇒ Boolean
is the connection logged in?.
-
#message_exists?(response) ⇒ Boolean
is the response for a new message?.
-
#process_mailbox ⇒ Object
trigger the handler to process this mailbox for new messages.
-
#quit ⇒ Object
stop running.
-
#ready_to_idle? ⇒ Boolean
is the connection ready to idle?.
-
#reset ⇒ Object
clear disconnected imap reset imap state.
-
#run ⇒ Object
run the mailbox watcher.
-
#running? ⇒ Boolean
are we running?.
-
#set_mailbox ⇒ Object
select the mailbox name we want to use.
-
#setup ⇒ Object
log in and set the mailbox.
-
#stop_idling ⇒ Object
trigger the idle to finish and wait for the thread to finish.
Constructor Details
#initialize(mailbox) ⇒ MailboxWatcher
Watch a new mailbox
11 12 13 14 15 16 |
# File 'lib/mail_room/mailbox_watcher.rb', line 11 def initialize(mailbox) @mailbox = mailbox reset @running = false end |
Instance Attribute Details
#idling_thread ⇒ Object
Returns the value of attribute idling_thread.
7 8 9 |
# File 'lib/mail_room/mailbox_watcher.rb', line 7 def idling_thread @idling_thread end |
Instance Method Details
#disconnected? ⇒ Boolean
is the imap connection closed?
48 49 50 |
# File 'lib/mail_room/mailbox_watcher.rb', line 48 def disconnected? @imap.disconnected? end |
#handler ⇒ Object
build a handler to process mailbox messages
24 25 26 |
# File 'lib/mail_room/mailbox_watcher.rb', line 24 def handler @handler ||= MailboxHandler.new(@mailbox, imap) end |
#idle ⇒ Object
maintain an imap idle connection
92 93 94 95 96 97 98 99 100 |
# File 'lib/mail_room/mailbox_watcher.rb', line 92 def idle return unless ready_to_idle? @idling = true imap.idle(&idle_handler) ensure @idling = false end |
#idling? ⇒ Boolean
is the connection blocked idling?
42 43 44 |
# File 'lib/mail_room/mailbox_watcher.rb', line 42 def idling? @idling end |
#imap ⇒ Object
build a net/imap connection to google imap
19 20 21 |
# File 'lib/mail_room/mailbox_watcher.rb', line 19 def imap @imap ||= Net::IMAP.new(@mailbox.host, :port => @mailbox.port, :ssl => @mailbox.ssl) end |
#log_in ⇒ Object
send the imap login command to google
81 82 83 84 |
# File 'lib/mail_room/mailbox_watcher.rb', line 81 def log_in imap.login(@mailbox.email, @mailbox.password) @logged_in = true end |
#logged_in? ⇒ Boolean
is the connection logged in?
36 37 38 |
# File 'lib/mail_room/mailbox_watcher.rb', line 36 def logged_in? @logged_in end |
#message_exists?(response) ⇒ Boolean
is the response for a new message?
61 62 63 |
# File 'lib/mail_room/mailbox_watcher.rb', line 61 def (response) response.respond_to?(:name) && response.name == 'EXISTS' end |
#process_mailbox ⇒ Object
trigger the handler to process this mailbox for new messages
145 146 147 |
# File 'lib/mail_room/mailbox_watcher.rb', line 145 def process_mailbox handler.process end |
#quit ⇒ Object
stop running
138 139 140 141 142 |
# File 'lib/mail_room/mailbox_watcher.rb', line 138 def quit @running = false stop_idling # disconnect end |
#ready_to_idle? ⇒ Boolean
is the connection ready to idle?
54 55 56 |
# File 'lib/mail_room/mailbox_watcher.rb', line 54 def ready_to_idle? logged_in? && !idling? end |
#reset ⇒ Object
clear disconnected imap reset imap state
74 75 76 77 78 |
# File 'lib/mail_room/mailbox_watcher.rb', line 74 def reset @imap = nil @logged_in = false @idling = false end |
#run ⇒ Object
run the mailbox watcher
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 |
# File 'lib/mail_room/mailbox_watcher.rb', line 111 def run setup @running = true # prefetch messages before first idle process_mailbox self.idling_thread = Thread.start do while(running?) do begin # block until we stop idling idle # when new messages are ready process_mailbox rescue Net::IMAP::Error, IOError => e # we've been disconnected, so re-setup setup end end end idling_thread.abort_on_exception = true end |
#running? ⇒ Boolean
are we running?
30 31 32 |
# File 'lib/mail_room/mailbox_watcher.rb', line 30 def running? @running end |
#set_mailbox ⇒ Object
select the mailbox name we want to use
87 88 89 |
# File 'lib/mail_room/mailbox_watcher.rb', line 87 def set_mailbox imap.select(@mailbox.name) if logged_in? end |
#setup ⇒ Object
log in and set the mailbox
66 67 68 69 70 |
# File 'lib/mail_room/mailbox_watcher.rb', line 66 def setup reset log_in set_mailbox end |
#stop_idling ⇒ Object
trigger the idle to finish and wait for the thread to finish
103 104 105 106 107 108 |
# File 'lib/mail_room/mailbox_watcher.rb', line 103 def stop_idling return unless idling? imap.idle_done idling_thread.join end |