zm-ruby-client 2.2.6 → 3.0.0

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 (201) hide show
  1. checksums.yaml +4 -4
  2. data/.rubocop.yml +6 -0
  3. data/.ruby-version +1 -0
  4. data/CHANGELOG.md +10 -6
  5. data/Gemfile +1 -6
  6. data/README.md +10 -15
  7. data/VERSION +1 -1
  8. data/lib/zm/client/account/account.rb +35 -22
  9. data/lib/zm/client/account/account_aliases_collection.rb +14 -6
  10. data/lib/zm/client/account/account_dls_membership_collection.rb +10 -4
  11. data/lib/zm/client/account/account_dls_owner_collection.rb +8 -5
  12. data/lib/zm/client/account/account_jsns_builder.rb +31 -19
  13. data/lib/zm/client/account/account_jsns_initializer.rb +4 -14
  14. data/lib/zm/client/account/accounts_builder.rb +1 -1
  15. data/lib/zm/client/account/accounts_collection.rb +15 -8
  16. data/lib/zm/client/ace/ace.rb +5 -5
  17. data/lib/zm/client/ace/ace_jsns_builder.rb +8 -8
  18. data/lib/zm/client/ace/ace_jsns_initializer.rb +7 -7
  19. data/lib/zm/client/ace/aces_builder.rb +1 -1
  20. data/lib/zm/client/ace/aces_collection.rb +2 -2
  21. data/lib/zm/client/appointment/appointment.rb +27 -24
  22. data/lib/zm/client/appointment/appointment_jsns_builder.rb +29 -20
  23. data/lib/zm/client/appointment/appointment_jsns_initializer.rb +14 -13
  24. data/lib/zm/client/appointment/appointments_builder.rb +1 -1
  25. data/lib/zm/client/appointment/appointments_collection.rb +8 -7
  26. data/lib/zm/client/backup/backup.rb +6 -2
  27. data/lib/zm/client/backup/backup_jsns_initializer.rb +14 -12
  28. data/lib/zm/client/backup/backups_builder.rb +1 -1
  29. data/lib/zm/client/backup/backups_collection.rb +2 -1
  30. data/lib/zm/client/base/account_search_objects_collection.rb +46 -20
  31. data/lib/zm/client/base/admin_objects_collection.rb +22 -27
  32. data/lib/zm/client/base/base_account_jsns_builder.rb +1 -4
  33. data/lib/zm/client/base/base_jsns_initializer.rb +8 -8
  34. data/lib/zm/client/base/mailbox_infos_collection.rb +23 -15
  35. data/lib/zm/client/base/mailbox_object.rb +174 -69
  36. data/lib/zm/client/base/mailbox_prefs_collection.rb +4 -8
  37. data/lib/zm/client/base/object.rb +2 -2
  38. data/lib/zm/client/base/objects_builder.rb +7 -3
  39. data/lib/zm/client/base/objects_collection.rb +4 -13
  40. data/lib/zm/client/base/zimbra_attribute.rb +4 -17
  41. data/lib/zm/client/base/zimbra_attributes_collection.rb +16 -9
  42. data/lib/zm/client/base.rb +0 -1
  43. data/lib/zm/client/cluster/batch_request.rb +50 -0
  44. data/lib/zm/client/cluster/cluster.rb +49 -27
  45. data/lib/zm/client/cluster/cluster_config.rb +129 -40
  46. data/lib/zm/client/common/recipients.rb +2 -2
  47. data/lib/zm/client/common/utils.rb +1 -13
  48. data/lib/zm/client/common.rb +0 -1
  49. data/lib/zm/client/connector/{rest_account.rb → rest_connector.rb} +44 -20
  50. data/lib/zm/client/connector/soap_account.rb +19 -15
  51. data/lib/zm/client/connector/soap_admin.rb +9 -9
  52. data/lib/zm/client/connector/soap_base.rb +23 -18
  53. data/lib/zm/client/connector/soap_error.rb +3 -3
  54. data/lib/zm/client/constant.rb +18 -16
  55. data/lib/zm/client/contact/contact.rb +21 -40
  56. data/lib/zm/client/contact/contact_jsns_builder.rb +1 -1
  57. data/lib/zm/client/contact/contact_jsns_initializer.rb +4 -26
  58. data/lib/zm/client/contact/contacts_builder.rb +1 -1
  59. data/lib/zm/client/contact/contacts_collection.rb +4 -24
  60. data/lib/zm/client/contact/group_contact_jsns_builder.rb +17 -18
  61. data/lib/zm/client/contact/mod_group_contact.rb +6 -2
  62. data/lib/zm/client/cos/cos.rb +35 -11
  63. data/lib/zm/client/cos/cos_domains_collection.rb +1 -1
  64. data/lib/zm/client/cos/cos_jsns_builder.rb +22 -9
  65. data/lib/zm/client/cos/cos_jsns_initializer.rb +2 -6
  66. data/lib/zm/client/cos/cos_servers_collection.rb +5 -6
  67. data/lib/zm/client/cos/coses_builder.rb +1 -1
  68. data/lib/zm/client/cos/coses_collection.rb +6 -4
  69. data/lib/zm/client/datasource/datasource.rb +27 -34
  70. data/lib/zm/client/datasource/datasource_jsns_builder.rb +1 -1
  71. data/lib/zm/client/datasource/datasource_jsns_initializer.rb +5 -6
  72. data/lib/zm/client/datasource/datasources_builder.rb +6 -9
  73. data/lib/zm/client/datasource/datasources_collection.rb +6 -5
  74. data/lib/zm/client/distributionlist/distributionlist.rb +22 -42
  75. data/lib/zm/client/distributionlist/distributionlist_aliases_collection.rb +14 -6
  76. data/lib/zm/client/distributionlist/distributionlist_jsns_builder.rb +20 -16
  77. data/lib/zm/client/distributionlist/distributionlist_jsns_initializer.rb +2 -11
  78. data/lib/zm/client/distributionlist/distributionlist_members_collection.rb +20 -12
  79. data/lib/zm/client/distributionlist/distributionlist_owners_collection.rb +16 -5
  80. data/lib/zm/client/distributionlist/distributionlists_builder.rb +1 -1
  81. data/lib/zm/client/distributionlist/distributionlists_collection.rb +12 -6
  82. data/lib/zm/client/distributionlist/dls_membership_collection.rb +12 -6
  83. data/lib/zm/client/document/document.rb +16 -8
  84. data/lib/zm/client/document/document_jsns_builder.rb +1 -1
  85. data/lib/zm/client/document/document_jsns_initializer.rb +27 -26
  86. data/lib/zm/client/document/documents_builder.rb +1 -1
  87. data/lib/zm/client/document/documents_collection.rb +1 -1
  88. data/lib/zm/client/domain/domain.rb +18 -37
  89. data/lib/zm/client/domain/domain_accounts_collection.rb +1 -1
  90. data/lib/zm/client/domain/domain_distributionlists_collection.rb +1 -1
  91. data/lib/zm/client/domain/domain_jsns_builder.rb +18 -9
  92. data/lib/zm/client/domain/domain_jsns_initializer.rb +2 -6
  93. data/lib/zm/client/domain/domain_resources_collection.rb +1 -1
  94. data/lib/zm/client/domain/domains_builder.rb +1 -1
  95. data/lib/zm/client/domain/domains_collection.rb +12 -6
  96. data/lib/zm/client/filter_rules/filter_rule_jsns_initializer.rb +7 -6
  97. data/lib/zm/client/filter_rules/filter_rules_builder.rb +5 -2
  98. data/lib/zm/client/filter_rules/filter_rules_collection.rb +6 -5
  99. data/lib/zm/client/filter_rules/outgoing_filter_rules_collection.rb +5 -2
  100. data/lib/zm/client/folder/folder.rb +73 -56
  101. data/lib/zm/client/folder/folder_grant.rb +7 -5
  102. data/lib/zm/client/folder/folder_grant_jsns_builder.rb +6 -3
  103. data/lib/zm/client/folder/folder_grants_collection.rb +2 -4
  104. data/lib/zm/client/folder/folder_jsns_builder.rb +14 -7
  105. data/lib/zm/client/folder/folder_jsns_initializer.rb +58 -40
  106. data/lib/zm/client/folder/folder_retention_policies_collection.rb +4 -2
  107. data/lib/zm/client/folder/folders_builder.rb +1 -1
  108. data/lib/zm/client/folder/folders_collection.rb +21 -18
  109. data/lib/zm/client/folder/folders_jsns_builder.rb +4 -2
  110. data/lib/zm/client/folder/mod_document_folder.rb +5 -4
  111. data/lib/zm/client/identity/identities_builder.rb +1 -1
  112. data/lib/zm/client/identity/identities_collection.rb +6 -5
  113. data/lib/zm/client/identity/identity.rb +9 -29
  114. data/lib/zm/client/identity/identity_jsns_builder.rb +8 -4
  115. data/lib/zm/client/identity/identity_jsns_initializer.rb +5 -4
  116. data/lib/zm/client/license/license.rb +7 -1
  117. data/lib/zm/client/license/license_jsns_initializer.rb +8 -5
  118. data/lib/zm/client/license/licenses_collection.rb +1 -1
  119. data/lib/zm/client/mailbox/mailbox_item_concern.rb +33 -0
  120. data/lib/zm/client/mailbox/mailbox_item_id.rb +7 -0
  121. data/lib/zm/client/message/attachments_collection.rb +2 -2
  122. data/lib/zm/client/message/message.rb +60 -21
  123. data/lib/zm/client/message/message_flags.rb +21 -13
  124. data/lib/zm/client/message/message_jsns_builder.rb +11 -6
  125. data/lib/zm/client/message/message_jsns_initializer.rb +24 -22
  126. data/lib/zm/client/message/messages_builder.rb +1 -1
  127. data/lib/zm/client/message/messages_collection.rb +1 -1
  128. data/lib/zm/client/mountpoint/mountpoint.rb +17 -25
  129. data/lib/zm/client/mountpoint/mountpoint_jsns_builder.rb +6 -3
  130. data/lib/zm/client/mountpoint/mountpoint_jsns_initializer.rb +25 -24
  131. data/lib/zm/client/mountpoint/mountpoints_builder.rb +1 -1
  132. data/lib/zm/client/mountpoint/mountpoints_collection.rb +13 -9
  133. data/lib/zm/client/mta_queue/mta_queue.rb +6 -2
  134. data/lib/zm/client/mta_queue/mta_queue_jsns_initializer.rb +3 -7
  135. data/lib/zm/client/mta_queue/mta_queues_builder.rb +3 -1
  136. data/lib/zm/client/mta_queue_item/mta_queue_item.rb +5 -3
  137. data/lib/zm/client/mta_queue_item/mta_queue_item_jsns_initializer.rb +1 -5
  138. data/lib/zm/client/mta_queue_item/mta_queue_items_builder.rb +3 -1
  139. data/lib/zm/client/mta_queue_item/mta_queue_items_collection.rb +3 -1
  140. data/lib/zm/client/resource/resource.rb +6 -18
  141. data/lib/zm/client/resource/resource_jsns_builder.rb +20 -13
  142. data/lib/zm/client/resource/resource_jsns_initializer.rb +4 -14
  143. data/lib/zm/client/resource/resources_builder.rb +1 -1
  144. data/lib/zm/client/resource/resources_collection.rb +12 -7
  145. data/lib/zm/client/search_folder/search_folder.rb +13 -29
  146. data/lib/zm/client/search_folder/search_folder_jsns_builder.rb +6 -6
  147. data/lib/zm/client/search_folder/search_folder_jsns_initializer.rb +19 -18
  148. data/lib/zm/client/search_folder/search_folders_builder.rb +1 -1
  149. data/lib/zm/client/search_folder/search_folders_collection.rb +6 -5
  150. data/lib/zm/client/server/server.rb +9 -3
  151. data/lib/zm/client/server/server_accounts_collection.rb +4 -2
  152. data/lib/zm/client/server/server_jsns_initializer.rb +1 -5
  153. data/lib/zm/client/server/servers_builder.rb +1 -1
  154. data/lib/zm/client/server/servers_collection.rb +17 -9
  155. data/lib/zm/client/share/share.rb +5 -3
  156. data/lib/zm/client/share/share_jsns_initializer.rb +15 -14
  157. data/lib/zm/client/share/share_mountpoints_collection.rb +3 -6
  158. data/lib/zm/client/share/shares_builder.rb +1 -1
  159. data/lib/zm/client/share/shares_collection.rb +6 -3
  160. data/lib/zm/client/signature/signature.rb +6 -21
  161. data/lib/zm/client/signature/signature_jsns_builder.rb +12 -7
  162. data/lib/zm/client/signature/signature_jsns_initializer.rb +5 -4
  163. data/lib/zm/client/signature/signatures_builder.rb +1 -1
  164. data/lib/zm/client/signature/signatures_collection.rb +6 -5
  165. data/lib/zm/client/soap_request/request_methods_admin.rb +56 -0
  166. data/lib/zm/client/soap_request/request_methods_mailbox.rb +60 -0
  167. data/lib/zm/client/soap_request/soap_constants.rb +7 -0
  168. data/lib/zm/client/soap_request/soap_context.rb +11 -2
  169. data/lib/zm/client/soap_request.rb +3 -0
  170. data/lib/zm/client/tag/account_object_tags_collection.rb +11 -9
  171. data/lib/zm/client/tag/tag.rb +8 -44
  172. data/lib/zm/client/tag/tag_jsns_builder.rb +4 -2
  173. data/lib/zm/client/tag/tag_jsns_initializer.rb +7 -6
  174. data/lib/zm/client/tag/tags_builder.rb +1 -1
  175. data/lib/zm/client/tag/tags_collection.rb +6 -5
  176. data/lib/zm/client/task/task.rb +6 -5
  177. data/lib/zm/client/task/task_jsns_initializer.rb +28 -27
  178. data/lib/zm/client/task/tasks_builder.rb +1 -1
  179. data/lib/zm/client/task/tasks_collection.rb +2 -2
  180. data/lib/zm/client/token.rb +52 -0
  181. data/lib/zm/client/upload/upload.rb +102 -92
  182. data/lib/zm/client/zm_logger.rb +83 -0
  183. data/lib/zm/client.rb +6 -1
  184. data/lib/zm/modules/base.rb +0 -1
  185. data/lib/zm/modules/belongs_to_folder.rb +6 -7
  186. data/lib/zm/modules/belongs_to_tag.rb +3 -1
  187. data/lib/zm/modules/common/zimbra-attrs.csv +2 -2
  188. data/lib/zm/modules/inspector.rb +2 -2
  189. data/lib/zm/modules/missing_method_static_collection.rb +2 -2
  190. data/lib/zm/support/cache/entry.rb +63 -0
  191. data/lib/zm/support/cache/file_store.rb +141 -0
  192. data/lib/zm/support/cache/null_store.rb +43 -0
  193. data/lib/zm/support/cache/request_strategy.rb +10 -0
  194. data/lib/zm/support/cache/store.rb +197 -0
  195. data/lib/zm/support/cache/strategy.rb +13 -0
  196. data/lib/zm/support/cache.rb +26 -0
  197. data/lib/zm-ruby-client.rb +2 -2
  198. data/zm-ruby-client.gemspec +10 -15
  199. metadata +37 -36
  200. data/lib/zm/client/common/token_metadata.rb +0 -44
  201. data/lib/zm/modules/zm_logger.rb +0 -26
