Class: Gmail::Message

Inherits:
APIResource show all
Includes:
Base::Delete, Base::Get, Base::List, Base::Modify, Base::Trash
Defined in:
lib/gmail/message.rb

Instance Method Summary collapse

Methods included from Base::Trash

included, #trash, #untrash

Methods included from Base::Modify

#archive, #archive!, #mark_as_read, #mark_as_read!, #mark_as_unread, #mark_as_unread!, #modify, #modify!, #star, #star!, #unarchive, #unarchive!, #unstar, #unstar!

Methods included from Base::Get

included

Methods included from Base::Delete

#delete, included

Methods included from Base::List

included

Methods inherited from APIResource

base_method, class_name

Methods inherited from GmailObject

#[], #[]=, #as_json, #detailed, #initialize, #inspect, #refresh, #to_hash, #to_json, #to_s, #values

Constructor Details

This class inherits a constructor from Gmail::GmailObject

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class Gmail::GmailObject

Instance Method Details

#create_draftObject



18
19
20
# File 'lib/gmail/message.rb', line 18

def create_draft
  Draft.create(message: msg_parameters)
end

#deliverObject



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

def deliver
  response = Gmail.request(self.class.base_method.to_h['gmail.users.messages.send'],{}, msg_parameters)
  Message.get(response[:id])
end

#deliver!Object



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

def deliver!
  response = Gmail.request(self.class.base_method.to_h['gmail.users.messages.send'],{}, msg_parameters)
  @values = Message.get(response[:id]).values
  self
end

#forward_with(msg) ⇒ Object



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/gmail/message.rb', line 57

def forward_with msg
  # save headers that need to be override by users compared to a classic reply
  x_cc = msg.cc
  x_to = msg.to
  x_bcc = msg.bcc
  x_subject = msg.subject || subject #if user doesn't override keep classic behavior
  # set headers as for reply
  msg = set_headers_for_reply msg
  # quote message
  msg = quote_in msg
  # reset saved overridden headers
  msg.cc = x_cc
  msg.to = x_to
  msg.bcc = x_bcc
  msg.subject = x_subject
  msg
end

#inbox?Boolean

Returns:

  • (Boolean)


89
90
91
# File 'lib/gmail/message.rb', line 89

def inbox?
  (labelIds||[]).include?("INBOX")
end

#insertObject



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

def insert
  response = Gmail.request(self.class.base_method.insert,{}, msg_parameters)
  Message.get(response[:id])
end

#insert!Object



38
39
40
41
42
# File 'lib/gmail/message.rb', line 38

def insert!
  response = Gmail.request(self.class.base_method.insert,{}, msg_parameters)
  @values = Message.get(response[:id]).values
  self
end

#rawObject

is not in private because the method is used in Draft



95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
# File 'lib/gmail/message.rb', line 95

def raw # is not in private because the method is used in Draft
  if super #check if raw is set to allow fully custom message to be sent
    super
  else
    s = self
    msg = Mail.new
    msg.subject = subject
    if body
      msg.body = body
    end
    msg.from = from
    msg.to   = to
    msg.cc = cc
    msg.header['X-Bcc'] = bcc unless bcc.nil?#because Mail gem doesn't allow bcc headers...
    msg.in_reply_to = in_reply_to  unless in_reply_to.nil?
    msg.references = references unless references.nil?
    if text || html
      bodypart = Mail::Part.new
      if text
        bodypart.text_part = Mail::Part.new do |p|
          content_type 'text/plain; charset=UTF-8'
          p.body s.text
        end
      end
      if html
        bodypart.html_part = Mail::Part.new do |p|
          content_type 'text/html; charset=UTF-8'
          p.body s.html
        end
      end
      msg.add_part bodypart
    end
    if attachments
      if attachments.is_a?(Hash)
        attachments.each do |name, attachment|
          msg.add_file filename: name, content: attachment
        end
      elsif attachments.is_a?(Array)
        attachments.each do |attachment|
          msg.add_file(attachment)
        end
      end
    end
    Base64.urlsafe_encode64 msg.to_s.sub("X-Bcc", "Bcc") #because Mail gem doesn't allow bcc headers...
  end
end

#reply_all_with(msg) ⇒ Object



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

def reply_all_with msg
  msg = set_headers_for_reply msg
  msg = quote_in msg
  msg
end

#reply_sender_with(msg) ⇒ Object



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

def reply_sender_with msg
  msg = set_headers_for_reply msg
  msg = quote_in msg
  msg.cc = nil
  msg
end

#sent?Boolean

Returns:

  • (Boolean)


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

def sent?
  (labelIds||[]).include?("SENT")
end

#threadObject



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

def thread
  Thread.get(threadId)
end

#thread_idObject



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

def thread_id
  threadId
end

#unread?Boolean

Returns:

  • (Boolean)


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

def unread?
  (labelIds||[]).include?("UNREAD")
end