Module: ZimbraRestApi::ZimbraObject::ClassMethods
- Defined in:
- lib/zimbra_rest_api/zimbra_object.rb
Overview
Doc placeholder
Instance Method Summary collapse
- #all(query = {}, object = nil) ⇒ Object
- #build_search_hash(query = {}) ⇒ Object
- #count(query = {}, object = nil) ⇒ Object
- #create(params = {}, object = nil) ⇒ Object
- #find(query, object = nil) ⇒ Object
- #get_sort_ops(query) ⇒ Object
- #get_zimbra_object(object) ⇒ Object
- #hash_to_ldap(query = {}) ⇒ Object
- #search(object, query) ⇒ Object
- #zimbra_attrs_to_load ⇒ Object
- #zimbra_attrs_to_load=(array) ⇒ Object
Instance Method Details
#all(query = {}, object = nil) ⇒ Object
69 70 71 72 73 74 75 76 77 78 |
# File 'lib/zimbra_rest_api/zimbra_object.rb', line 69 def all(query = {}, object = nil) zimbra_object = get_zimbra_object(object) search_result = search(zimbra_object, query) return nil if search_result.nil? raw_els = search_result[:results] search_result[:results] = raw_els.nil? ? [] : raw_els.map do |o| new(o) end search_result end |
#build_search_hash(query = {}) ⇒ Object
125 126 127 128 129 130 131 132 133 134 |
# File 'lib/zimbra_rest_api/zimbra_object.rb', line 125 def build_search_hash(query = {}) query_dup = query.clone = get_sort_ops(query_dup) count_only = query_dup.delete('count_only') || query_dup.delete(:count_only) query_hash = { domain: query_dup.delete('domain') || query_dup.delete(:domain), query: hash_to_ldap(query_dup) } query_hash.merge().merge(count_only: count_only) end |
#count(query = {}, object = nil) ⇒ Object
80 81 82 83 84 85 86 87 |
# File 'lib/zimbra_rest_api/zimbra_object.rb', line 80 def count(query = {}, object = nil) query ||= {} query[:count_only] = true query['per_page'] = 0 zimbra_object = get_zimbra_object(object) result = search(zimbra_object, query) !result.nil? ? result : { count: 0 } end |
#create(params = {}, object = nil) ⇒ Object
89 90 91 92 93 94 |
# File 'lib/zimbra_rest_api/zimbra_object.rb', line 89 def create(params = {}, object = nil) zimbra_object = get_zimbra_object(object) name = params.delete('name') result = zimbra_object.create(name, params) new(result) end |
#find(query, object = nil) ⇒ Object
96 97 98 99 100 101 102 103 104 |
# File 'lib/zimbra_rest_api/zimbra_object.rb', line 96 def find(query, object = nil) zimbra_object = get_zimbra_object(object) if UUID.validate(query) result = zimbra_object.find_by_id(query) else result = zimbra_object.find_by_name(query) end result.nil? ? nil : new(result) end |
#get_sort_ops(query) ⇒ Object
136 137 138 139 140 141 142 143 144 |
# File 'lib/zimbra_rest_api/zimbra_object.rb', line 136 def get_sort_ops(query) page = query.delete('page') || 0 limit = query.delete('per_page') || 0 max_results = query.delete('max_results') || ZimbraRestApi.zimbra_max_results attrs = query.delete('attrs') || nil offset = page.to_i <= 1 ? 0 : ((page.to_i - 1) * limit.to_i) { limit: limit.to_i, offset: offset.to_i, attrs: attrs, max_results: max_results } end |
#get_zimbra_object(object) ⇒ Object
106 107 108 109 110 |
# File 'lib/zimbra_rest_api/zimbra_object.rb', line 106 def get_zimbra_object(object) object ||= self.is_a?(Class) ? name : self.class.name object.gsub!(/ZimbraRestApi::/, '') "Zimbra::#{object.camelize}".constantize end |
#hash_to_ldap(query = {}) ⇒ Object
146 147 148 149 150 151 152 153 154 155 |
# File 'lib/zimbra_rest_api/zimbra_object.rb', line 146 def hash_to_ldap(query = {}) raw_filter = query.delete('raw_ldap_filter').to_s if query.delete('inverse_filter') result = query.map { |k, v| "(!(#{k}=#{v}))" }.join('') else result = query.map { |k, v| "(#{k}=#{v})" }.join('') end raw_filter << "(&#{result})" raw_filter end |
#search(object, query) ⇒ Object
112 113 114 115 116 117 118 119 120 121 122 123 |
# File 'lib/zimbra_rest_api/zimbra_object.rb', line 112 def search(object, query) zimbra_type = object.name.split(/::/).last.downcase search_hash = build_search_hash(query) search_hash.merge!(type: zimbra_type) query = search_hash.delete(:query) begin Zimbra::Directory.search(query, search_hash) rescue Zimbra::HandsoapErrors::SOAPFault => e msg = 'number of results exceeded the limit: too many search results returned' raise ZimbraRestApi::TO_MANY_RESULTS if e. == msg end end |
#zimbra_attrs_to_load ⇒ Object
164 165 166 167 168 169 |
# File 'lib/zimbra_rest_api/zimbra_object.rb', line 164 def zimbra_attrs_to_load klass_name = self.name.split(/::/)[1] klass = "Zimbra::#{klass_name}".constantize return [] if klass.zimbra_attrs_to_load.nil? klass.zimbra_attrs_to_load end |
#zimbra_attrs_to_load=(array) ⇒ Object
157 158 159 160 161 162 |
# File 'lib/zimbra_rest_api/zimbra_object.rb', line 157 def zimbra_attrs_to_load=(array) klass_name = self.name.split(/::/)[1] klass = "Zimbra::#{klass_name}".constantize fail(ArgumentError, 'Must be an array') unless array.is_a?Array klass.zimbra_attrs_to_load = array end |