Class: Imapcli::Mailbox
- Inherits:
-
Object
- Object
- Imapcli::Mailbox
- Defined in:
- lib/imapcli/mailbox.rb
Overview
In IMAP speak, a mailbox is what one would commonly call a ‘folder’
Instance Attribute Summary collapse
-
#children ⇒ Object
readonly
Returns the value of attribute children.
-
#imap_mailbox_list ⇒ Object
readonly
Returns the value of attribute imap_mailbox_list.
-
#level ⇒ Object
readonly
Returns the value of attribute level.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#options ⇒ Object
Returns the value of attribute options.
-
#stats ⇒ Object
readonly
Returns the value of attribute stats.
Class Method Summary collapse
-
.consolidate(list) ⇒ Object
Consolidates a list of mailboxes: If a mailbox is a sub-mailbox of another one, the mailbox is removed from the list.
Instance Method Summary collapse
- #[](mailbox) ⇒ Object
-
#add_mailbox(imap_mailbox_list) ⇒ Object
Adds a sub mailbox designated by the
nameof a Net::IMAP::MailboxList. -
#add_mailbox_list(array_of_mailbox_list_items) ⇒ Object
Add a list of mailboxes as returned by Net::IMAP#list.
-
#collect_stats(client, max_level = nil) {|@stats| ... } ⇒ Object
Collects statistics for this mailbox and the subordinate mailboxes up to a given level.
-
#contains?(other_mailbox) ⇒ Boolean
Returns true if this mailbox contains a given other mailbox.
-
#count(max_level = nil) ⇒ Object
Counts all sub mailboxes recursively.
-
#find_sub_mailbox(relative_name, delimiter) ⇒ Object
Attempts to locate and retrieve a sub mailbox.
- #full_name ⇒ Object
-
#get_max_level ⇒ Object
Determines the maximum level in the mailbox tree.
- #has_children? ⇒ Boolean
-
#initialize(mailbox_list_items = nil, options = {}) ⇒ Mailbox
constructor
Creates a new root Mailbox object and optionally adds sub mailboxes from an array of Net::IMAP::MailboxList items.
-
#is_imap_mailbox? ⇒ Boolean
Determines if this mailbox represents a dedicated IMAP mailbox with an associated Net::IMAP::MailboxList structure.
- #is_root? ⇒ Boolean
-
#to_list(max_level = nil) ⇒ Object
Converts the mailbox tree to a flat list.
Constructor Details
#initialize(mailbox_list_items = nil, options = {}) ⇒ Mailbox
Creates a new root Mailbox object and optionally adds sub mailboxes from an array of Net::IMAP::MailboxList items.
9 10 11 12 13 14 |
# File 'lib/imapcli/mailbox.rb', line 9 def initialize(mailbox_list_items = nil, = {}) @level = 0 @children = {} = add_mailbox_list(mailbox_list_items) if mailbox_list_items end |
Instance Attribute Details
#children ⇒ Object (readonly)
Returns the value of attribute children.
5 6 7 |
# File 'lib/imapcli/mailbox.rb', line 5 def children @children end |
#imap_mailbox_list ⇒ Object (readonly)
Returns the value of attribute imap_mailbox_list.
5 6 7 |
# File 'lib/imapcli/mailbox.rb', line 5 def imap_mailbox_list @imap_mailbox_list end |
#level ⇒ Object
Returns the value of attribute level.
5 6 7 |
# File 'lib/imapcli/mailbox.rb', line 5 def level @level end |
#name ⇒ Object
Returns the value of attribute name.
5 6 7 |
# File 'lib/imapcli/mailbox.rb', line 5 def name @name end |
#options ⇒ Object
Returns the value of attribute options.
4 5 6 |
# File 'lib/imapcli/mailbox.rb', line 4 def end |
#stats ⇒ Object (readonly)
Returns the value of attribute stats.
5 6 7 |
# File 'lib/imapcli/mailbox.rb', line 5 def stats @stats end |
Class Method Details
.consolidate(list) ⇒ Object
Consolidates a list of mailboxes: If a mailbox is a sub-mailbox of another one, the mailbox is removed from the list.
138 139 140 141 142 |
# File 'lib/imapcli/mailbox.rb', line 138 def self.consolidate(list) list.reject do |mailbox| list.any? { |parent| parent.contains? mailbox } end.uniq end |
Instance Method Details
#[](mailbox) ⇒ Object
16 17 18 |
# File 'lib/imapcli/mailbox.rb', line 16 def [](mailbox) @children[mailbox] end |
#add_mailbox(imap_mailbox_list) ⇒ Object
Adds a sub mailbox designated by the name of a Net::IMAP::MailboxList.
69 70 71 72 |
# File 'lib/imapcli/mailbox.rb', line 69 def add_mailbox(imap_mailbox_list) return unless imap_mailbox_list&.name&.length > 0 recursive_add(0, imap_mailbox_list, imap_mailbox_list.name) end |
#add_mailbox_list(array_of_mailbox_list_items) ⇒ Object
Add a list of mailboxes as returned by Net::IMAP#list.
64 65 66 |
# File 'lib/imapcli/mailbox.rb', line 64 def add_mailbox_list(array_of_mailbox_list_items) array_of_mailbox_list_items.sort_by { |m| m.name.downcase }.each { |i| add_mailbox i } end |
#collect_stats(client, max_level = nil) {|@stats| ... } ⇒ Object
Collects statistics for this mailbox and the subordinate mailboxes up to a given level.
If level is nil, all sub mailboxes are analyzed as well.
If a block is given, it is called with the Imapcli::Stats object for this mailbox.
106 107 108 109 110 111 112 113 114 115 116 117 |
# File 'lib/imapcli/mailbox.rb', line 106 def collect_stats(client, max_level = nil) return if @stats if full_name # proceed only if this is a mailbox of its own @stats = Stats.new(client.(full_name)) end yield @stats if block_given? if max_level.nil? || level < max_level @children.values.each do |child| child.collect_stats(client, max_level) { |child_stats| yield child_stats} end end end |
#contains?(other_mailbox) ⇒ Boolean
Returns true if this mailbox contains a given other mailbox.
75 76 77 78 79 |
# File 'lib/imapcli/mailbox.rb', line 75 def contains?(other_mailbox) @children.values.any? do |child| child == other_mailbox || child.contains?(other_mailbox) end end |
#count(max_level = nil) ⇒ Object
Counts all sub mailboxes recursively.
The result includes the current mailbox.
33 34 35 36 37 38 39 40 |
# File 'lib/imapcli/mailbox.rb', line 33 def count(max_level = nil) sum = 1 if max_level.nil? || level < max_level @children.values.inject(sum) do |count, child| count + child.count(max_level) end end end |
#find_sub_mailbox(relative_name, delimiter) ⇒ Object
Attempts to locate and retrieve a sub mailbox.
Returns nil of none exists with the given name. Name must be relative to the current mailbox.
85 86 87 88 89 90 91 92 93 94 95 96 97 |
# File 'lib/imapcli/mailbox.rb', line 85 def find_sub_mailbox(relative_name, delimiter) if relative_name sub_mailbox_name, subs_subs = relative_name.split(delimiter, 2) key = normalize_key(sub_mailbox_name, level) if sub_mailbox = @children[key] sub_mailbox.find_sub_mailbox(subs_subs, delimiter) else nil # no matching sub mailbox found, stop searching the tree end else self end end |
#full_name ⇒ Object
51 52 53 |
# File 'lib/imapcli/mailbox.rb', line 51 def full_name imap_mailbox_list&.name end |
#get_max_level ⇒ Object
Determines the maximum level in the mailbox tree
43 44 45 46 47 48 49 |
# File 'lib/imapcli/mailbox.rb', line 43 def get_max_level if has_children? @children.values.map { |child| child.get_max_level }.max else level end end |
#has_children? ⇒ Boolean
55 56 57 |
# File 'lib/imapcli/mailbox.rb', line 55 def has_children? @children.length > 0 end |
#is_imap_mailbox? ⇒ Boolean
Determines if this mailbox represents a dedicated IMAP mailbox with an associated Net::IMAP::MailboxList structure.
22 23 24 |
# File 'lib/imapcli/mailbox.rb', line 22 def is_imap_mailbox? not imap_mailbox_list.nil? end |
#is_root? ⇒ Boolean
26 27 28 |
# File 'lib/imapcli/mailbox.rb', line 26 def is_root? name.respond_to?(:empty?) ? !!name.empty? : !name end |
#to_list(max_level = nil) ⇒ Object
Converts the mailbox tree to a flat list.
Mailbox objects that do not represent IMAP mailboxes (such as the root mailbox) are not included.
123 124 125 126 127 128 129 130 131 132 |
# File 'lib/imapcli/mailbox.rb', line 123 def to_list(max_level = nil) me = is_imap_mailbox? ? [self] : [] if max_level.nil? || level < max_level @children.values.inject(me) do |ary, child| ary + child.to_list(max_level) end.sort_by { |e| e.full_name } else me end end |