Class: Gmail::Mailbox
Constant Summary collapse
- MAILBOX_ALIASES =
{ :all => ['ALL'], :seen => ['SEEN'], :unseen => ['UNSEEN'], :read => ['SEEN'], :unread => ['UNSEEN'], :flagged => ['FLAGGED'], :unflagged => ['UNFLAGGED'], :starred => ['FLAGGED'], :unstarred => ['UNFLAGGED'], :deleted => ['DELETED'], :undeleted => ['UNDELETED'], :draft => ['DRAFT'], :undrafted => ['UNDRAFT'] }
Instance Attribute Summary collapse
-
#external_name ⇒ Object
readonly
Returns the value of attribute external_name.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
Instance Method Summary collapse
-
#count(*args) ⇒ Object
This is a convenience method that really probably shouldn’t need to exist, but it does make code more readable, if seriously all you want is the count of messages.
-
#emails(*args, &block) ⇒ Object
(also: #mails, #search, #find, #filter)
Returns list of emails which meets given criteria.
-
#expunge ⇒ Object
This permanently removes messages which are marked as deleted.
-
#initialize(gmail, name = "INBOX") ⇒ Mailbox
constructor
A new instance of Mailbox.
- #inspect ⇒ Object
-
#messages ⇒ Object
Cached messages.
- #to_s ⇒ Object
Constructor Details
#initialize(gmail, name = "INBOX") ⇒ Mailbox
Returns a new instance of Mailbox.
22 23 24 25 26 |
# File 'lib/gmail/mailbox.rb', line 22 def initialize(gmail, name="INBOX") @name = name @external_name = Net::IMAP.decode_utf7(name) @gmail = gmail end |
Instance Attribute Details
#external_name ⇒ Object (readonly)
Returns the value of attribute external_name.
20 21 22 |
# File 'lib/gmail/mailbox.rb', line 20 def external_name @external_name end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
19 20 21 |
# File 'lib/gmail/mailbox.rb', line 19 def name @name end |
Instance Method Details
#count(*args) ⇒ Object
This is a convenience method that really probably shouldn’t need to exist, but it does make code more readable, if seriously all you want is the count of messages.
Examples
gmail.inbox.count(:all)
gmail.inbox.count(:unread, :from => "[email protected]")
gmail.mailbox("Test").count(:all, :after => Time.now-(20*24*3600))
87 88 89 |
# File 'lib/gmail/mailbox.rb', line 87 def count(*args) emails(*args).size end |
#emails(*args, &block) ⇒ Object Also known as: mails, search, find, filter
Returns list of emails which meets given criteria.
Examples
gmail.inbox.emails(:all)
gmail.inbox.emails(:unread, :from => "[email protected]")
gmail.inbox.emails(:all, :after => Time.now-(20*24*3600))
gmail.mailbox("Test").emails(:read)
gmail.mailbox("Test") do |box|
box.emails(:read)
box.emails(:unread) do |email|
... do something with each email...
end
end
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 |
# File 'lib/gmail/mailbox.rb', line 43 def emails(*args, &block) args << :all if args.size == 0 if args.first.is_a?(Symbol) search = MAILBOX_ALIASES[args.shift].dup opts = args.first.is_a?(Hash) ? args.first : {} opts[:after] and search.concat ['SINCE', opts[:after].to_imap_date] opts[:before] and search.concat ['BEFORE', opts[:before].to_imap_date] opts[:on] and search.concat ['ON', opts[:on].to_imap_date] opts[:from] and search.concat ['FROM', opts[:from]] opts[:to] and search.concat ['TO', opts[:to]] opts[:subject] and search.concat ['SUBJECT', opts[:subject]] opts[:label] and search.concat ['LABEL', opts[:label]] opts[:attachment] and search.concat ['HAS', 'attachment'] opts[:search] and search.concat [opts[:search]] @gmail.mailbox(name) do @gmail.conn.uid_search(search).collect do |uid| = ([uid] ||= Message.new(self, uid)) block.call() if block_given? end end elsif args.first.is_a?(Hash) emails(:all, args.first) else raise ArgumentError, "Invalid search criteria" end end |
#expunge ⇒ Object
This permanently removes messages which are marked as deleted
92 93 94 |
# File 'lib/gmail/mailbox.rb', line 92 def expunge @gmail.mailbox(name) { @gmail.conn.expunge } end |
#inspect ⇒ Object
101 102 103 |
# File 'lib/gmail/mailbox.rb', line 101 def inspect "#<Gmail::Mailbox#{'0x%04x' % (object_id << 1)} name=#{external_name}>" end |
#messages ⇒ Object
Cached messages.
97 98 99 |
# File 'lib/gmail/mailbox.rb', line 97 def @messages ||= {} end |
#to_s ⇒ Object
105 106 107 |
# File 'lib/gmail/mailbox.rb', line 105 def to_s name end |