zm-ruby-client 0.10.4 → 0.17.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (132) hide show
  1. checksums.yaml +4 -4
  2. data/.gitignore +1 -0
  3. data/Gemfile +1 -0
  4. data/README.md +6 -0
  5. data/lib/zm/client/account/account.rb +39 -17
  6. data/lib/zm/client/account/account_jsns_initializer.rb +44 -0
  7. data/lib/zm/client/account/accounts_builder.rb +6 -3
  8. data/lib/zm/client/account/accounts_collection.rb +11 -20
  9. data/lib/zm/client/account.rb +1 -0
  10. data/lib/zm/client/ace/ace.rb +22 -9
  11. data/lib/zm/client/ace/ace_jsns_builder.rb +38 -0
  12. data/lib/zm/client/ace/ace_jsns_initializer.rb +26 -0
  13. data/lib/zm/client/ace/aces_builder.rb +6 -9
  14. data/lib/zm/client/ace/aces_collection.rb +23 -9
  15. data/lib/zm/client/ace.rb +2 -0
  16. data/lib/zm/client/appointment/appointment.rb +9 -6
  17. data/lib/zm/client/appointment/appointments_collection.rb +6 -90
  18. data/lib/zm/client/backup/backup.rb +8 -4
  19. data/lib/zm/client/base/account_object.rb +34 -0
  20. data/lib/zm/client/base/account_objects_collection.rb +14 -0
  21. data/lib/zm/client/base/account_search_objects_collection.rb +125 -0
  22. data/lib/zm/client/base/admin_object.rb +6 -0
  23. data/lib/zm/client/base/admin_objects_collection.rb +65 -0
  24. data/lib/zm/client/base/base_jsns_builder.rb +19 -0
  25. data/lib/zm/client/base/base_jsns_initializer.rb +29 -0
  26. data/lib/zm/client/base/folder_object.rb +41 -0
  27. data/lib/zm/client/base/ldap_filter.rb +33 -0
  28. data/lib/zm/client/base/object.rb +19 -0
  29. data/lib/zm/client/base/objects_builder.rb +4 -0
  30. data/lib/zm/client/base/objects_collection.rb +11 -32
  31. data/lib/zm/client/base/zimbra_attribute.rb +119 -0
  32. data/lib/zm/client/base/zimbra_attributes_collection.rb +38 -0
  33. data/lib/zm/client/base.rb +15 -0
  34. data/lib/zm/client/cluster/cluster.rb +47 -7
  35. data/lib/zm/client/cluster/cluster_config.rb +19 -17
  36. data/lib/zm/client/common/token_metadata.rb +44 -0
  37. data/lib/zm/client/common.rb +2 -1
  38. data/lib/zm/client/connector/rest_account.rb +0 -3
  39. data/lib/zm/client/connector/soap_account.rb +66 -122
  40. data/lib/zm/client/connector/soap_admin.rb +47 -15
  41. data/lib/zm/client/connector/soap_base.rb +11 -3
  42. data/lib/zm/client/contact/contact.rb +2 -1
  43. data/lib/zm/client/contact/contacts_builder.rb +3 -13
  44. data/lib/zm/client/contact/contacts_collection.rb +6 -12
  45. data/lib/zm/client/cos/cos.rb +9 -5
  46. data/lib/zm/client/cos/coses_builder.rb +1 -16
  47. data/lib/zm/client/cos/coses_collection.rb +6 -9
  48. data/lib/zm/client/distributionlist/distributionlist.rb +44 -6
  49. data/lib/zm/client/distributionlist/distributionlists_builder.rb +2 -17
  50. data/lib/zm/client/distributionlist/distributionlists_collection.rb +4 -11
  51. data/lib/zm/client/distributionlist/dls_membership_collection.rb +24 -0
  52. data/lib/zm/client/distributionlist.rb +1 -0
  53. data/lib/zm/client/document/document.rb +11 -9
  54. data/lib/zm/client/domain/domain.rb +23 -7
  55. data/lib/zm/client/domain/domain_accounts_collection.rb +13 -0
  56. data/lib/zm/client/domain/domains_builder.rb +1 -19
  57. data/lib/zm/client/domain/domains_collection.rb +7 -10
  58. data/lib/zm/client/domain.rb +1 -0
  59. data/lib/zm/client/folder/folder.rb +50 -124
  60. data/lib/zm/client/folder/folder_grant.rb +44 -22
  61. data/lib/zm/client/folder/folder_grant_jsns_builder.rb +43 -0
  62. data/lib/zm/client/folder/folder_grants_collection.rb +35 -0
  63. data/lib/zm/client/folder/folder_jsns_builder.rb +132 -0
  64. data/lib/zm/client/folder/folder_jsns_initializer.rb +49 -0
  65. data/lib/zm/client/folder/folder_retention_policies_collection.rb +40 -0
  66. data/lib/zm/client/folder/folder_retention_policy.rb +8 -30
  67. data/lib/zm/client/folder/folders_builder.rb +2 -2
  68. data/lib/zm/client/folder/folders_collection.rb +46 -19
  69. data/lib/zm/client/folder/folders_jsns_builder.rb +22 -0
  70. data/lib/zm/client/folder.rb +6 -0
  71. data/lib/zm/client/identity/identities_builder.rb +4 -15
  72. data/lib/zm/client/identity/identities_collection.rb +7 -13
  73. data/lib/zm/client/identity/identity.rb +9 -7
  74. data/lib/zm/client/message/message.rb +111 -21
  75. data/lib/zm/client/message/messages_builder.rb +1 -1
  76. data/lib/zm/client/message/messages_collection.rb +10 -92
  77. data/lib/zm/client/mountpoint/mountpoint.rb +15 -68
  78. data/lib/zm/client/mountpoint/mountpoint_jsns_builder.rb +100 -0
  79. data/lib/zm/client/mountpoint/mountpoint_jsns_initializer.rb +33 -0
  80. data/lib/zm/client/mountpoint/mountpoints_builder.rb +1 -1
  81. data/lib/zm/client/mountpoint/mountpoints_collection.rb +14 -23
  82. data/lib/zm/client/mountpoint.rb +2 -0
  83. data/lib/zm/client/resource/resource.rb +5 -5
  84. data/lib/zm/client/resource/resources_builder.rb +1 -16
  85. data/lib/zm/client/resource/resources_collection.rb +5 -13
  86. data/lib/zm/client/search_folder/search_folder.rb +19 -39
  87. data/lib/zm/client/search_folder/search_folder_jsns_builder.rb +86 -0
  88. data/lib/zm/client/search_folder/search_folder_jsns_initializer.rb +33 -0
  89. data/lib/zm/client/search_folder/search_folders_builder.rb +7 -11
  90. data/lib/zm/client/search_folder/search_folders_collection.rb +7 -15
  91. data/lib/zm/client/search_folder.rb +2 -0
  92. data/lib/zm/client/server/server.rb +7 -14
  93. data/lib/zm/client/server/server_accounts_collection.rb +19 -0
  94. data/lib/zm/client/server/servers_builder.rb +4 -16
  95. data/lib/zm/client/server/servers_collection.rb +5 -10
  96. data/lib/zm/client/server.rb +1 -0
  97. data/lib/zm/client/share/share.rb +2 -9
  98. data/lib/zm/client/share/shares_builder.rb +3 -12
  99. data/lib/zm/client/signature/signature.rb +17 -13
  100. data/lib/zm/client/signature/signature_jsns_builder.rb +5 -1
  101. data/lib/zm/client/signature/signature_jsns_initializer.rb +25 -0
  102. data/lib/zm/client/signature/signatures_builder.rb +7 -11
  103. data/lib/zm/client/signature/signatures_collection.rb +6 -14
  104. data/lib/zm/client/signature.rb +1 -0
  105. data/lib/zm/client/tag/tag.rb +27 -20
  106. data/lib/zm/client/tag/tag_jsns_builder.rb +55 -0
  107. data/lib/zm/client/tag/tag_jsns_initializer.rb +26 -0
  108. data/lib/zm/client/tag/tags_builder.rb +6 -9
  109. data/lib/zm/client/tag/tags_collection.rb +6 -12
  110. data/lib/zm/client/tag.rb +2 -0
  111. data/lib/zm/client/task/tasks_collection.rb +6 -90
  112. data/lib/zm/client/upload/upload.rb +1 -1
  113. data/lib/zm/client/version.rb +2 -2
  114. data/lib/zm/modules/common/dl_common.rb +6 -1
  115. data/lib/zm/modules/common/zimbra-attrs.json.bak +1 -0
  116. data/lib/zm/modules/zm_logger.rb +24 -0
  117. data/lib/zm/modules/zm_model.rb +41 -0
  118. data/test/accounts.rb +15 -1
  119. data/test/ace.rb +50 -0
  120. data/test/cluster.rb +41 -0
  121. data/test/cluster_zimbra_attributes.rb +38 -0
  122. data/test/domains.rb +8 -0
  123. data/test/folder.rb +125 -0
  124. data/test/folder_grant.rb +51 -0
  125. data/test/mountpoint.rb +49 -0
  126. data/test/retention_policies.rb +40 -0
  127. data/test/search_folder.rb +67 -0
  128. data/test/signature.rb +53 -0
  129. data/test/tag.rb +65 -0
  130. data/test/zimbra_attributes.rb +35 -0
  131. data/zm-ruby-client.gemspec +1 -0
  132. metadata +60 -3