@@ -4,60 +4,24 @@ module Zm
4
4
  module Client
5
5
  # class account tag
6
6
  class Tag < Base::Object
7
- # include Zm::Model::AttributeChangeObserver
7
+ include RequestMethodsMailbox
8
+ include MailboxItemConcern
8
9
 
9
- attr_accessor :id, :name, :color, :rgb
10
-
11
- # define_changed_attributes :name, :color, :rgb
10
+ attr_accessor :name, :color, :rgb
12
11
 
13
12
  def create!
14
- rep = @parent.sacc.invoke(jsns_builder.to_jsns)
13
+ rep = @parent.soap_connector.invoke(build_create)
15
14
  json = rep[:CreateTagResponse][:tag].first
16
15
  TagJsnsInitializer.update(self, json)
17
- @id
18
- end
19
-
20
- def modify!
21
- # return unless color_changed? || rgb_changed?
22
-
23
- @parent.sacc.invoke(jsns_builder.to_update)
24
- true
25
- end
26
-
27
- def update!(hash)
28
- return false if hash.delete_if { |k, v| v.nil? || !respond_to?(k) }.empty?
29
-
30
- do_update!(hash)
31
-
32
- hash.each do |key, value|
33
- update_attribute(key, value)
34
- end
35
-
36
- true
37
- end
38
-
39
- def rename!(new_name)
40
- return false if new_name == @name
41
-
42
- @parent.sacc.invoke(jsns_builder.to_rename(new_name))
43
- @name = new_name
44
- end
45
-
46
- def delete!
47
- return false if @id.nil?
48
-
49
- @parent.sacc.invoke(jsns_builder.to_delete)
50
- @id = nil
16
+ id
51
17
  end
