Class: Gmail::Message
Defined Under Namespace
Classes: NoLabelError
Instance Attribute Summary (collapse)
-
- (Object) uid
readonly
Returns the value of attribute uid.
Instance Method Summary (collapse)
-
- (Object) archive!
Archive this message.
-
- (Object) delete!
Move to trash / bin.
- - (Object) envelope
-
- (Object) flag(name)
Mark message with given flag.
-
- (Message) initialize(mailbox, uid)
constructor
A new instance of Message.
- - (Object) inspect
-
- (Object) label(name, from = nil)
Mark this message with given label.
-
- (Object) label!(name, from = nil)
(also: #add_label, #add_label!)
Mark this message with given label.
-
- (Object) mark(flag)
Do commonly used operations on message.
- - (Object) message (also: #raw_message)
- - (Object) method_missing(meth, *args, &block)
-
- (Object) move_to(name, from = nil)
(also: #move)
Move to given box and delete from others.
-
- (Object) move_to!(name, from = nil)
(also: #move!)
Move message to given and delete from others.
-
- (Object) read!
Mark as read.
-
- (Object) remove_label!(name)
(also: #delete_label!)
Remove given label from this message.
- - (Boolean) respond_to?(meth, *args, &block)
-
- (Object) spam!
Mark this message as a spam.
-
- (Object) star!
Mark message with star.
-
- (Object) unflag(name)
Unmark message.
-
- (Object) unread!
Mark as unread.
-
- (Object) unstar!
Remove message from list of starred.
Constructor Details
- (Message) initialize(mailbox, uid)
A new instance of Message
10 11 12 13 14 |
# File 'lib/gmail/message.rb', line 10 def initialize(mailbox, uid) @uid = uid @mailbox = mailbox @gmail = mailbox.instance_variable_get("@gmail") if mailbox end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
- (Object) method_missing(meth, *args, &block)
129 130 131 132 133 134 135 136 137 138 |
# File 'lib/gmail/message.rb', line 129 def method_missing(meth, *args, &block) # Delegate rest directly to the message. if envelope.respond_to?(meth) envelope.send(meth, *args, &block) elsif .respond_to?(meth) .send(meth, *args, &block) else super(meth, *args, &block) end end |
Instance Attribute Details
- (Object) uid (readonly)
Returns the value of attribute uid
8 9 10 |
# File 'lib/gmail/message.rb', line 8 def uid @uid ||= @gmail.conn.uid_search(['HEADER', 'Message-ID', ])[0] end |
Instance Method Details
- (Object) archive!
Archive this message.
78 79 80 |
# File 'lib/gmail/message.rb', line 78 def archive! move_to('[Gmail]/All Mail') end |
- (Object) delete!
Move to trash / bin.
68 69 70 71 72 73 74 75 |
# File 'lib/gmail/message.rb', line 68 def delete! @mailbox..delete(uid) flag(:deleted) # For some, it's called "Trash", for others, it's called "Bin". Support both. trash = @gmail.labels.exist?('[Gmail]/Bin') ? '[Gmail]/Bin' : '[Gmail]/Trash' move_to(trash) unless %w[[Gmail]/Spam [Gmail]/Bin [Gmail]/Trash].include?(@mailbox.name) end |
- (Object) envelope
150 151 152 153 154 |
# File 'lib/gmail/message.rb', line 150 def envelope @envelope ||= @gmail.mailbox(@mailbox.name) { @gmail.conn.uid_fetch(uid, "ENVELOPE")[0].attr["ENVELOPE"] } end |
- (Object) flag(name)
Mark message with given flag.
21 22 23 |
# File 'lib/gmail/message.rb', line 21 def flag(name) !!@gmail.mailbox(@mailbox.name) { @gmail.conn.uid_store(uid, "+FLAGS", [name]) } end |
- (Object) inspect
125 126 127 |
# File 'lib/gmail/message.rb', line 125 def inspect "#<Gmail::Message#{'0x%04x' % (object_id << 1)} mailbox=#{@mailbox.external_name}#{' uid='+@uid.to_s if @uid}#{' message_id='+@message_id.to_s if @message_id}>" end |
- (Object) label(name, from = nil)
Mark this message with given label. When given label doesn't exist then it will raise NoLabelError.
See also Gmail::Message#label!.
100 101 102 103 104 |
# File 'lib/gmail/message.rb', line 100 def label(name, from=nil) @gmail.mailbox(Net::IMAP.encode_utf7(from || @mailbox.external_name)) { @gmail.conn.uid_copy(uid, Net::IMAP.encode_utf7(name)) } rescue Net::IMAP::NoResponseError raise NoLabelError, "Label '#{name}' doesn't exist!" end |
- (Object) label!(name, from = nil) Also known as: add_label, add_label!
Mark this message with given label. When given label doesn't exist then it will be automaticaly created.
See also Gmail::Message#label.
110 111 112 113 114 115 |
# File 'lib/gmail/message.rb', line 110 def label!(name, from=nil) label(name, from) rescue NoLabelError @gmail.labels.add(Net::IMAP.encode_utf7(name)) label(name, from) end |
- (Object) mark(flag)
Do commonly used operations on message.
31 32 33 34 35 36 37 38 39 40 |
# File 'lib/gmail/message.rb', line 31 def mark(flag) case flag when :read then read! when :unread then unread! when :deleted then delete! when :spam then spam! else flag(flag) end end |
- (Object) message Also known as: raw_message
156 157 158 159 160 |
# File 'lib/gmail/message.rb', line 156 def @message ||= Mail.new(@gmail.mailbox(@mailbox.name) { @gmail.conn.uid_fetch(uid, "RFC822")[0].attr["RFC822"] # RFC822 }) end |
- (Object) move_to(name, from = nil) Also known as: move
Move to given box and delete from others.
83 84 85 86 |
# File 'lib/gmail/message.rb', line 83 def move_to(name, from=nil) label(name, from) delete! if !%w[[Gmail]/Bin [Gmail]/Trash].include?(name) end |
- (Object) move_to!(name, from = nil) Also known as: move!
Move message to given and delete from others. When given mailbox doesn't exist then it will be automaticaly created.
91 92 93 |
# File 'lib/gmail/message.rb', line 91 def move_to!(name, from=nil) label!(name, from) && delete! end |
- (Object) read!
Mark as read.
48 49 50 |
# File 'lib/gmail/message.rb', line 48 def read! flag(:Seen) end |
- (Object) remove_label!(name) Also known as: delete_label!
Remove given label from this message.
120 121 122 |
# File 'lib/gmail/message.rb', line 120 def remove_label!(name) move_to('[Gmail]/All Mail', name) end |
- (Boolean) respond_to?(meth, *args, &block)
140 141 142 143 144 145 146 147 148 |
# File 'lib/gmail/message.rb', line 140 def respond_to?(meth, *args, &block) if envelope.respond_to?(meth) return true elsif .respond_to?(meth) return true else super(meth, *args, &block) end end |
- (Object) spam!
Mark this message as a spam.
43 44 45 |
# File 'lib/gmail/message.rb', line 43 def spam! move_to('[Gmail]/Spam') end |
- (Object) star!
Mark message with star.
58 59 60 |
# File 'lib/gmail/message.rb', line 58 def star! flag('[Gmail]/Starred') end |
- (Object) unflag(name)
Unmark message.
26 27 28 |
# File 'lib/gmail/message.rb', line 26 def unflag(name) !!@gmail.mailbox(@mailbox.name) { @gmail.conn.uid_store(uid, "-FLAGS", [name]) } end |
- (Object) unread!
Mark as unread.
53 54 55 |
# File 'lib/gmail/message.rb', line 53 def unread! unflag(:Seen) end |
- (Object) unstar!
Remove message from list of starred.
63 64 65 |
# File 'lib/gmail/message.rb', line 63 def unstar! unflag('[Gmail]/Starred') end |