Web Full Example (ruby)

The example requires the sinatra web framework. I generated the project on Redial’s server by running the following command from my home directory:

ruby new_sinatra_app.rb main_agi

This file uses “erb” html templates. Erb allows you to mix Ruby code with HTML.
Introduction to ERB

The script also uses the following context to create a “click-to-call” option.

;---- Bridge Call
exten => _1NXXNXXXXXX,1,Set(CALLERID(num)=16466429290)
exten => _1NXXNXXXXXX,n,Dial(SIP/flowroute/${EXTEN},30,r);


require 'sinatra'
require 'rubygems'
require 'data_mapper'
require 'fileutils'

#------- set up database
DataMapper.setup(:default, {
 :adapter => 'mysql',
 :host => 'localhost',
 :username => 'ck987' ,
 :password => 'xxxxxxxxx',
 :database => 'ck987'})
#caller model for database interaction
class Caller
  include DataMapper::Resource

  property :id, Serial
  property :caller_id, String, :required => true
  property :name_audio, String, :required => true, :length => 64
  property :last_call_time, DateTime
  property :created_at, DateTime
# Finish setup

# ------- main route
get '/' do
  @calls = Caller.all
  erb :index #render the index erb template (see end of this file)

# ------- Playback audio
get '/names' do
  erb :names #render the names erb template (see end of this file)

#generates a call file that will first call my phone, then call the other person's phone
def gencallfile(numbertocall)
  time = (Time.now.to_f * 1000).to_i #current timestamp
  temp_dir = "/tmp/"
  callfile = "call_" + time.to_s + ".call"
  startcallfile = temp_dir + callfile
  end_dir = "/var/spool/asterisk/outgoing/"
  endcallfile = end_dir + callfile
  #write file to disk
  file = File.open(startcallfile,"w")
  file.puts("Channel: SIP/flowroute/16466429290\n")
  file.puts("MaxRetries: 1\n")
  file.puts("RetryTime: 60\n")
  file.puts("WaitTime: 30\n")
  file.puts("Context: ck987_bridge\n")
  file.puts("Extension: #{numbertocall}\n")
  #change file permission
  File.chmod(0777, startcallfile)
  #move file to /var/spool/outgoing

#Now define html templates. Normally these would be separate files
@@ layout
    <%= yield %>

<% if (@params_call) %>
  <% gencallfile(@params_call) #make the call %>
  Calling <%= @params_call %>
<% end %>

<b>Callers To Chris's System</b><br><br>
<% @calls.each do |call| %>
<% spl=call.name_audio.split '/' %>
<% filename=spl[spl.length-1] %>
    <td> <%= call.last_call_time.to_s %> </td>
    <td> <a href="names?file=<%= filename %>" target="_blank"> <%= call.caller_id %> </a> </td>
    <td> <a href="?call=<%= call.caller_id %>">Call Them</a> </td>
<% end %>

<embed src="<%= @audio_file %>" width="200" height="40" autoplay="true" />