@@ -3,18 +3,17 @@
3
3
  module Zm
4
4
  module Client
5
5
  # class for account folder retention policy
6
- class FolderRetentionPolicy < Base::AccountObject
7
-
8
- class << self
9
- def create_by_json(parent, policy, json)
10
- frp = self.new(parent)
11
- frp.init_from_json(policy, json)
12
- frp
13
- end
14
- end
6
+ class FolderRetentionPolicy
15
7
 
16
8
  attr_accessor :type, :policy, :lifetime
17
9
 
10
+ def initialize(parent, policy, lifetime, type)
11
+ @parent = parent
12
+ @policy = policy
13
+ @lifetime = lifetime
14
+ @type = type
15
+ end
16
+
18
17
  def keep?
19
18
  @policy == :keep
20
19
  end
@@ -22,27 +21,6 @@ module Zm
22
21
  def purge?
23
22
  @policy == :purge
24
23
  end
25
-
26
- def init_from_json(policy, json)
27
- return if json.empty? || json[:policy].nil?
28
-
29
- @policy = policy
30
- @type = json[:policy].first[:type]
31
- @lifetime = json[:policy].first[:lifetime]
32
- end
33
-
34
- def to_h
35
- # :purge=>[{:policy=>[{:lifetime=>"366d", :type=>"user"}]}]
36
- {
37
- @policy => {
38
- policy: {
39
- lifetime: @lifetime,
40
- type: @type
41
- }
42
- }
43
- }
44
- end
45
-
46
24
  end
