Class: Animoto::GMail
- Inherits:
-
Object
- Object
- Animoto::GMail
- Defined in:
- lib/gmail.rb
Constant Summary collapse
- @@logger =
nil
Instance Attribute Summary collapse
-
#imap ⇒ Object
Returns the value of attribute imap.
Class Method Summary collapse
Instance Method Summary collapse
-
#initialize(login, password, mailbox = "[Gmail]/All Mail") ⇒ GMail
constructor
connects to gmail server and selects the global mailbox.
- #logger ⇒ Object
-
#search(options) ⇒ Object
clean interface for searching an imap mailbox accepts a conditions hash with search options.
Constructor Details
#initialize(login, password, mailbox = "[Gmail]/All Mail") ⇒ GMail
connects to gmail server and selects the global mailbox
30 31 32 33 34 35 36 37 38 39 40 |
# File 'lib/gmail.rb', line 30 def initialize(login, password, mailbox = "[Gmail]/All Mail") @messages = [] @messages_mutex = Mutex.new # connect to gmail server @imap = Net::IMAP.new('imap.gmail.com', 993, true) @imap.login(login, password) # select the mailbox with "all" messages @imap.select(mailbox) # tag to select end |
Instance Attribute Details
#imap ⇒ Object
Returns the value of attribute imap.
18 19 20 |
# File 'lib/gmail.rb', line 18 def imap @imap end |
Class Method Details
.logger=(l) ⇒ Object
21 22 23 |
# File 'lib/gmail.rb', line 21 def self.logger=(l) @@logger = l end |
Instance Method Details
#logger ⇒ Object
25 26 27 |
# File 'lib/gmail.rb', line 25 def logger @@logger end |
#search(options) ⇒ Object
clean interface for searching an imap mailbox accepts a conditions hash with search options
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 |
# File 'lib/gmail.rb', line 44 def search() conditions = .delete(:conditions) || {} mailbox = conditions.delete(:label) @imap.select( mailbox ) if mailbox # allow user to pass in custom clause custom = conditions.delete(:custom) || [] # validate search flags # http://tools.ietf.org/html/rfc3501#section-6.4.4 valid_flags = %w(before body cc from label new not or on since subject to) if bad_flag = conditions.keys.detect{ |flag| !valid_flags.include?(flag.to_s) } raise ArgumentError, "Invalid search flag '#{bad_flag}'" end # imap requires search params to be in a flattened array # e.g. ['TO', '[email protected]', 'BEFORE', Time.now] keys = [ *conditions.map{|k,v| [k.to_s.upcase, v] }.flatten ] + custom logger.info { "Executing IMAP search with #{keys.join(',')}" } msg_ids = @imap.search(keys) logger.info { "Retrieved #{msg_ids.size}" } msg_ids = msg_ids[0, [:limit].to_i] unless [:limit].nil? (msg_ids, !![:full_message]) end |