52
18
 
53
19
  private
54
20
 
55
- def do_update!(hash)
56
- @parent.sacc.invoke(jsns_builder.to_patch(hash))
57
- end
58
-
59
21
  def jsns_builder
60
- @jsns_builder ||= TagJsnsBuilder.new(self)
22
+ return @jsns_builder if defined? @jsns_builder
23
+
24
+ @jsns_builder = TagJsnsBuilder.new(self)
61
25
  end
62
26
  end
63
27
  end
@@ -9,7 +9,8 @@ module Zm
9
9
  name: @item.name,
10
10
  color: @item.color,
11
11
  rgb: @item.rgb
12
- }.delete_if { |_, v| v.nil? }
12
+ }
13
+ attrs.compact!
13
14
 
14
15
  soap_request = SoapElement.mail(SoapMailConstants::CREATE_TAG_REQUEST)
15
16
  node_tag = SoapElement.create(SoapConstants::TAG).add_attributes(attrs)
@@ -25,7 +26,8 @@ module Zm
25
26
  id: @item.id,
26
27
  color: @item.color,
27
28
  rgb: @item.rgb
28
- }.reject { |_, v| v.nil? }
29
+ }
30
+ attrs.compact!
29
31
 
30
32
  build(attrs)
31
33
  end
@@ -6,15 +6,16 @@ module Zm
6
6
  class TagJsnsInitializer