47
25
  end
48
26
  end
@@ -28,7 +28,7 @@ module Zm
28
28
  def make
29
29
  root = @json[:Body][:GetFolderResponse][@key]
30
30
 
31
- @root_folder = Folder.new(@account, root.first)
31
+ @root_folder = FolderJsnsInitializer.create(@account, root.first)
32
32
 
33
33
  if !root.first[@key].nil? && root.first[@key].any?
34
34
  construct_tree(@root_folder, root.first[@key])
@@ -39,7 +39,7 @@ module Zm
39
39
 
40
40
  def construct_tree(parent_folder, json_folders)
41
41
  json_folders.each do |json_folder|
42
- folder = Folder.new(@account, json_folder)
42
+ folder = FolderJsnsInitializer.create(@account, json_folder)
43
43
  parent_folder.folders << folder
44
44
 
45
45
  construct_tree(folder, json_folder[@key]) if !json_folder[@key].nil? && json_folder[@key].any?
@@ -3,20 +3,25 @@
3
3
  module Zm
4
4
  module Client
5
5
  # collection of folders
6
- class FoldersCollection < Base::ObjectsCollection
7
- METHODS_MISSING_LIST = %i[select each map length].to_set.freeze
6
+ class FoldersCollection < Base::AccountObjectsCollection
8
7
 
