Class: Gmail::Message

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

Instance Method Summary collapse

Constructor Details

#initialize(gmail, mailbox, uid) ⇒ Message

Returns a new instance of Message.



4
5
6
7
8
# File 'lib/gmail/message.rb', line 4

def initialize(gmail, mailbox, uid)
  @gmail = gmail
  @mailbox = mailbox
  @uid = uid
end

Instance Method Details

#archive!Object



102
103
104
# File 'lib/gmail/message.rb', line 102

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

#bodyObject



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

def body
  @body ||= @gmail.in_mailbox(@mailbox) do
    @gmail.imap.uid_fetch(uid, "RFC822")[0].attr["RFC822"]
  end
end

#delete!Object



66
67
68
69
# File 'lib/gmail/message.rb', line 66

def delete!
  @mailbox.messages.delete(uid)
  flag(:Deleted)
end

#flag(flg) ⇒ Object

IMAP Operations



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

def flag(flg)
  @gmail.in_mailbox(@mailbox) do
    @gmail.imap.uid_store(uid, "+FLAGS", [flg])
  end
end

#inspectObject



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

def inspect
  "<#Message:#{object_id} mailbox=#{@mailbox.name}#{' uid='[email protected]_s if @uid}#{' message_id='+@message_id.to_s if @message_id}>"
end

#label(name) ⇒ Object



71
72
73
74
75
76
77
78
79
# File 'lib/gmail/message.rb', line 71

def label(name)
  @gmail.in_mailbox(@mailbox) do
    begin
      @gmail.imap.uid_copy(uid, name)
    rescue Net::IMAP::NoResponseError
      raise Gmail::NoLabel, "No label `#{name}' exists!"
    end
  end
end

#label!(name) ⇒ Object



81
82
83
84
85
86
87
88
89
90
91
# File 'lib/gmail/message.rb', line 81

def label!(name)
  @gmail.in_mailbox(@mailbox) do
    begin
      @gmail.imap.uid_copy(uid, name)
    rescue Net::IMAP::NoResponseError
      # need to create the label first
      @gmail.create_label(name)
      retry
    end
  end
end

#mark(flag) ⇒ Object

Gmail Operations



53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/gmail/message.rb', line 53

def mark(flag)
  case flag
  when :read
    flag(:Seen)
  when :unread
    unflag(:Seen)
  when :deleted
    flag(:Deleted)
  when :spam
    move_to('[Gmail]/Spam')
  end
end

#messageObject

Parsed MIME message object



35
36
37
# File 'lib/gmail/message.rb', line 35

def message
  @message ||= MIME::Message.new(body)
end

#message_idObject



19
20
21
22
23
24
25
26
# File 'lib/gmail/message.rb', line 19

def message_id
  @message_id || begin
    @gmail.in_mailbox(@mailbox) do
      @message_id = @gmail.imap.uid_fetch(@uid, ['ENVELOPE'])[0].attr['ENVELOPE'].message_id
    end
  end
  @message_id
end

#move_to(name) ⇒ Object

We’re not sure of any ‘labels’ except the ‘mailbox’ we’re in at the moment. Research whether we can find flags that tell which other labels this email is a part of. def remove_label(name) end



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

def move_to(name)
  label(name) && delete!
end

#uidObject

Auto IMAP info



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

def uid
  @uid ||= @gmail.imap.uid_search(['HEADER', 'Message-ID', message_id])[0]
end

#unflag(flg) ⇒ Object



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

def unflag(flg)
  @gmail.in_mailbox(@mailbox) do
    @gmail.imap.uid_store(uid, "-FLAGS", [flg])
  end
end