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



125
126
127
128
129
130
131
132
133
134
# File 'lib/gmail/message.rb', line 125

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.



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

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

#delete!Object

Move to trash.



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

def delete!
  @mailbox.messages.delete(uid)
  flag(:Deleted)
  move_to('[Gmail]/Trash') unless %w[[Gmail]/Spam [Gmail]/Trash].include?(@mailbox.name)
end

#envelopeObject



136
137
138
139
140
# File 'lib/gmail/message.rb', line 136

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



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

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

#label(name, from = nil) ⇒ Object Also known as: add_label

Mark this message with given label. When given label doesn't exist then it will raise NoLabelError.

See also Gmail::Message#label!.



96
97
98
99
100
# File 'lib/gmail/message.rb', line 96

def label(name, from=nil)
  @gmail.mailbox(from || @mailbox.name) { @gmail.conn.uid_copy(uid, name) }
rescue Net::IMAP::NoResponseError
  raise NoLabelError, "Label '#{name}' doesn't exist!"
end

#label!(name, from = nil) ⇒ Object

Mark this message with given label. When given label doesn't exist then it will be automaticaly created.

See also Gmail::Message#label.



107
108
109
110
111
112
# File 'lib/gmail/message.rb', line 107

def label!(name, from=nil)
  label(name, from) 
rescue NoLabelError
  @gmail.labels.add(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

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

Move to given box and delete from others.



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

def move_to(name, from=nil)
  label(name, from) && delete!
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.



87
88
89
# File 'lib/gmail/message.rb', line 87

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.



116
117
118
# File 'lib/gmail/message.rb', line 116

def remove_label!(name)
  move_to('[Gmail]/All Mail', name)
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