9
8
  attr_reader :root
10
9
 
10
+ attr_accessor :view, :tr, :visible, :needGranteeName, :depth
11
+
11
12
  def initialize(parent)
12
- @parent = parent
13
+ @child_class = Folder
14
+ @builder_class = FoldersBuilder
15
+ super(parent)
13
16
  @root = nil
14
17
  reset_query_params
15
18
  end
16
19
 
17
- def new
18
- folder = Folder.new(@parent)
19
- yield(folder) if block_given?
20
+ def find(id)
21
+ folder = @child_class.new(@parent) do |f|
22
+ f.id = id
23
+ end
24
+ folder.reload!
20
25
  folder
21
26
  end
22
27
 
@@ -28,16 +33,7 @@ module Zm
28
33
  end
29
34
 
30
35
  def ids
31
- fb = FoldersBuilder.new @parent, make_query
32
- fb.ids
33
- end
34
-
35
- def all
36
- @all || all!
37
- end
38
-
39
- def all!
40
- build_response
36
+ @builder_class.new(@parent, make_query).ids
41
37
  end
42
38
 
43
39
  def clear
@@ -46,11 +42,35 @@ module Zm
46
42
  reset_query_params
47
43
  end
48
44
 
45
+ def document
46
+ @view = FolderDefault::BRIEFCASE[:type]
47
+ self
48
+ end
49
+
50
+ def appointment
51
+ @view = FolderDefault::CALENDAR[:type]
52
+ self
53
+ end
54
+
55
+ def contact
56
+ @view = FolderDefault::CONTACTS[:type]
57
+ self
58
+ end
59
+
60
+ def message
61
+ @view = FolderDefault::INBOX[:type]
62
+ self
63
+ end
64
+
65
+ def task
66
+ @view = FolderDefault::TASKS[:type]
67
+ self
68
+ end
69
+
49
70
  private
50
71
 
51
72
  def build_response
52
- rep = make_query
53
- fb = FoldersBuilder.new @parent, rep
73
+ fb = @builder_class.new(@parent, make_query)
54
74
  @root = fb.make
55
75
  @all = fb.flatten
56
76
  @all.select! { |folder| folder.view == @view } unless @view.nil?
@@ -58,12 +78,19 @@ module Zm
58
78
  end
59
79
 
60
80
  def make_query
61
- @parent.sacc.get_all_folders(@parent.token, @view, @tr)
81
+ @parent.sacc.get_folder(@parent.token, jsns_builder.to_jsns)
62
82
  end
63
83
 
64
84
  def reset_query_params
65
85
  @view = nil
66
86
  @tr = nil
87
+ @visible = nil
88
+ @needGranteeName = nil
89
+ @depth = nil
90
+ end
91
+
92
+ def jsns_builder
93
+ @jsns_builder ||= FoldersJsnsBuilder.new(self)
67
94
  end
68
95
  end
69
96
  end
@@ -0,0 +1,22 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Zm
4
+ module Client
5
+ # class for account collection folders jsns builder
6
+ class FoldersJsnsBuilder
7
+ def initialize(item)
8
+ @item = item
9
+ end
10
+
11
+ def to_jsns
12
+ {
13
+ visible: @item.visible,
14
+ needGranteeName: @item.needGranteeName,
15
+ view: @item.view,
16
+ depth: @item.depth,
17
+ tr: @item.tr
18
+ }.delete_if { |_, v| v.nil? }
19
+ end
20
+ end
21
+ end
22
+ end
@@ -1,8 +1,14 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require 'zm/client/folder/folder'
4
+ require 'zm/client/folder/folder_jsns_builder'
5
+ require 'zm/client/folder/folder_jsns_initializer'
6
+ require 'zm/client/folder/folders_jsns_builder'
4
7
  require 'zm/client/folder/folder_grant'
