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.



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

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

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

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

Delegate all other methods to the Mail message



102
103
104
105
106
107
108
# File 'lib/gmail/message.rb', line 102

def method_missing(*args, &block)
  if block_given?
    message.send(*args, &block)
  else
    message.send(*args)
  end
end

Instance Method Details

#archive!Object



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

def archive!
  move_to_special_folder(:All)
end

#delete!Object



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

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

#flag(flg) ⇒ Object

IMAP Operations



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

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

#inspectObject



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

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

#label(name) ⇒ Object



50
51
52
53
54
55
56
57
58
# File 'lib/gmail/message.rb', line 50

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



60
61
62
63
64
65
66
67
68
69
70
# File 'lib/gmail/message.rb', line 60

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



32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/gmail/message.rb', line 32

def mark(flag)
  case flag
  when :read
    flag(:Seen)
  when :unread
    unflag(:Seen)
  when :deleted
    flag(:Deleted)
  when :spam
    move_to_special_folder(:Junk)
  end ? true : false
end

#messageObject

Parsed MIME message object



91
92
93
94
95
96
97
# File 'lib/gmail/message.rb', line 91

def message
  require 'mail'
  request,part = 'RFC822','RFC822'
  request,part = 'BODY.PEEK[]','BODY[]' if @gmail.peek
  _body = @gmail.in_mailbox(@mailbox) { @gmail.imap.uid_fetch(uid, request)[0].attr[part] }
  @message ||= Mail.new(_body)
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



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

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

#move_to_special_folder(attribute) ⇒ Object



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

def move_to_special_folder(attribute)
  name = @gmail.find_label_by_attribute(attribute).name
  move_to(name)
end

#uidObject

Auto IMAP info



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

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

#unflag(flg) ⇒ Object



25
26
27
28
29
# File 'lib/gmail/message.rb', line 25

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