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

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



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

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

Instance Method Details

#archive!Object



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

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

#delete!Object



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

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

#flag(flg) ⇒ Object

IMAP Operations



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

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

#inspectObject



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

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



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

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



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

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



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

def mark(flag)
  case flag
  when :read
    flag(:Seen)
  when :unread
    unflag(:Seen)
  when :deleted
    flag(:Deleted)
  when :spam
    move_to('[Gmail]/Spam')
  end ? true : false
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



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

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

#save_attachments_to(path = nil) ⇒ Object



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

def save_attachments_to(path=nil)
  attachments.each {|a| a.save_to_file(path) }
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



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

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