8
+ require 'zm/client/folder/folder_grants_collection'
9
+ require 'zm/client/folder/folder_grant_jsns_builder'
5
10
  require 'zm/client/folder/folder_retention_policy'
11
+ require 'zm/client/folder/folder_retention_policies_collection'
6
12
  require 'zm/client/folder/folders_builder'
7
13
  require 'zm/client/folder/folders_collection'
8
14
  require 'zm/client/folder/mod_document_folder'
@@ -2,23 +2,12 @@
2
2
 
3
3
  module Zm
4
4
  module Client
5
- # class factory [identitys]
5
+ # class factory [identities]
6
6
  class IdentitiesBuilder < Base::ObjectsBuilder
7
7
  def initialize(parent, json)
8
- @parent = parent
9
- @json = json
10
- end
11
-
12
- def make
13
- root = @json[:Body][:GetIdentitiesResponse][:identity]
14
- return [] if root.nil?
15
-
16
- root = [root] unless root.is_a?(Array)
17
- root.map do |s|
18
- identity = Identity.new(@parent)
19
- identity.init_from_json(s)
20
- identity
21
- end
8
+ super(parent, json)
9
+ @child_class = Identity
10
+ @json_item_key = :identity
22
11
  end
23
12
  end
24
13
  end
@@ -2,24 +2,18 @@
2
2
 
3
3
  module Zm
4
4
  module Client
5
- # collection account identitys
6
- class IdentitiesCollection < Base::ObjectsCollection
5
+ # collection account identities
6
+ class IdentitiesCollection < Base::AccountObjectsCollection
7
7
  def initialize(parent)
8
- @parent = parent
9
- end
10
-
11
- def new
12
- identity = Identity.new(@parent)
13
- yield(identity) if block_given?
14
- identity
8
+ @child_class = Identity
9
+ @builder_class = IdentitiesBuilder
10
+ super(parent)
15
11
  end
16
12
 
17
13
  private
18
14
 
19
- def build_response
20
- rep = @parent.sacc.get_all_identities(@parent.token)
21
- ib = IdentitiesBuilder.new @parent, rep
22
- ib.make
15
+ def make_query
16
+ @parent.sacc.get_all_identities(@parent.token)
23
17
  end
24
18
  end
25
19
  end
@@ -8,22 +8,23 @@ module Zm
8
8
  INSTANCE_VARIABLE_KEYS = %i[id name zimbraPrefIdentityName zimbraPrefFromDisplay zimbraPrefFromAddress
9
9
  zimbraPrefFromAddressType zimbraPrefReplyToEnabled zimbraPrefReplyToDisplay zimbraPrefReplyToAddress
10
10
  zimbraPrefDefaultSignatureId zimbraPrefForwardReplySignatureId zimbraPrefWhenSentToEnabled
11
- zimbraPrefWhenInFoldersEnabled]
11
+ zimbraPrefWhenInFoldersEnabled zimbraPrefWhenSentToAddresses]
12
12
 
13
13
  ATTRS_WRITE = %i[zimbraPrefIdentityName zimbraPrefFromDisplay zimbraPrefFromAddress
14
14
  zimbraPrefFromAddressType zimbraPrefReplyToEnabled zimbraPrefReplyToDisplay zimbraPrefReplyToAddress
15
15
  zimbraPrefDefaultSignatureId zimbraPrefForwardReplySignatureId zimbraPrefWhenSentToEnabled
16
- zimbraPrefWhenInFoldersEnabled]
16
+ zimbraPrefWhenInFoldersEnabled zimbraPrefWhenSentToAddresses]
17
17
 