7
7
  class << self
8
8
  def create(parent, json)
9
- item = Tag.new(parent)
10
- update(item, json)
9
+ Tag.new(parent).tap do |item|
10
+ update(item, json)
11
+ end
11
12
  end
12
13
 
13
14
  def update(item, json)
14
- item.id = json[:id].to_i
15
- item.name = json[:name]
16
- item.color = json[:color].to_i
17
- item.rgb = json[:rgb]
15
+ item.id = json.delete(:id)
16
+ item.name = json.delete(:name)
17
+ item.color = json.delete(:color)
18
+ item.rgb = json.delete(:rgb)
18
19
 
19
20
  item
20
21
  end
@@ -5,7 +5,7 @@ module Zm
5
5
  # class factory [tags]
6
6
  class TagBuilder < Base::ObjectsBuilder
7
7
  def initialize(parent, json)
8
- super(parent, json)
8
+ super
9
9
  @json_item_key = :tag
10
10
  end
11
11
 
@@ -7,14 +7,15 @@ module Zm
7
7
  def initialize(parent)
8
8
  @child_class = Tag
9
9
  @builder_class = TagBuilder
10
- super(parent)
10
+ super
11
11
  end
12
12
 
13
- private
14
-
15
13
  def make_query
16
- soap_request = SoapElement.mail(SoapMailConstants::GET_TAG_REQUEST)
17
- @parent.sacc.invoke(soap_request)
14
+ @parent.soap_connector.invoke(build_query)
15
+ end
16
+
17
+ def build_query
18
+ SoapElement.mail(SoapMailConstants::GET_TAG_REQUEST)
18
19
  end
