active_mailbox 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/LICENSE +20 -0
- data/README.markdown +215 -0
- data/Rakefile +33 -0
- data/bin/active_mailbox +6 -0
- data/lib/active_mailbox.rb +16 -0
- data/lib/active_mailbox/cli.rb +112 -0
- data/lib/active_mailbox/errors.rb +8 -0
- data/lib/active_mailbox/folder.rb +166 -0
- data/lib/active_mailbox/mailbox.rb +250 -0
- data/lib/active_mailbox/message.rb +136 -0
- data/lib/active_mailbox/version.rb +3 -0
- data/test/folder_test.rb +70 -0
- data/test/mailbox_test.rb +135 -0
- data/test/message_test.rb +50 -0
- data/test/test_helper.rb +97 -0
- metadata +104 -0
@@ -0,0 +1,135 @@
|
|
1
|
+
require File.dirname(__FILE__) + '/test_helper.rb'
|
2
|
+
|
3
|
+
class MailboxTest < Test::Unit::TestCase
|
4
|
+
context "ActiveMailbox::Mailbox" do
|
5
|
+
should "find Mailbox with extension and context" do
|
6
|
+
ActiveMailbox::Fixtures.execute! do
|
7
|
+
assert ActiveMailbox::Mailbox.find('15183332220')
|
8
|
+
end
|
9
|
+
end
|
10
|
+
|
11
|
+
should "find Mailbox with extension only" do
|
12
|
+
ActiveMailbox::Fixtures.execute! do
|
13
|
+
m1 = ActiveMailbox::Mailbox.find('15183332220', '518')
|
14
|
+
m2 = ActiveMailbox::Mailbox.find('15183332220')
|
15
|
+
assert m1 == m2
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
should "raise MailboxNotFound with invalid path" do
|
20
|
+
assert_raise ActiveMailbox::Errors::MailboxNotFound do
|
21
|
+
ActiveMailbox::Mailbox.find('i certainly', 'dont exist')
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
context "instance" do
|
26
|
+
setup do
|
27
|
+
ActiveMailbox::Fixtures.create!
|
28
|
+
@mailbox = ActiveMailbox::Mailbox.find('15183332220', '518')
|
29
|
+
end
|
30
|
+
|
31
|
+
teardown do
|
32
|
+
ActiveMailbox::Fixtures.destroy!
|
33
|
+
end
|
34
|
+
|
35
|
+
should "use method_missing to simulate folder accessors" do
|
36
|
+
@mailbox.folders.keys.each do |folder|
|
37
|
+
assert_nothing_raised do
|
38
|
+
assert @mailbox.send(folder)
|
39
|
+
end
|
40
|
+
assert @mailbox.send(folder) == @mailbox.folders[folder]
|
41
|
+
end
|
42
|
+
|
43
|
+
assert_raise NoMethodError do
|
44
|
+
@mailbox.i_dont_exist
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
48
|
+
should "yield Folder hash" do
|
49
|
+
assert @mailbox.folders.is_a?(Hash)
|
50
|
+
@mailbox.folders.each do |key, folder|
|
51
|
+
assert folder.is_a?(ActiveMailbox::Folder)
|
52
|
+
end
|
53
|
+
end
|
54
|
+
|
55
|
+
should "purge all messages in all folders" do
|
56
|
+
assert @mailbox.total_messages > 0
|
57
|
+
@mailbox.purge!
|
58
|
+
@mailbox.folders.each do |name, folder|
|
59
|
+
assert folder.messages.empty?
|
60
|
+
assert File.exists?(folder.path)
|
61
|
+
end
|
62
|
+
assert_equal 0, @mailbox.total_messages
|
63
|
+
end
|
64
|
+
|
65
|
+
should "destroy itself" do
|
66
|
+
@mailbox.destroy!
|
67
|
+
assert File.exists?(@mailbox.path) == false
|
68
|
+
end
|
69
|
+
|
70
|
+
should "yield a current greeting" do
|
71
|
+
assert @mailbox.current_greeting
|
72
|
+
end
|
73
|
+
|
74
|
+
should "delete valid greeting" do
|
75
|
+
@mailbox.delete_greeting!(:unavail)
|
76
|
+
assert File.exists?(@mailbox.greeting_path(:unavail)) == false
|
77
|
+
assert_raise ActiveMailbox::Errors::GreetingNotFound do
|
78
|
+
@mailbox.delete_greeting!(:i_am_not_real)
|
79
|
+
end
|
80
|
+
|
81
|
+
assert @mailbox.delete_greeting!(:unavail) == false
|
82
|
+
end
|
83
|
+
|
84
|
+
should "delete temp greeting" do
|
85
|
+
@mailbox.delete_temp_greeting!
|
86
|
+
assert File.exists?(@mailbox.greeting_path(:temp)) == false
|
87
|
+
end
|
88
|
+
|
89
|
+
should "delete unvavail greeting" do
|
90
|
+
@mailbox.delete_unavail_greeting!
|
91
|
+
assert File.exists?(@mailbox.greeting_path(:unavail)) == false
|
92
|
+
end
|
93
|
+
|
94
|
+
should "delete busy greeting" do
|
95
|
+
@mailbox.delete_busy_greeting!
|
96
|
+
assert File.exists?(@mailbox.greeting_path(:busy)) == false
|
97
|
+
end
|
98
|
+
|
99
|
+
should "clean ghost messages in all folders" do
|
100
|
+
@mailbox.folders.each do |key, folder|
|
101
|
+
ActiveMailbox::Fixtures.simulate_ghosts(folder)
|
102
|
+
count = @mailbox.total_messages
|
103
|
+
@mailbox.clean_ghosts!
|
104
|
+
assert count > @mailbox.total_messages
|
105
|
+
end
|
106
|
+
end
|
107
|
+
|
108
|
+
should "clean stale messages in all folders" do
|
109
|
+
count = @mailbox.total_messages
|
110
|
+
@mailbox.clean_stale!
|
111
|
+
assert count > @mailbox.total_messages
|
112
|
+
end
|
113
|
+
|
114
|
+
should "sort and rename all messages in all folders" do
|
115
|
+
@mailbox.folders.each do |key, folder|
|
116
|
+
messages = folder.messages.map(&:name)
|
117
|
+
assert messages.size == folder.size
|
118
|
+
|
119
|
+
ActiveMailbox::Fixtures.simulate_unordered(folder)
|
120
|
+
assert messages.size > folder.size
|
121
|
+
|
122
|
+
names = messages.map { |m| "msg%04d" % m.match(/([0-9]{4})/)[1].to_i }
|
123
|
+
check = (0..messages.size - 1).map { |i| "msg%04d" % i }
|
124
|
+
assert_same_elements names, check
|
125
|
+
end
|
126
|
+
end
|
127
|
+
|
128
|
+
should "be compared to another instance based on mailbox number" do
|
129
|
+
mailbox = ActiveMailbox::Mailbox.find(@mailbox.mailbox)
|
130
|
+
assert mailbox == @mailbox
|
131
|
+
end
|
132
|
+
|
133
|
+
end
|
134
|
+
end
|
135
|
+
end
|
@@ -0,0 +1,50 @@
|
|
1
|
+
require File.dirname(__FILE__) + '/test_helper.rb'
|
2
|
+
|
3
|
+
class MessageTest < Test::Unit::TestCase
|
4
|
+
context "ActiveMailbox::Message" do
|
5
|
+
setup do
|
6
|
+
ActiveMailbox::Fixtures.create!
|
7
|
+
@mailbox = ActiveMailbox::Mailbox.find('15183332220', '518')
|
8
|
+
@message = @mailbox.inbox.messages.first
|
9
|
+
end
|
10
|
+
|
11
|
+
teardown do
|
12
|
+
ActiveMailbox::Fixtures.destroy!
|
13
|
+
end
|
14
|
+
|
15
|
+
should "raise MessageNotFound initializing with invalid info file path" do
|
16
|
+
assert_raise ActiveMailbox::Errors::MessageNotFound do
|
17
|
+
ActiveMailbox::Message.new('i dont exist', @mailbox)
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
context "instance" do
|
22
|
+
should "destroy itself" do
|
23
|
+
wav = @message.wav
|
24
|
+
txt = @message.txt
|
25
|
+
|
26
|
+
assert @message.destroy!
|
27
|
+
assert File.exists?(wav) == false
|
28
|
+
assert File.exists?(txt) == false
|
29
|
+
end
|
30
|
+
|
31
|
+
should "yield attributes" do
|
32
|
+
[:timestamp, :duration, :callerid_number, :callerid_name, :name, :path].each do |key|
|
33
|
+
assert @message.send(key).nil? == false
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
should "be stale if older than MaximumAge" do
|
38
|
+
# last fixture message is older than MaximumAge
|
39
|
+
assert @mailbox.inbox.messages.last.stale?
|
40
|
+
end
|
41
|
+
|
42
|
+
should "be compared based on message number" do
|
43
|
+
m1 = @mailbox.inbox.messages[0]
|
44
|
+
m2 = @mailbox.inbox.messages[1]
|
45
|
+
assert m1 < m2
|
46
|
+
end
|
47
|
+
|
48
|
+
end
|
49
|
+
end
|
50
|
+
end
|
data/test/test_helper.rb
ADDED
@@ -0,0 +1,97 @@
|
|
1
|
+
# Set Asterisk's voicemail path so tests don't clobber a production system
|
2
|
+
ENV['ASTERISK_VOICEMAIL_ROOT'] = "/tmp/active_mailbox_test_#{Time.now.to_i}"
|
3
|
+
|
4
|
+
require 'rubygems'
|
5
|
+
require 'test/unit'
|
6
|
+
require 'turn'
|
7
|
+
require 'shoulda'
|
8
|
+
require 'fileutils'
|
9
|
+
|
10
|
+
require 'active_mailbox'
|
11
|
+
|
12
|
+
module ActiveMailbox::Fixtures
|
13
|
+
extend self
|
14
|
+
|
15
|
+
BASE_DIR = ENV['ASTERISK_VOICEMAIL_ROOT']
|
16
|
+
|
17
|
+
def simulate_ghosts(folder)
|
18
|
+
[2, 4, 0, 5].map do |i|
|
19
|
+
message = folder.messages[i]
|
20
|
+
File.unlink(message.wav)
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
def simulate_unordered(folder)
|
25
|
+
[2, 4, 0, 5].map do |i|
|
26
|
+
folder.messages[i]
|
27
|
+
end.each(&:destroy!)
|
28
|
+
end
|
29
|
+
|
30
|
+
def mailbox
|
31
|
+
File.join(BASE_DIR, '518', '15183332220')
|
32
|
+
end
|
33
|
+
|
34
|
+
def inbox
|
35
|
+
File.join(mailbox, 'INBOX')
|
36
|
+
end
|
37
|
+
|
38
|
+
def old
|
39
|
+
File.join(mailbox, 'Old')
|
40
|
+
end
|
41
|
+
|
42
|
+
def info_template(date)
|
43
|
+
template = %{;
|
44
|
+
; Message Information file
|
45
|
+
;
|
46
|
+
[message]
|
47
|
+
origmailbox=15183332220
|
48
|
+
context=incoming
|
49
|
+
macrocontext=
|
50
|
+
exten=15183332220
|
51
|
+
priority=6
|
52
|
+
callerchan=SIP/16.26.15.146-00c811d0
|
53
|
+
callerid="Joshua Priddle" <15183332220>
|
54
|
+
origdate=#{date}
|
55
|
+
origtime=1244496482
|
56
|
+
category=
|
57
|
+
duration=0}.gsub(/^ /, '')
|
58
|
+
end
|
59
|
+
|
60
|
+
def execute!(&block)
|
61
|
+
create!
|
62
|
+
yield
|
63
|
+
destroy!
|
64
|
+
end
|
65
|
+
|
66
|
+
def destroy!
|
67
|
+
if File.exists?(BASE_DIR)
|
68
|
+
FileUtils.rm_rf(BASE_DIR)
|
69
|
+
end
|
70
|
+
end
|
71
|
+
|
72
|
+
def create!
|
73
|
+
FileUtils.mkdir_p(mailbox)
|
74
|
+
FileUtils.touch(File.join(mailbox, 'unavail.wav'))
|
75
|
+
FileUtils.touch(File.join(mailbox, 'temp.wav'))
|
76
|
+
|
77
|
+
[inbox, old].each do |dir|
|
78
|
+
FileUtils.mkdir_p(dir)
|
79
|
+
Dir.chdir(dir) do
|
80
|
+
0.upto(10) do |i|
|
81
|
+
msg = "msg%04d" % i
|
82
|
+
wav = "#{msg}.wav"
|
83
|
+
txt = "#{msg}.txt"
|
84
|
+
|
85
|
+
FileUtils.touch(wav)
|
86
|
+
|
87
|
+
date = Time.now - (60 * 60 * 24 * (4 * (i + 1)))
|
88
|
+
|
89
|
+
File.open(txt, "w") do |f|
|
90
|
+
f.puts info_template(date)
|
91
|
+
end
|
92
|
+
|
93
|
+
end
|
94
|
+
end
|
95
|
+
end
|
96
|
+
end
|
97
|
+
end
|
metadata
ADDED
@@ -0,0 +1,104 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: active_mailbox
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
prerelease: false
|
5
|
+
segments:
|
6
|
+
- 0
|
7
|
+
- 0
|
8
|
+
- 1
|
9
|
+
version: 0.0.1
|
10
|
+
platform: ruby
|
11
|
+
authors:
|
12
|
+
- Joshua Priddle
|
13
|
+
autorequire:
|
14
|
+
bindir: bin
|
15
|
+
cert_chain: []
|
16
|
+
|
17
|
+
date: 2010-12-10 00:00:00 -05:00
|
18
|
+
default_executable:
|
19
|
+
dependencies:
|
20
|
+
- !ruby/object:Gem::Dependency
|
21
|
+
name: shoulda
|
22
|
+
prerelease: false
|
23
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
24
|
+
requirements:
|
25
|
+
- - "="
|
26
|
+
- !ruby/object:Gem::Version
|
27
|
+
segments:
|
28
|
+
- 2
|
29
|
+
- 11
|
30
|
+
- 3
|
31
|
+
version: 2.11.3
|
32
|
+
type: :development
|
33
|
+
version_requirements: *id001
|
34
|
+
- !ruby/object:Gem::Dependency
|
35
|
+
name: turn
|
36
|
+
prerelease: false
|
37
|
+
requirement: &id002 !ruby/object:Gem::Requirement
|
38
|
+
requirements:
|
39
|
+
- - "="
|
40
|
+
- !ruby/object:Gem::Version
|
41
|
+
segments:
|
42
|
+
- 0
|
43
|
+
- 8
|
44
|
+
- 1
|
45
|
+
version: 0.8.1
|
46
|
+
type: :development
|
47
|
+
version_requirements: *id002
|
48
|
+
description: " ActiveMailbox provides a simple API and command line utility to work\n with voicemail boxes and voicemails generated by Asterisk.\n"
|
49
|
+
email: jpriddle@nevercraft.net
|
50
|
+
executables:
|
51
|
+
- active_mailbox
|
52
|
+
extensions: []
|
53
|
+
|
54
|
+
extra_rdoc_files:
|
55
|
+
- README.markdown
|
56
|
+
- LICENSE
|
57
|
+
files:
|
58
|
+
- Rakefile
|
59
|
+
- README.markdown
|
60
|
+
- LICENSE
|
61
|
+
- bin/active_mailbox
|
62
|
+
- lib/active_mailbox/cli.rb
|
63
|
+
- lib/active_mailbox/errors.rb
|
64
|
+
- lib/active_mailbox/folder.rb
|
65
|
+
- lib/active_mailbox/mailbox.rb
|
66
|
+
- lib/active_mailbox/message.rb
|
67
|
+
- lib/active_mailbox/version.rb
|
68
|
+
- lib/active_mailbox.rb
|
69
|
+
- test/folder_test.rb
|
70
|
+
- test/mailbox_test.rb
|
71
|
+
- test/message_test.rb
|
72
|
+
- test/test_helper.rb
|
73
|
+
has_rdoc: true
|
74
|
+
homepage: http://github.com/itspriddle/active_mailbox
|
75
|
+
licenses: []
|
76
|
+
|
77
|
+
post_install_message:
|
78
|
+
rdoc_options:
|
79
|
+
- --charset=UTF-8
|
80
|
+
require_paths:
|
81
|
+
- lib
|
82
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
83
|
+
requirements:
|
84
|
+
- - ">="
|
85
|
+
- !ruby/object:Gem::Version
|
86
|
+
segments:
|
87
|
+
- 0
|
88
|
+
version: "0"
|
89
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
90
|
+
requirements:
|
91
|
+
- - ">="
|
92
|
+
- !ruby/object:Gem::Version
|
93
|
+
segments:
|
94
|
+
- 0
|
95
|
+
version: "0"
|
96
|
+
requirements: []
|
97
|
+
|
98
|
+
rubyforge_project:
|
99
|
+
rubygems_version: 1.3.6
|
100
|
+
signing_key:
|
101
|
+
specification_version: 3
|
102
|
+
summary: "ActiveMailbox: API/CLI for Asterisk Voicemail"
|
103
|
+
test_files: []
|
104
|
+
|