18
18
  attr_accessor *INSTANCE_VARIABLE_KEYS
19
19
 
20
- def concat
21
- INSTANCE_VARIABLE_KEYS.map { |key| instance_variable_get(arrow_name(key)) }
20
+ def all_instance_variable_keys
21
+ INSTANCE_VARIABLE_KEYS
22
22
  end
23
23
 
24
24
  def init_from_json(json)
25
- super(json)
26
- INSTANCE_VARIABLE_KEYS.each do |key|
25
+ @id = json[:id]
26
+ @name = json[:name]
27
+ all_instance_variable_keys.each do |key|
27
28
  value = json[:_attrs][key]
28
29
  next if value.nil?
29
30
 
@@ -55,7 +56,8 @@ module Zm
55
56
  end
56
57
 
57
58
  def delete!
58
- @parent.sacc.delete_identity(@parent.token, id)
59
+ @parent.sacc.delete_identity(@parent.token, @id)
60
+ super
59
61
  end
60
62
 
61
63
  def rename!(new_name)
@@ -4,7 +4,7 @@ module Zm
4
4
  module Client
5
5
  # class message for account
6
6
  class Message < Base::AccountObject
7
- INSTANCE_VARIABLE_KEYS = %i[id date l su fr]
7
+ INSTANCE_VARIABLE_KEYS = %i[id date l su fr autoSendTime mid idnt]
8
8
  attr_accessor *INSTANCE_VARIABLE_KEYS
9
9
 
10
10
  attr_accessor :subject
@@ -22,6 +22,14 @@ module Zm
22
22
  yield(self) if block_given?
23
23
  end
24
24
 
25
+ def has_attachment?
26
+ @has_attachment ||= @attachments.all.any?
27
+ end
28
+
29
+ def all_instance_variable_keys
30
+ INSTANCE_VARIABLE_KEYS
31
+ end
32
+
25
33
  def from
26
34
  @from ||= @recipients.find { |r| r.field == Recipient::FROM }
27
35
  end
@@ -31,15 +39,6 @@ module Zm
31
39
  @l = folder.id
32
40
  end
33
41
 
34
- def to_jsns
35
- {
36
- attach: @attachments.to_jsns,
37
- e: @recipients.to_jsns,
38
- su: { _content: @subject },
39
- mp: @body.to_jsns
40
- }
41
- end
42
-
43
42
  def delete!
44
43
  msg_action('delete')
45
44
  end
@@ -89,6 +88,20 @@ module Zm
89
88
  @parent.sacc.send_msg(@parent.token, to_jsns)
90
89
  end
91
90
 
91
+ def to_jsns
92
+ h = {
93
+ id: @id,
94
+ attach: @attachments.to_jsns,
95
+ e: @recipients.to_jsns,
96
+ su: { _content: @su },
97
+ mp: @body.to_jsns
98
+ }.delete_if { |_, v| v.nil? }
99
+
100
+ h[:did] = @id if l.to_i == FolderDefault::DRAFTS[:id]
101
+
102
+ h
103
+ end
104
+
92
105
  def init_from_json(json)
93
106
  # puts json
94
107
  @id = json[:id]
@@ -96,11 +109,54 @@ module Zm
96
109
  @l = json[:l]
97
110
  @su = json[:su]
98
111
  @fr = json[:fr]
112
+ @autoSendTime = json[:autoSendTime]
113
+ @mid = json[:mid]
114
+ @idnt = json[:idnt]
115
+ @has_attachment = json[:f].to_s.include?('a')
99
116
 
100
117
  json[:e].each do |e|
101
118
  recipient = Recipient.new(e[:t], e[:a], e[:p])
102
119
  @recipients.add(recipient)
103
120
  end