19
20
  end
20
21
  end
@@ -7,12 +7,13 @@ module Zm
7
7
  include BelongsToFolder
8
8
  include BelongsToTag
9
9
 
10
- attr_accessor :uid, :priority, :ptst, :percentComplete, :name, :loc, :alarm, :isOrg, :id, :invId, :compNum, :l,
11
- :status, :class, :allDay, :f, :tn, :t, :rev, :s, :d, :md, :ms, :cm, :sf
10
+ attr_accessor :uid, :priority, :ptst, :percentComplete, :name, :loc, :alarm, :isOrg,
11
+ :id, :invId, :compNum, :l, :status, :class, :allDay, :f, :tn, :t, :rev,
12
+ :s, :d, :md, :ms, :cm, :sf
12
13
 
13
- def download(dest_file_path, fmt = 'ics')
14
- uploader = Upload.new(@parent, RestAccountConnector.new)
15
- uploader.download_file(Zm::Client::FolderDefault::ROOT[:path], fmt, [Zm::Client::FolderView::TASK], [@id], dest_file_path)
14
+ def download(dest_file_path, fmt: 'ics')
15
+ uploader = @parent.build_uploader
16
+ uploader.download_file(dest_file_path, id, FolderView::APPOINTMENT, fmt:)
16
17
  end
17
18
 
18
19
  def create!
@@ -6,36 +6,37 @@ module Zm
6
6
  class TaskJsnsInitializer
7
7
  class << self
8
8
  def create(parent, json)
9
- item = Task.new(parent)
10
- update(item, json)
9
+ Task.new(parent).tap do |item|
10
+ update(item, json)
11
+ end
11
12
  end
12
13
 
13
14
  def update(item, json)
14
- item.uid = json[:uid]
15
- item.priority = json[:priority].to_i
16
- item.ptst = json[:ptst]
17
- item.percentComplete = json[:percentComplete].to_f
18
- item.name = json[:name]
19
- item.loc = json[:loc]
20
- item.alarm = json[:alarm]
21
- item.isOrg = json[:isOrg]
22
- item.id = json[:id].to_i
23
- item.invId = json[:invId]
24
- item.compNum = json[:compNum]
25
- item.l = json[:l].to_i
26
- item.status = json[:status]
27
- item.class = json[:class]
28
- item.allDay = json[:allDay]
29
- item.f = json[:f]
30
- item.tn = json[:tn]
31
- item.t = json[:t]
32
- item.rev = json[:rev]
33
- item.s = json[:s]
34
- item.d = json[:d]
35
- item.md = json[:md]
36
- item.ms = json[:ms]
37
- item.cm = json[:cm]
38
- item.sf = json[:sf]
15
+ item.uid = json.delete(:uid)
16
+ item.priority = json.delete(:priority)
17
+ item.ptst = json.delete(:ptst)
18
+ item.percentComplete = json.delete(:percentComplete)
19
+ item.name = json.delete(:name)
20
+ item.loc = json.delete(:loc)
21
+ item.alarm = json.delete(:alarm)
22
+ item.isOrg = json.delete(:isOrg)
23
+ item.id = json.delete(:id).to_i
24
+ item.invId = json.delete(:invId)
25
+ item.compNum = json.delete(:compNum)
26
+ item.l = json.delete(:l).to_i
27
+ item.status = json.delete(:status)
28
+ item.class = json.delete(:class)
29
+ item.allDay = json.delete(:allDay)
30
+ item.f = json.delete(:f)
31
+ item.tn = json.delete(:tn)
32
+ item.t = json.delete(:t)
33
+ item.rev = json.delete(:rev)
34
+ item.s = json.delete(:s)
35
+ item.d = json.delete(:d)
36
+ item.md = json.delete(:md)
37
+ item.ms = json.delete(:ms)
38
+ item.cm = json.delete(:cm)
39
+ item.sf = json.delete(:sf)
39
40
 
40
41
  item
41
42
  end
@@ -5,7 +5,7 @@ module Zm
5
5
  # class factory [tasks]
6
6
  class TasksBuilder < Base::ObjectsBuilder
7
7
  def initialize(parent, json)
8
- super(parent, json)
8
+ super
9
9
  @json_item_key = :task
10
10
  end
11
11
 
@@ -7,7 +7,7 @@ module Zm
7
7
  DEFAULT_QUERY = 'in:tasks'
8
8
 
9
9
  def initialize(parent)
10
- super(parent)
10
+ super
11
11
  @child_class = Task
12
12
  @builder_class = TasksBuilder
13
13
  @type = 'task'
@@ -15,7 +15,7 @@ module Zm
15
15
  end
16
16
 
17
17
  def find_each(offset: 0, limit: 500, &block)
18
- (1970..(Time.now.year + 10)).each do |year|
18
+ (Time.at(0).year..(Time.now.year + 10)).each do |year|
19
19
  @start_at = Time.new(year, 1, 1)
