Class: Gmail::Message

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

Defined Under Namespace

Classes: NoLabelError

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(mailbox, uid) ⇒ Message

Returns 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

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



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 message.respond_to?(meth)
    message.send(meth, *args, &block)
  else
    super(meth, *args, &block)
  end
end

Instance Attribute Details

#uidObject (readonly)

Returns the value of attribute uid.



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

def uid
  @uid
end

Instance Method Details

#archive!Object

Archive this message.



78
79
80
# File 'lib/gmail/message.rb', line 78

def archive!
  move_to('[Gmail]/All Mail')
end

#delete!Object

Move to trash / bin.



68
69
70
71
72
73
74
75
# File 'lib/gmail/message.rb', line 68

def delete!
  @mailbox.messages.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

#envelopeObject



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

#flag(name) ⇒ Object

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

#inspectObject



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

#label(name, from = nil) ⇒ Object

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

#label!(name, from = nil) ⇒ Object 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

#mark(flag) ⇒ Object

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

#messageObject Also known as: raw_message



156
157
158
159
160
# File 'lib/gmail/message.rb', line 156

def message
  @message ||= Mail.new(@gmail.mailbox(@mailbox.name) { 
    @gmail.conn.uid_fetch(uid, "RFC822")[0].attr["RFC822"] # RFC822
  })
end

#move_to(name, from = nil) ⇒ Object 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

#move_to!(name, from = nil) ⇒ Object 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

#read!Object

Mark as read.



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

def read!
  flag(:Seen)
end

#remove_label!(name) ⇒ Object 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

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

Returns:

  • (Boolean)


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 message.respond_to?(meth)
    return true
  else
    super(meth, *args, &block)
  end
end

#spam!Object

Mark this message as a spam.



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

def spam!
  move_to('[Gmail]/Spam')
end

#star!Object

Mark message with star.



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

def star!
  flag('[Gmail]/Starred')
end

#unflag(name) ⇒ Object

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

#unread!Object

Mark as unread.



53
54
55
# File 'lib/gmail/message.rb', line 53

def unread!
  unflag(:Seen)
end

#unstar!Object

Remove message from list of starred.



63
64
65
# File 'lib/gmail/message.rb', line 63

def unstar!
  unflag('[Gmail]/Starred')
end