121
+
122
+ init_part_from_json(json[:mp])
123
+ end
124
+
125
+ def init_part_from_json(json)
126
+ return if json.nil?
127
+ # puts json
128
+ json = [json] unless json.is_a?(Array)
129
+
130
+ json.each do |json_part|
131
+ if ['text/plain', 'text/html'].include?(json_part[:ct])
132
+ init_body_from_json(json_part)
133
+ elsif json_part[:cd] == 'attachment'
134
+ init_attachment_from_json(json_part)
135
+ else
136
+ init_part_from_json(json_part[:mp])
137
+ end
138
+ end
139
+ end
140
+
141
+ def init_body_from_json(json)
142
+ # puts "\ninit_body_from_json #{json}\n"
143
+ body.text = json[:content] if json[:ct] == 'text/plain'
144
+ body.html = json[:content] if json[:ct] == 'text/html'
145
+ end
146
+
147
+ def init_attachment_from_json(json)
148
+ # puts "\ninit_attachment_from_json #{json}\n"
149
+ pj = Zm::Client::Message::Attachment.new(self)
150
+ # pj.part = json[:part],
151
+ pj.mid = json[:mid]
152
+ pj.aid = json[:aid]
153
+ pj.ct = json[:ct]
154
+ pj.s = json[:s]
155
+ pj.filename = json[:filename]
156
+ pj.ci = json[:ci]
157
+ pj.cd = json[:cd]
158
+ pj.part = json[:part]
159
+ attachments.add(pj)
104
160
  end
105
161
 
106
162
  def msg_action(action_name, options = {})
@@ -113,18 +169,18 @@ module Zm
113
169
  attr_accessor :text, :html
114
170
 
115
171
  def text_jsns
116
- { ct: 'text/plain', content: { _content: @text } }
172
+ @text.nil? ? nil : { ct: 'text/plain', content: { _content: @text } }
117
173
  end
118
174
 
119
175
  def html_jsns
120
- { ct: 'text/html', content: { _content: @html } }
176
+ @html.nil? ? nil : { ct: 'text/html', content: { _content: @html } }
121
177
  end
122
178
 
123
179
  def to_jsns
124
180
  [
125
181
  {
126
182
  ct: 'multipart/alternative',
127
- mp: [text_jsns, html_jsns]
183
+ mp: [text_jsns, html_jsns].compact
128
184
  }
129
185
  ]
130
186
  end
@@ -132,36 +188,66 @@ module Zm
132
188
 
133
189
  # collection attachments
134
190
  class Attachments
191
+ attr_reader :all
135
192
  def initialize
136
- @attachments = []
193
+ @all = []
137
194
  end
138
195
 
139
196
  def add(attachment)
140
197
  return unless attachment.is_a?(Attachment)
141
198
 
142
- @attachments.push(attachment)
199
+ @all.push(attachment)
143
200
  end
144
201
 
145
202
  def to_jsns
146
- @attachments.map(&:to_jsns)
203
+ @all.map(&:to_jsns)
147
204
  end
148
205
  end
149
206
 
150
207
  # class attachment for email
151
208
  class Attachment
152
- attr_accessor :aid, :part, :mid
209
+ attr_accessor :aid, :part, :mid, :ct, :s, :filename, :ci, :cd
153
210
 
154
- def initialize
211
+ def initialize(parent)
212
+ @parent = parent
155
213
  yield(self) if block_given?
156
214
  end
157
215
 
216
+ def download(dest_file_path)
217
+ h = {
218
+ id: @parent.id,
219
+ part: part,
220
+ auth: 'qp',
221
+ zauthtoken: account.token,
222
+ disp: 'a'
223
+ }
224
+
225
+ url = account.home_url
226
+
227
+ uri = Addressable::URI.new
228
+ uri.query_values = h
229
+ url << '?' << uri.query
230
+
231
+ uploader = Upload.new(@parent, RestAccountConnector.new)
232
+ uploader.download_file_with_url(url, dest_file_path)
233
+ end
234
+
158
235
  def to_jsns
159
236
  {
160
- part: @part,
161
- mid: @mid,
162
- aid: @aid
237
+ part: part,
238
+ mid: mid,
239
+ aid: aid,
240
+ ct: ct,
241
+ s: s,
242
+ filename: filename,
243
+ ci: ci,
244
+ cd: cd
163
245
  }.reject { |_, v| v.nil? }