20
20
  @end_at = Time.new(year, 12, 31)
21
21
  @more = true
@@ -0,0 +1,52 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Zm
4
+ module Client
5
+ class Token
6
+ def initialize(str)
7
+ @str = str
8
+ @key_id, @hmac, @encoded = @str.split('_')
9
+ end
10
+
11
+ def to_s
12
+ @str
13
+ end
14
+
15
+ def decoded
16
+ return @decoded if defined? @decoded
17
+
18
+ @decoded = [@encoded].pack('H*')
19
+ end
20
+
21
+ def metadatas
22
+ return @metadatas if defined? @metadatas
23
+
24
+ @metadatas = decoded.split(';').to_h { |part| part.split(/=\d+:/) }.freeze
25
+ end
26
+
27
+ def zimbra_id
28
+ metadatas['id']
29
+ end
30
+
31
+ def admin?
32
+ return @admin if defined? @admin
33
+
34
+ @admin = metadatas['admin'] == '1'
35
+ end
36
+
37
+ def server_version
38
+ metadatas['version']
39
+ end
40
+
41
+ def expire_at
42
+ return @expire_at if defined? @expire_at
43
+
44
+ @expire_at = Time.at(metadatas['exp'].to_f / 1000).freeze
45
+ end
46
+
47
+ def expired?
48
+ expire_at < Time.now
49
+ end
50
+ end
51
+ end
52
+ end
@@ -2,148 +2,158 @@
2
2
 
3
3
  module Zm
4
4
  module Client
5
- # class for upload account file
5
+ # class for upload mailbox file
6
6
  class Upload
7
7
  FMT_TYPES_H = {
8
- 'ics' => ['appointment'],
9
- 'vcard' => ['contact']
8
+ 'ics' => FolderView::APPOINTMENT,
9
+ 'vcard' => FolderView::CONTACT,
10
+ 'eml' => FolderView::MESSAGE,
11
+ 'vcf' => FolderView::CONTACT
10
12
  }.freeze
11
13
 
12
- def initialize(parent, rac = nil)
13
- @parent = parent
14
- @rac = rac || @parent.rac
15
- @rac.cookies("ZM_AUTH_TOKEN=#{@parent.token}")
16
- end
14
+ AUTHORIZED_FOLDER_FMT = %w[zip tgz ics csv tar xml json rss atom html ifb].freeze
17
15
 
18
- def download_file_with_url(url, dest_file_path)
19
- raise ZmError, 'home_url is not defined' if @parent.home_url.nil?
16
+ AUTHORIZED_PARAMS = %i[charset auth fmt id list types emptyname disp resolve query meta csvfmt].freeze
20
17
 
21
- url = File.join(@parent.home_url, url) unless url.start_with?('http')
22
- @rac.download(url, dest_file_path)
23
- end
18
+ AUTHORIZED_RESOLVE = %w[replace modify reset skip].freeze
19
+
20
+ attr_reader :rest_connector
21
+ attr_accessor :charset, :auth, :emptyname, :disp
22
+
23
+ def initialize(base_url, token, is_token_admin: false, **rest_options)
24
+ raise ZmError, 'base_url must to be present' if base_url.nil?
24
25
 
25
- def download_file(folder_path, fmt, types, ids, dest_file_path)
26
- @rac.download(download_file_url(folder_path, fmt, types, ids), dest_file_path)
26
+ @charset = 'UTF-8'
27
+ @auth = 'co'
28
+ @emptyname = 'Empty'
29
+ @disp = 'a'
30
+
31
+ yield(self) if block_given?
32
+
33
+ @base_url = base_url
34
+
35
+ @rest_connector = RestConnector.new(**rest_options) do |conn|
36
+ conn.cookies = if is_token_admin
37
+ "ZM_ADMIN_AUTH_TOKEN=#{token}"
38
+ else
39
+ "ZM_AUTH_TOKEN=#{token}"
40
+ end
41
+ end
27
42
  end
28
43
 
29
- def download_folder(id, fmt, dest_file_path)
30
- @rac.download(download_folder_url(id, fmt), dest_file_path)
44
+ def download_file(dest_file_path, file_id, type, fmt: nil)
45
+ fmt ||= File.extname(dest_file_path)[1..]
46
+ type ||= FMT_TYPES_H[fmt]
47
+
48
+ raise RestError, 'Invalid fmt type' if type.nil? && fmt.nil?
49
+
50
+ remote_url = download_file_url(fmt, type, file_id)
51
+ @rest_connector.download(remote_url, dest_file_path)
31
52
  end
32
53
 
33
- def download_folder_url(id, fmt)
34
- raise ZmError, 'home_url is not defined' if @parent.home_url.nil?
54
+ def read_file(file_id, type, fmt: nil)
55
+ type ||= FMT_TYPES_H[fmt]
35
56
 
36
- url_folder_path = @parent.home_url.dup
57
+ raise RestError, 'Invalid fmt type' if type.nil? && fmt.nil?
37
58
 
