Class: ActiveMailbox::Folder
- Inherits:
-
Object
- Object
- ActiveMailbox::Folder
- Includes:
- Comparable
- Defined in:
- lib/active_mailbox/folder.rb
Overview
ActiveMailbox::Folder represents an Asterisk mailbox Folder, such as INBOX, or Old. They contain the actuall messages for a mailbox.
Instance Attribute Summary collapse
-
#mailbox ⇒ Object
readonly
Returns the value of attribute mailbox.
-
#path ⇒ Object
readonly
Returns the value of attribute path.
-
#reload_messages ⇒ Object
Returns the value of attribute reload_messages.
Instance Method Summary collapse
-
#<=>(other) ⇒ Object
Compare base on path.
-
#clean_ghosts!(autosort = true) ⇒ Object
Clean all ‘ghost’ Messages in this Folder.
-
#clean_stale!(autosort = true) ⇒ Object
Destroy Messages in this Folder that are older than 30 days.
-
#count ⇒ Object
(also: #size)
Returns number of messages in this folder.
-
#destroy! ⇒ Object
Destroy this Folder.
-
#initialize(path, mailbox) ⇒ Folder
constructor
Create a new Folder object.
-
#messages(reload = false) ⇒ Object
An array of Message objects in this folder.
-
#name ⇒ Object
The name of this folder.
-
#purge! ⇒ Object
Destroy all messages/info files in this folder.
-
#sort! ⇒ Object
Sort messages in this folder.
Constructor Details
#initialize(path, mailbox) ⇒ Folder
Create a new Folder object
17 18 19 20 21 22 23 |
# File 'lib/active_mailbox/folder.rb', line 17 def initialize(path, mailbox) unless File.exists?(path) raise ActiveMailbox::Errors::FolderNotFound, "#{path} does not exist" end @mailbox = mailbox @path = path end |
Instance Attribute Details
#mailbox ⇒ Object (readonly)
Returns the value of attribute mailbox.
10 11 12 |
# File 'lib/active_mailbox/folder.rb', line 10 def mailbox @mailbox end |
#path ⇒ Object (readonly)
Returns the value of attribute path.
10 11 12 |
# File 'lib/active_mailbox/folder.rb', line 10 def path @path end |
#reload_messages ⇒ Object
Returns the value of attribute reload_messages.
12 13 14 |
# File 'lib/active_mailbox/folder.rb', line 12 def @reload_messages end |
Instance Method Details
#<=>(other) ⇒ Object
Compare base on path
28 29 30 |
# File 'lib/active_mailbox/folder.rb', line 28 def <=>(other) path <=> other.path end |
#clean_ghosts!(autosort = true) ⇒ Object
Clean all ‘ghost’ Messages in this Folder
A ‘ghost’ voicemail occurs when the .wav file does not exist. Asterisk sees the info (txt) file and thinks a voicemail exists, then plays nothing as the wav is not found.
At the same time, you might run into issues if a wav exists, but not the txt file.
117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 |
# File 'lib/active_mailbox/folder.rb', line 117 def clean_ghosts!(autosort = true) Dir.chdir(@path) do .each do |file| txt = "#{Dir.pwd}/#{file}.txt" wav = "#{Dir.pwd}/#{file}.wav" txt_exists = File.exists?(txt) wav_exists = File.exists?(wav) unless txt_exists && wav_exists File.unlink(txt) if txt_exists File.unlink(wav) if wav_exists end end end sort! if autosort end |
#clean_stale!(autosort = true) ⇒ Object
Destroy Messages in this Folder that are older than 30 days
138 139 140 141 142 143 |
# File 'lib/active_mailbox/folder.rb', line 138 def clean_stale!(autosort = true) .each do || .destroy! if .stale? end sort! if autosort end |
#count ⇒ Object Also known as: size
Returns number of messages in this folder
59 60 61 |
# File 'lib/active_mailbox/folder.rb', line 59 def count .count end |
#destroy! ⇒ Object
Destroy this Folder
148 149 150 |
# File 'lib/active_mailbox/folder.rb', line 148 def destroy! FileUtils.rm_rf(@path) end |
#messages(reload = false) ⇒ Object
An array of Message objects in this folder
35 36 37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/active_mailbox/folder.rb', line 35 def (reload = false) if reload or @reload_messages or ! defined?(@messages) @messages = [] Dir.chdir(@path) do Dir["*.txt"].each do |txt| @messages << Message.new("#{Dir.pwd}/#{txt}", self) end end end @messages ensure @reload_messages = false end |
#name ⇒ Object
The name of this folder
52 53 54 |
# File 'lib/active_mailbox/folder.rb', line 52 def name @name ||= path.split('/').last end |
#purge! ⇒ Object
Destroy all messages/info files in this folder
97 98 99 100 101 102 103 104 105 |
# File 'lib/active_mailbox/folder.rb', line 97 def purge! Dir.chdir(@path) do Dir["*"].each do |file| File.unlink(file) end end ensure @reload_messages = true end |
#sort! ⇒ Object
Sort messages in this folder
Eg:
Before Sort: msg0002, msg0005, msg0010, msg0020
After Sort: msg0000, msg0001, msg0002, msg0003
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 |
# File 'lib/active_mailbox/folder.rb', line 71 def sort! unless .size == .last.number + 1 renamer = lambda do |old_file, new_file| %w[wav txt].each do |ext| File.rename("#{old_file}.#{ext}", "#{new_file}.#{ext}") end end .each_with_index.map do |, index| old_file = .path tmp_file = old_file.sub(/msg[0-9]{4}/, 'temp_msg%04d' % index) renamer.call(old_file, tmp_file) [old_file, tmp_file] end.each do |old_file, tmp_file| new_file = tmp_file.sub('temp_msg', 'msg') renamer.call(tmp_file, new_file) end end ensure @reload_messages = true end |