164
246
  end
247
+
248
+ def account
249
+ @parent.parent
250
+ end
165
251
  end
166
252
 
167
253
  # Collection recipients
@@ -212,6 +298,10 @@ module Zm
212
298
  @display_name = display_name
213
299
  end
214
300
 
301
+ def to_s
302
+ "#{@email} (#{@display_name})"
303
+ end
304
+
215
305
  def to_jsns
216
306
  {
217
307
  t: @field,
@@ -16,7 +16,7 @@ module Zm
16
16
  end
17
17
 
18
18
  def ids
19
- root.map { |s| s[:id] }
19
+ @json[:Body][:SearchResponse][:hit].map { |s| s[:id] }
20
20
  end
21
21
 
22
22
  def root
@@ -3,108 +3,26 @@
3
3
  module Zm
4
4
  module Client
5
5
  # Collection Messages
6
- class MessagesCollection < Base::ObjectsCollection
7
- attr_accessor :more
8
-
6
+ class MessagesCollection < Base::AccountSearchObjectsCollection
9
7
  def initialize(parent)
10
- @parent = parent
11
- @more = true
12
- reset_query_params
13
- end
14
-
15
-
16
- def find(id)
17
- rep = @parent.sacc.get_msg(@parent.token, id, { part: 0 })
18
- entry = rep[:Body][:GetMsgResponse][:m].first
19
- # puts entry
20
- msg = Message.new(@parent)
21
- msg.init_from_json(entry)
22
- msg
23
- end
24
-
25
- def new
26
- message = Message.new(@parent)
27
- yield(message) if block_given?
28
- message
29
- end
30
-
31
- def start_at(start_at)
32
- @start_at = start_at
33
- self
34
- end
35
-
36
- def end_at(end_at)
37
- @end_at = end_at
38
- self
39
- end
40
-
41
- def folders(folders)
42
- @folders = folders
43
- @folder_ids = @folders.map(&:id)
44
- self
45
- end
46
-
47
- def folder_ids(folder_ids)
48
- @folder_ids = folder_ids
49
- self
50
- end
51
-
52
- def where(query)
53
- @query = query
54
- self
55
- end
56
-
57
- def ids
58
- search_builder.ids
59
- end
60
-
61
- def all
62
- build_response
8
+ super(parent)
9
+ @child_class = Message
10
+ @builder_class = MessagesBuilder
11
+ @type = 'message'
12
+ @sort_by = 'dateDesc'
63
13
  end
64
14
 
65
15
  private
66
16
 
67
- def search_response
68
- rep = @parent.sacc.search(@parent.token, 'message', @offset, @limit, 'dateDesc', query, build_options)
69
- @more = rep[:Body][:SearchResponse][:more]
70
- rep
71
- end
72
-
73
- def search_builder
74
- MessagesBuilder.new(@parent, search_response)
75
- end
76
-
77
- def build_response
78
- messages = search_builder.make
79
- messages.each { |msg| msg.folder = find_folder(msg) } unless @folders.empty?
80
- messages
81
- end
82
-
83
17
  def build_options
84
- {
85
- recip: 2
86
- }
87
- end
88
-
89
- def query
90
- return @query unless @query.nil?
91
-
92
- return nil if @folder_ids.empty?
93
-
94
- @folder_ids.map { |id| %Q{inid:"#{id}"} }.join(' OR ')
95
- end
96
-
97
- def find_folder(message)
98
- @folders.find { |folder| folder.id == message.l }
18
+ options = super
19
+ options[:recip] = @recip
20
+ options
99
21
  end
100
22
 
101
23
  def reset_query_params
102
24
  super
103
- @start_at = nil
104
- @end_at = nil
105
- @query = nil
106
- @folder_ids = []
107
- @folders = []
25
+ @recip = 2
108
26
  end
109
27
  end
110
28
  end