38
- h = {
39
- fmt: fmt,
40
- id: id,
41
- emptyname: 'Empty',
42
- charset: 'UTF-8',
43
- auth: 'co',
44
- disp: 'a'
45
- }
59
+ remote_url = download_file_url(fmt, type, file_id)
60
+ @rest_connector.read(remote_url)
61
+ end
62
+
63
+ def download_files(dest_file_path, file_ids, type, fmt: nil)
64
+ fmt ||= File.extname(dest_file_path)[1..]
65
+ type ||= FMT_TYPES_H[fmt]
46
66
 
47
- url_folder_path << '?' << Utils.format_url_params(h)
67
+ raise RestError, 'Invalid fmt type' if type.nil? && fmt.nil?
48
68
 
49
- url_folder_path
69
+ remote_url = download_files_url(fmt, type, file_ids)
70
+ @rest_connector.download(remote_url, dest_file_path)
50
71
  end
51
72
 
52
- def download_file_url(folder_path, fmt, types, ids = [])
53
- raise ZmError, 'home_url is not defined' if @parent.home_url.nil?
73
+ def download_folder(dest_file_path, folder_id, fmt: nil)
74
+ fmt ||= File.extname(dest_file_path)[1..]
54
75
 
55
- url_folder_path = File.join(@parent.home_url, folder_path.to_s)
76
+ raise RestError, 'Unauthorized fmt parameter' unless AUTHORIZED_FOLDER_FMT.include?(fmt)
56
77
 
57
- h = {
58
- fmt: fmt,
59
- types: query_value_types(types, fmt),
60
- emptyname: 'Empty',
61
- charset: 'UTF-8',
62
- auth: 'co',
63
- disp: 'a'
64
- }
78
+ remote_url = download_folder_url(folder_id, fmt)
79
+ @rest_connector.download(remote_url, dest_file_path)
80
+ end
65
81
 
66
- h.merge!(query_ids(ids))
82
+ def send_file(src_file_path, folder_id, fmt: nil, type: nil, resolve: nil)
83
+ fmt ||= File.extname(src_file_path)[1..]
84
+ type ||= FMT_TYPES_H[fmt]
85
+ resolve ||= AUTHORIZED_RESOLVE.first
67
86
 
68
- h.reject! { |_, v| is_blank?(v) }
87
+ raise RestError, 'Unauthorized resolve parameter' unless AUTHORIZED_RESOLVE.include?(resolve)
69
88
 
70
- url_folder_path << '?' << Utils.format_url_params(h)
89
+ raise RestError, 'Invalid fmt type' if type.nil? && fmt.nil?
71
90
 
72
- url_folder_path
91
+ remote_url = send_file_url(folder_id, fmt:, type:, resolve:)
92
+ @rest_connector.upload(remote_url, src_file_path)
73
93
  end
74
94
 
75
- def send_file(folder_path, fmt, types, resolve, src_file_path)
76
- @rac.upload(send_file_url(folder_path, fmt, types, resolve), src_file_path)
95
+ def upload_attachment(src_file_path)
96
+ @rest_connector.upload(upload_attachment_url, src_file_path)
77
97
  end
78
98
 
79
- def send_file_url(folder_path, fmt, types, resolve)
80
- raise ZmError, 'home_url is not defined' if @parent.home_url.nil?
99
+ def build_params(**params)
100
+ params.select! { |k, _| AUTHORIZED_PARAMS.include?(k) }
81
101
 
82
- # resolve=[modfy|replace|reset|skip]
83
- url_folder_path = File.join(@parent.home_url, Utils.format_url_path(folder_path.to_s))
102
+ params[:charset] ||= @charset
103
+ params[:auth] ||= @auth
104
+ params[:emptyname] ||= @emptyname
105
+ params[:disp] ||= @disp
84
106
 
85
- h = {
86
- fmt: fmt,
87
- types: query_value_types(types, fmt),
88
- resolve: resolve,
89
- auth: 'co'
90
- }
91
-
92
- h.reject! { |_, v| is_blank?(v) }
107
+ params.compact!
93
108
 
94
- url_folder_path << '?' << Utils.format_url_params(h)
109
+ params
110
+ end
95
111
 
96
- url_folder_path
112
+ def format_url_params(params)
113
+ params.map { |k, v| "#{k}=#{v}" }.join('&')
97
114
  end
98
115
 
99
- def send_attachment(src_file_path)
100
- str = upload_attachment(src_file_path)
101
- AttachmentResponse.new(str)
116
+ def format_url(url, str_params)
117
+ return url if str_params.nil? || str_params.empty?
118
+
119
+ "#{url}?#{str_params}"
102
120
  end
103
121
 
104
- def upload_attachment(src_file_path)
105
- @rac.upload(upload_attachment_url, src_file_path)
122
+ def download_file_with_url(url, dest_file_path)
123
+ url = File.join(@base_url, url) unless url.start_with?('http')
124
+ @rest_connector.download(url, dest_file_path)
106
125
  end
107
126
 
108
- def upload_attachment_url
109
- h = {
110
- fmt: 'extended,raw'
111
- }
127
+ def download_folder_url(folder_id, fmt)
128
+ h = build_params(fmt:, id: folder_id)
112
129
 
