Class: ActiveMailbox::Mailbox
- Inherits:
-
Object
- Object
- ActiveMailbox::Mailbox
- Includes:
- Comparable
- Defined in:
- lib/active_mailbox/mailbox.rb
Overview
The ActiveMailbox::Mailbox class represents a top-level Asterisk mailbox. A mailbox contains folders, which contain the actual voicemails.
Constant Summary collapse
- ValidGreetings =
Greetings that can be altered by ActiveMailbox
[:unavail, :temp, :greet]
Class Attribute Summary collapse
-
.current_mailbox ⇒ Object
The Mailbox currently in use.
Instance Attribute Summary collapse
-
#context ⇒ Object
readonly
Returns the value of attribute context.
-
#mailbox ⇒ Object
readonly
Returns the value of attribute mailbox.
Class Method Summary collapse
-
.find(mailbox, context = nil) {|current_mailbox| ... } ⇒ Object
(also: [])
Find mailbox.
Instance Method Summary collapse
-
#<=>(other) ⇒ Object
Compare based on path name.
-
#clean_ghosts!(autosort = true) ⇒ Object
Deletes ‘ghost’ Messages from all Folders.
-
#clean_stale!(autosort = true) ⇒ Object
Deletes Messages older than 30 days from all Folders.
-
#current_greeting ⇒ Object
The greeting Asterisk will play.
-
#delete_busy_greeting! ⇒ Object
Delete busy.wav.
-
#delete_greeting!(greeting = :unavail) ⇒ Object
Delete greeting.
-
#delete_temp_greeting! ⇒ Object
Delete temp.wav.
-
#delete_unavail_greeting! ⇒ Object
Delete unavail.wav.
-
#destroy! ⇒ Object
Destroy this Mailbox and all messages/greetings.
-
#folders(reload = false) ⇒ Object
A hash/struct of folders in this Mailbox.
-
#greeting_path(greeting = :unavail) ⇒ Object
Path to greeting.
-
#initialize(mailbox, context) ⇒ Mailbox
constructor
Create a new Mailbox object.
-
#method_missing(method_name, *args, &block) ⇒ Object
Use method missing to simulate accessors for folders.
-
#path ⇒ Object
The full filepath to this Mailbox.
-
#purge! ⇒ Object
Destroy all Messages in this mailbox, but leave Mailbox, Folders, and greetings intact.
-
#respond_to?(method_name) ⇒ Boolean
Returns true if method_name is a folder name, or calls super.
-
#sort! ⇒ Object
Sort all Messages in all Folders.
-
#total_messages ⇒ Object
Return total number of messages in every folder.
Constructor Details
#initialize(mailbox, context) ⇒ Mailbox
Create a new Mailbox object
51 52 53 54 55 56 57 58 |
# File 'lib/active_mailbox/mailbox.rb', line 51 def initialize(mailbox, context) @context = context.to_s @mailbox = mailbox.to_s unless File.exists?(mailbox_path) raise ActiveMailbox::Errors::MailboxNotFound, "`#{mailbox_path}` does not exist" end end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method_name, *args, &block) ⇒ Object
Use method missing to simulate accessors for folders
Eg: self.inbox is the same as self.folders
72 73 74 75 76 77 78 79 |
# File 'lib/active_mailbox/mailbox.rb', line 72 def method_missing(method_name, *args, &block) meth = method_name.to_s.downcase.gsub(/\W/, '_').to_sym if folders.has_key?(meth) folders[meth] else super end end |
Class Attribute Details
.current_mailbox ⇒ Object
The Mailbox currently in use
19 20 21 |
# File 'lib/active_mailbox/mailbox.rb', line 19 def current_mailbox @current_mailbox end |
Instance Attribute Details
#context ⇒ Object (readonly)
Returns the value of attribute context.
46 47 48 |
# File 'lib/active_mailbox/mailbox.rb', line 46 def context @context end |
#mailbox ⇒ Object (readonly)
Returns the value of attribute mailbox.
46 47 48 |
# File 'lib/active_mailbox/mailbox.rb', line 46 def mailbox @mailbox end |
Class Method Details
.find(mailbox, context = nil) {|current_mailbox| ... } ⇒ Object Also known as: []
Find mailbox
On a default Asterisk installation, this would be /var/spool/asterisk/voicemail/CONTEXT/MAILBOX
Note: if context is not provided, it will be set to mailbox[1, 3],
which is the area code (NPA) on 11 digit phone numbers
Usage:
find('15183332220', 'Default') # Specify context 'Default'
find('15183332220') # Set context to '518' automatically
34 35 36 37 38 39 40 41 42 |
# File 'lib/active_mailbox/mailbox.rb', line 34 def find(mailbox, context = nil) if context self.current_mailbox = new(mailbox, context) else self.current_mailbox = new(mailbox, mailbox.to_s[1, 3]) end yield(current_mailbox) if block_given? current_mailbox end |
Instance Method Details
#<=>(other) ⇒ Object
Compare based on path name
63 64 65 |
# File 'lib/active_mailbox/mailbox.rb', line 63 def <=>(other) mailbox <=> other.mailbox end |
#clean_ghosts!(autosort = true) ⇒ Object
Deletes ‘ghost’ Messages from all Folders
See ActiveMailbox::Folder#clean_ghosts! for info on ‘ghost’ voicemails in Asterisk
204 205 206 207 208 |
# File 'lib/active_mailbox/mailbox.rb', line 204 def clean_ghosts!(autosort = true) folders.each do |name, folder| folder.clean_ghosts!(autosort) end end |
#clean_stale!(autosort = true) ⇒ Object
Deletes Messages older than 30 days from all Folders
213 214 215 216 217 |
# File 'lib/active_mailbox/mailbox.rb', line 213 def clean_stale!(autosort = true) folders.each do |name, folder| folder.clean_stale!(autosort) end end |
#current_greeting ⇒ Object
The greeting Asterisk will play
155 156 157 158 159 160 161 162 |
# File 'lib/active_mailbox/mailbox.rb', line 155 def current_greeting case when greeting_exists?(:temp) @current_greeting = greeting_path(:temp) when greeting_exists?(:unavail) @current_greeting = greeting_path(:unavail) end end |
#delete_busy_greeting! ⇒ Object
Delete busy.wav
187 188 189 |
# File 'lib/active_mailbox/mailbox.rb', line 187 def delete_busy_greeting! greeting_exists?(:busy) && File.unlink(greeting_path(:busy)) end |
#delete_greeting!(greeting = :unavail) ⇒ Object
Delete greeting
Valid options: :unavail, :temp, :busy
169 170 171 172 173 174 175 |
# File 'lib/active_mailbox/mailbox.rb', line 169 def delete_greeting!(greeting = :unavail) if ValidGreetings.include?(greeting) greeting_exists?(greeting) && File.unlink(greeting_path(greeting)) else raise ActiveMailbox::Errors::GreetingNotFound, "Invalid greeting `#{greeting}'" end end |
#delete_temp_greeting! ⇒ Object
Delete temp.wav
180 181 182 |
# File 'lib/active_mailbox/mailbox.rb', line 180 def delete_temp_greeting! greeting_exists?(:temp) && File.unlink(greeting_path(:temp)) end |
#delete_unavail_greeting! ⇒ Object
Delete unavail.wav
194 195 196 |
# File 'lib/active_mailbox/mailbox.rb', line 194 def delete_unavail_greeting! greeting_exists?(:unavail) && File.unlink(greeting_path(:unavail)) end |
#destroy! ⇒ Object
Destroy this Mailbox and all messages/greetings
137 138 139 |
# File 'lib/active_mailbox/mailbox.rb', line 137 def destroy! FileUtils.rm_rf(mailbox_path) end |
#folders(reload = false) ⇒ Object
A hash/struct of folders in this Mailbox. Keys are the folder name (as a symbol). Values are an array of Message objects
100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 |
# File 'lib/active_mailbox/mailbox.rb', line 100 def folders(reload = false) if reload or @reload_folders or ! defined?(@folders) @folders = {} Dir.chdir(mailbox_path) do Dir['*'].each do |folder| if File.directory?(folder) && ! ignore_dirs.include?(folder) key = folder.downcase.gsub(/\W/, '_').to_sym @folders[key] = Folder.new("#{Dir.pwd}/#{folder}", self) end end end end @folders ensure @reload_folders = false end |
#greeting_path(greeting = :unavail) ⇒ Object
Path to greeting
222 223 224 |
# File 'lib/active_mailbox/mailbox.rb', line 222 def greeting_path(greeting = :unavail) "#{mailbox_path}/#{greeting}.wav" end |
#path ⇒ Object
The full filepath to this Mailbox
91 92 93 |
# File 'lib/active_mailbox/mailbox.rb', line 91 def path @path ||= mailbox_path end |
#purge! ⇒ Object
Destroy all Messages in this mailbox, but leave Mailbox, Folders, and greetings intact
128 129 130 131 132 |
# File 'lib/active_mailbox/mailbox.rb', line 128 def purge! folders.each do |name, folder| folder.purge! end end |
#respond_to?(method_name) ⇒ Boolean
Returns true if method_name is a folder name, or calls super
84 85 86 |
# File 'lib/active_mailbox/mailbox.rb', line 84 def respond_to?(method_name) folders.has_key?(method_name.to_s.downcase.gsub(/\W/, '_').to_sym) || super end |
#sort! ⇒ Object
Sort all Messages in all Folders
See ActiveMailbox::Folder#sort! for more info
146 147 148 149 150 |
# File 'lib/active_mailbox/mailbox.rb', line 146 def sort! folders.each do |name, folder| folder.sort! end end |
#total_messages ⇒ Object
Return total number of messages in every folder
120 121 122 |
# File 'lib/active_mailbox/mailbox.rb', line 120 def folders.values.inject(0) { |sum, folder| sum += folder.size } end |