-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvoicemail-api.rb
46 lines (37 loc) · 1.1 KB
/
voicemail-api.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
require 'rubygems'
require 'sinatra'
require 'lib/sinatra/authorization'
require 'active_record'
require File.expand_path(File.dirname(__FILE__) + '/lib/config')
mime_type :json, "application/json"
mime_type :gsm, "audio/gsm"
mime_type :wav, "audio/wav"
set :authorization_realm, 'Asterisk Voicemail API'
# Models
class Voicemail < ActiveRecord::Base
set_table_name "voicemessages"
end
class User < ActiveRecord::Base
set_table_name "vmusers"
end
helpers do
def authorize(username, password)
User.count(:conditions => ["mailbox = ? AND password = ?", username, password]) > 0
end
end
before do
content_type :json
end
# List voicemails belonging to the current user
get '/voicemails' do
login_required
@voicemails = Voicemail.all(:select => "id, dir, context, callerid, origtime, duration", :conditions => {:mailboxuser => auth.username})
@voicemails.to_json
end
# Send the voicemail content
get '/voicemails/:id' do
login_required
content_type :wav
@voicemail = Voicemail.find(params[:id].to_i, :select => 'recording', :conditions => {:mailboxuser => auth.username})
@voicemail.recording
end