113
- File.join(@parent.public_url, 'service/upload') << '?' << Utils.format_url_params(h)
130
+ format_url(@base_url, format_url_params(h))
114
131
  end
115
132
 
116
- def query_ids(ids)
117
- return {} if ids.nil?
118
- return { id: ids } unless ids.is_a?(Array)
119
- return { id: ids.first } if ids.length == 1
133
+ def download_file_url(fmt, type, id)
134
+ h = build_params(fmt:, types: type, id:)
120
135
 
121
- { list: ids.join(',') }
136
+ format_url(@base_url, format_url_params(h))
122
137
  end
123
138
 
124
- def query_value_types(types, fmt)
125
- types = FMT_TYPES_H[fmt] if types.nil?
139
+ def download_files_url(fmt, type, ids)
140
+ h = build_params(fmt:, types: type, list: ids.join(','))
126
141
 
127
- types = [types] unless types.is_a?(Array)
128
- types.join(',')
142
+ format_url(@base_url, format_url_params(h))
129
143
  end
130
144
 
131
- def is_blank?(v)
132
- return false if v.is_a?(Numeric)
145
+ def send_file_url(folder_id, fmt:, type:, resolve:)
146
+ h = build_params(fmt:, types: type, resolve:, id: folder_id)
133
147
 
134
- v.nil? || v.empty?
148
+ format_url(@base_url, format_url_params(h))
135
149
  end
136
- end
137
150
 
138
- # class to parse upload attachment response
139
- class AttachmentResponse
140
- def initialize(str)
141
- @str = str
142
- @str_h = JSON.parse(str[str.index('['), str.length], symbolize_names: true).first
143
- end
151
+ def upload_attachment_url
152
+ h = {
153
+ fmt: 'extended,raw'
154
+ }
144
155
 
145
- def aid
146
- @str_h[:aid]
156
+ File.join(@base_url, 'service/upload') << '?' << format_url_params(h)
147
157
  end
148
158
  end
149
159
  end
@@ -0,0 +1,83 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'logger'
4
+
5
+ module Zm
6
+ module Client
7
+ class ZmLogger < ::Logger
8
+ def initialize(*args, **kwargs)
9
+ super
10
+ @formatter ||= ZmFormatter.new
11
+ end
12
+
13
+ def colorize!
14
+ extend(ZmLoggerColorized)
15
+ end
16
+
17
+ class ZmFormatter < ::Logger::Formatter
18
+ Format = "[%s] %5s : %s\n"
19
+
20
+ def call(severity, time, _, msg)
21
+ format(Format, format_datetime(time), severity, msg2str(msg))
22
+ end
23
+ end
24
+
25
+ module ZmLoggerColorized
26
+ # ANSI sequence modes
27
+ MODES = {
28
+ clear: 0,
29
+ bold: 1,
30
+ italic: 3,
31
+ underline: 4
32
+ }.freeze
33
+
34
+ # ANSI sequence colors
35
+ BLACK = "\e[30m"
36
+ RED = "\e[31m"
37
+ GREEN = "\e[32m"
38
+ YELLOW = "\e[33m"
39
+ BLUE = "\e[34m"
40
+ MAGENTA = "\e[35m"
41
+ CYAN = "\e[36m"
42
+ WHITE = "\e[37m"
43
+
44
+ def severity_color(severity)
45
+ case severity
46
+ when 'DEBUG'
47
+ CYAN
48
+ when 'INFO'
49
+ GREEN
50
+ when 'WARN'
51
+ YELLOW
52
+ when 'ERROR'
53
+ RED
54
+ when 'FATAL'
55
+ mode_code(:bold)
56
+ when 'ANY'
57
+ mode_code(:clear)
58
+ else
59
+ mode_code(:clear)
60
+ end
61
+ end
62
+
63
+ def mode_code(mode)
64
+ mode = mode.to_sym if mode.is_a?(String)
65
+ return unless (value = MODES[mode])
66
+
67
+ "\e[#{value}m"
68
+ end
69
+
70
+ def colorize_message(severity, str)
71
+ "#{severity_color(severity)}#{str}#{mode_code(:clear)}"
72
+ end
73
+
74
+ def format_message(severity, datetime, progname, msg)
75
+ colorize_message(
76
+ severity,
77
+ super
78
+ )
79
+ end
80
+ end
81
+ end
82
+ end
83
+ end
data/lib/zm/client.rb CHANGED
@@ -1,8 +1,13 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require 'date'
4
+
5
+ require 'zm/client/soap_request'
3
6
  require 'zm/modules/inspector'
7
+ require 'zm/support/cache'
8
+ require 'zm/client/zm_logger'
4
9
  require 'zm/client/version'
5
10
  require 'zm/client/constant'
11
+ require 'zm/client/token'
6
12
  require 'zm/client/base'
7
13
  require 'zm/client/cluster'
8
- require 'zm/client/soap_request'
@@ -1,6 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require 'zm/modules/zm_logger'
4
3
  require 'zm/modules/zm_model'
5
4
  require 'zm/modules/missing_method_static_collection'
6
5