Class: Zm::Client::Message

Inherits:
Base::AccountObject show all
Defined in:
lib/zm/client/message/message.rb

Overview

class message for account

Defined Under Namespace

Classes: Attachment, Attachments, Body, Recipient, Recipients

Constant Summary collapse

INSTANCE_VARIABLE_KEYS =
i[id date l su fr autoSendTime mid idnt]

Instance Attribute Summary collapse

Attributes inherited from Base::Object

#grantee_type, #id, #name, #parent, #token

Instance Method Summary collapse

Methods inherited from Base::AccountObject

#concat, #create!, #modify!, #rename!, #soap_account_connector, #to_h

Methods inherited from Base::Object

#arrow_name, #clone, #concat, #convert_json_string_value, #disable_builder, #enable_builder, #instance_variables_array, #instance_variables_hash, #logger, #recorded?, #save!, #to_s, #use_builder?

Constructor Details

#initialize(parent, json = nil) {|_self| ... } ⇒ Message

Returns a new instance of Message.

Yields:

  • (_self)

Yield Parameters:



13
14
15
16
17
18
19
20
21
22
23
# File 'lib/zm/client/message/message.rb', line 13

def initialize(parent, json = nil)
  @parent = parent
  @recipients = Recipients.new
  @subject = ''
  @body = Body.new
  @attachments = Attachments.new

  init_from_json(json) if json.is_a?(Hash)

  yield(self) if block_given?
end

Instance Attribute Details

#attachmentsObject (readonly)

Returns the value of attribute attachments.



11
12
13
# File 'lib/zm/client/message/message.rb', line 11

def attachments
  @attachments
end

#bodyObject (readonly)

Returns the value of attribute body.



11
12
13
# File 'lib/zm/client/message/message.rb', line 11

def body
  @body
end

#folderObject

Returns the value of attribute folder.



11
12
13
# File 'lib/zm/client/message/message.rb', line 11

def folder
  @folder
end

#recipientsObject (readonly)

Returns the value of attribute recipients.



11
12
13
# File 'lib/zm/client/message/message.rb', line 11

def recipients
  @recipients
end

#subjectObject

Returns the value of attribute subject.



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

def subject
  @subject
end

Instance Method Details

#all_instance_variable_keysObject



29
30
31
# File 'lib/zm/client/message/message.rb', line 29

def all_instance_variable_keys
  INSTANCE_VARIABLE_KEYS
end

#delete!Object



42
43
44
# File 'lib/zm/client/message/message.rb', line 42

def delete!
  msg_action('delete')
end

#flag!Object



63
64
65
# File 'lib/zm/client/message/message.rb', line 63

def flag!
  msg_action('flag')
end

#fromObject



33
34
35
# File 'lib/zm/client/message/message.rb', line 33

def from
  @from ||= @recipients.find { |r| r.field == Recipient::FROM }
end

#has_attachment?Boolean

Returns:

  • (Boolean)


25
26
27
# File 'lib/zm/client/message/message.rb', line 25

def has_attachment?
  @has_attachment ||= @attachments.all.any?
end

#init_attachment_from_json(json) ⇒ Object



147
148
149
150
151
152
153
154
155
156
157
158
159
160
# File 'lib/zm/client/message/message.rb', line 147

def init_attachment_from_json(json)
  # puts "\ninit_attachment_from_json #{json}\n"
  pj = Zm::Client::Message::Attachment.new(self)
  # pj.part = json[:part],
  pj.mid  = json[:mid]
  pj.aid  = json[:aid]
  pj.ct   = json[:ct]
  pj.s    = json[:s]
  pj.filename = json[:filename]
  pj.ci   = json[:ci]
  pj.cd   = json[:cd]
  pj.part = json[:part]
  attachments.add(pj)
end

#init_body_from_json(json) ⇒ Object



141
142
143
144
145
# File 'lib/zm/client/message/message.rb', line 141

def init_body_from_json(json)
  # puts "\ninit_body_from_json #{json}\n"
  body.text = json[:content] if json[:ct] == 'text/plain'
  body.html = json[:content] if json[:ct] == 'text/html'
end

#init_from_json(json) ⇒ Object



105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/zm/client/message/message.rb', line 105

def init_from_json(json)
  # puts json
  @id   = json[:id]
  @date = Time.at(json[:d]/1000)
  @l    = json[:l]
  @su   = json[:su]
  @fr   = json[:fr]
  @autoSendTime   = json[:autoSendTime]
  @mid  = json[:mid]
  @idnt = json[:idnt]
  @has_attachment = json[:f].to_s.include?('a')

  json[:e].each do |e|
    recipient = Recipient.new(e[:t], e[:a], e[:p])
    @recipients.add(recipient)
  end

  init_part_from_json(json[:mp])
end

#init_part_from_json(json) ⇒ Object



125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
# File 'lib/zm/client/message/message.rb', line 125

def init_part_from_json(json)
  return if json.nil?
  # puts json
  json = [json] unless json.is_a?(Array)

  json.each do |json_part|
    if ['text/plain', 'text/html'].include?(json_part[:ct])
      init_body_from_json(json_part)
    elsif json_part[:cd] == 'attachment'
      init_attachment_from_json(json_part)
    else
      init_part_from_json(json_part[:mp])
    end
  end
end

#move!(folder_id = l) ⇒ Object



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

def move!(folder_id = l)
  msg_action('move', l: folder_id)
  l = folder_id if l != folder_id
end

#msg_action(action_name, options = {}) ⇒ Object



162
163
164
165
# File 'lib/zm/client/message/message.rb', line 162

def msg_action(action_name, options = {})
  @parent.sacc.msg_action(@parent.token, action_name, id, options)
  true
end

#read!Object



55
56
57
# File 'lib/zm/client/message/message.rb', line 55

def read!
  msg_action('read')
end

#send!Object



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

def send!
  @parent.sacc.send_msg(@parent.token, to_jsns)
end

#spam!Object



79
80
81
# File 'lib/zm/client/message/message.rb', line 79

def spam!
  msg_action('spam')
end

#tag!(tag_name) ⇒ Object



71
72
73
# File 'lib/zm/client/message/message.rb', line 71

def tag!(tag_name)
  msg_action('tag', tn: tag_name)
end

#to_jsnsObject



91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/zm/client/message/message.rb', line 91

def to_jsns
  h = {
    id: @id,
    attach: @attachments.to_jsns,
    e: @recipients.to_jsns,
    su: { _content: @su },
    mp: @body.to_jsns
  }.delete_if { |_, v| v.nil? }

  h[:did] = @id if l.to_i == FolderDefault::DRAFTS[:id]

  h
end

#trash!Object



83
84
85
# File 'lib/zm/client/message/message.rb', line 83

def trash!
  msg_action('trash')
end

#unflag!Object



59
60
61
# File 'lib/zm/client/message/message.rb', line 59

def unflag!
  msg_action('!flag')
end

#unread!Object



51
52
53
# File 'lib/zm/client/message/message.rb', line 51

def unread!
  msg_action('!read')
end

#unspam!Object



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

def unspam!
  msg_action('!spam')
end

#untag!(tag_name) ⇒ Object



67
68
69
# File 'lib/zm/client/message/message.rb', line 67

def untag!(tag_name)
  msg_action('!tag', tn: tag_name)
end