Refactor Import::Exchange::Folder#children
This commit is contained in:
parent
97f55bcd83
commit
370b1a2645
1 changed files with 12 additions and 16 deletions
|
@ -5,6 +5,8 @@ module Import
|
||||||
class Folder
|
class Folder
|
||||||
include ::Mixin::RailsLogger
|
include ::Mixin::RailsLogger
|
||||||
|
|
||||||
|
DEFAULT_ROOTS = %i[root msgfolderroot publicfoldersroot].freeze
|
||||||
|
|
||||||
def initialize(connection)
|
def initialize(connection)
|
||||||
@connection = connection
|
@connection = connection
|
||||||
@lookup_map = {}
|
@lookup_map = {}
|
||||||
|
@ -27,22 +29,16 @@ module Import
|
||||||
end
|
end
|
||||||
|
|
||||||
def all
|
def all
|
||||||
# request folders only if neccessary and store the result
|
@all ||= children(*DEFAULT_ROOTS)
|
||||||
@all ||= children(%i[root msgfolderroot publicfoldersroot])
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def children(parent_identifiers)
|
def children(*parents)
|
||||||
parent_identifiers.each_with_object([]) do |parent_identifier, result|
|
return [] if parents.empty?
|
||||||
|
|
||||||
child_folders = request_children(parent_identifier)
|
direct_descendants = parents.map(&method(:request_children))
|
||||||
|
.flatten.uniq.compact
|
||||||
|
|
||||||
next if child_folders.blank?
|
direct_descendants | children(*direct_descendants)
|
||||||
|
|
||||||
child_folder_ids = child_folders.collect(&:id)
|
|
||||||
child_folders += children(child_folder_ids)
|
|
||||||
|
|
||||||
result.concat(child_folders)
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def display_path(folder)
|
def display_path(folder)
|
||||||
|
@ -65,11 +61,11 @@ module Import
|
||||||
|
|
||||||
private
|
private
|
||||||
|
|
||||||
def request_children(parent_identifier)
|
def request_children(parent)
|
||||||
@connection.folders(root: parent_identifier)
|
parent = parent.id if parent.respond_to?(:id) # type coercion
|
||||||
|
@connection.folders(root: parent)
|
||||||
rescue Viewpoint::EWS::EwsFolderNotFound => e
|
rescue Viewpoint::EWS::EwsFolderNotFound => e
|
||||||
logger.warn(e)
|
logger.warn(e) && return
|
||||||
nil
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in a new issue