Class: Gmail::Message

Inherits:
Object show all
Defined in:
lib/gmail/message.rb

Defined Under Namespace

Classes: NoLabelError

Constant Summary collapse

PREFETCH_ATTRS =
["UID", "ENVELOPE", "BODY.PEEK[]", "FLAGS", "X-GM-LABELS", "X-GM-MSGID", "X-GM-THRID"]

Instance Method Summary collapse

Constructor Details

#initialize(mailbox, uid, _attrs = nil) ⇒ Message


8
9
10
11
12
13
# File 'lib/gmail/message.rb', line 8

def initialize(mailbox, uid, _attrs = nil)
  @uid     = uid
  @mailbox = mailbox
  @gmail   = mailbox.instance_variable_get("@gmail") if mailbox # UGLY
  @_attrs  = _attrs
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(meth, *args, &block) ⇒ Object


165
166
167
168
169
170
171
172
173
174
# File 'lib/gmail/message.rb', line 165

def method_missing(meth, *args, &block)
  # Delegate rest directly to the message.
  if envelope.respond_to?(meth)
    envelope.send(meth, *args, &block)
  elsif message.respond_to?(meth)
    message.send(meth, *args, &block)
  else
    super(meth, *args, &block)
  end
end

Instance Method Details

#add_label(name) ⇒ Object Also known as: label, label!, add_label!

Use Gmail IMAP Extensions to add a Label to an email


142
143
144
145
146
147
# File 'lib/gmail/message.rb', line 142

def add_label(name)
  @gmail.mailbox(@mailbox.name) do
    @gmail.conn.uid_store(uid, "+X-GM-LABELS", [Net::IMAP.encode_utf7(name.to_s)])
    clear_cached_attributes
  end
end

#archive!Object

Archiving is done by adding the `Trash` label. To undo this, you just re-apply the `Inbox` label (see `#unarchive!`)


118
119
120
# File 'lib/gmail/message.rb', line 118

def archive!
  remove_label("\\Inbox")
end

#delete!Object

Deleting is done by adding the `Trash` label. To undo this, you just re-apply the `Inbox` label (see `#undelete!`)


112
113
114
# File 'lib/gmail/message.rb', line 112

def delete!
  add_label("\\Trash")
end

#envelopeObject


29
30
31
# File 'lib/gmail/message.rb', line 29

def envelope
  @envelope ||= fetch("ENVELOPE")
end

#flag(name) ⇒ Object

Mark message with given flag.


47
48
49
50
51
52
# File 'lib/gmail/message.rb', line 47

def flag(name)
  !!@gmail.mailbox(@mailbox.name) do
    @gmail.conn.uid_store(uid, "+FLAGS", [name])
    clear_cached_attributes
  end
end

#flagsObject


38
39
40
# File 'lib/gmail/message.rb', line 38

def flags
  @flags ||= fetch("FLAGS")
end

#inspectObject


161
162
163
# File 'lib/gmail/message.rb', line 161

def inspect
  "#<Gmail::Message#{'0x%04x' % (object_id << 1)} mailbox=#{@mailbox.name}#{' uid=' + @uid.to_s if @uid}#{' message_id=' + @msg_id.to_s if @msg_id}>"
end

#labelsObject


42
43
44
# File 'lib/gmail/message.rb', line 42

def labels
  @labels ||= fetch("X-GM-LABELS")
end

#mark(flag) ⇒ Object

Do commonly used operations on message.


63
64
65
66
67
68
69
70
71
72
# File 'lib/gmail/message.rb', line 63

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

#messageObject Also known as: raw_message


33
34
35
# File 'lib/gmail/message.rb', line 33

def message
  @message ||= Mail.new(fetch("BODY[]"))
end

#move_to(name, from = nil) ⇒ Object Also known as: move, move!, move_to!

Move to given box and delete from others. Apply a given label and optionally remove one. TODO: We should probably deprecate this method. It doesn't really add a lot

of value, especially since the concept of "moving" a message from one
label to another doesn't totally make sense in the Gmail world.

133
134
135
136
# File 'lib/gmail/message.rb', line 133

def move_to(name, from = nil)
  add_label(name)
  remove_label(from) if from
end

#msg_idObject Also known as: message_id


19
20
21
# File 'lib/gmail/message.rb', line 19

def msg_id
  @msg_id ||= fetch("X-GM-MSGID")
end

#read!Object

Mark as read.


80
81
82
# File 'lib/gmail/message.rb', line 80

def read!
  flag(:Seen)
end

#read?Boolean

Check whether message is read


75
76
77
# File 'lib/gmail/message.rb', line 75

def read?
  flags.include?(:Seen)
end

#remove_label(name) ⇒ Object Also known as: remove_label!

Use Gmail IMAP Extensions to remove a Label from an email


153
154
155
156
157
158
# File 'lib/gmail/message.rb', line 153

def remove_label(name)
  @gmail.mailbox(@mailbox.name) do
    @gmail.conn.uid_store(uid, "-X-GM-LABELS", [Net::IMAP.encode_utf7(name.to_s)])
    clear_cached_attributes
  end
end

#respond_to?(meth, *args, &block) ⇒ Boolean


176
177
178
179
180
181
182
183
184
# File 'lib/gmail/message.rb', line 176

def respond_to?(meth, *args, &block)
  if envelope.respond_to?(meth)
    return true
  elsif message.respond_to?(meth)
    return true
  else
    super(meth, *args, &block)
  end
end

#spam!Object

Marking as spam is done by adding the `Spam` label. To undo this, you just re-apply the `Inbox` label (see `#unspam!`)


106
107
108
# File 'lib/gmail/message.rb', line 106

def spam!
  add_label("\\Spam")
end

#star!Object

Mark message with star.


95
96
97
# File 'lib/gmail/message.rb', line 95

def star!
  flag(:Flagged)
end

#starred?Boolean

Check whether message is starred


90
91
92
# File 'lib/gmail/message.rb', line 90

def starred?
  flags.include?(:Flagged)
end

#thr_idObject Also known as: thread_id


24
25
26
# File 'lib/gmail/message.rb', line 24

def thr_id
  @thr_id ||= fetch("X-GM-THRID")
end

#uidObject


15
16
17
# File 'lib/gmail/message.rb', line 15

def uid
  @uid ||= fetch("UID")
end

#unarchive!Object Also known as: unspam!, undelete!


122
123
124
# File 'lib/gmail/message.rb', line 122

def unarchive!
  add_label("\\Inbox")
end

#unflag(name) ⇒ Object

Unmark message.


55
56
57
58
59
60
# File 'lib/gmail/message.rb', line 55

def unflag(name)
  !!@gmail.mailbox(@mailbox.name) do
    @gmail.conn.uid_store(uid, "-FLAGS", [name])
    clear_cached_attributes
  end
end

#unread!Object

Mark as unread.


85
86
87
# File 'lib/gmail/message.rb', line 85

def unread!
  unflag(:Seen)
end

#unstar!Object

Remove message from list of starred.


100
101
102
# File 'lib/gmail/message.rb', line 100

def unstar!
  unflag(:Flagged)
end