Skip to content

Commit

Permalink
Refactoring completed
Browse files Browse the repository at this point in the history
  • Loading branch information
Emiliano Della Casa committed Nov 11, 2013
1 parent 49a875a commit da27ed4
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 61 deletions.
3 changes: 1 addition & 2 deletions README.md
Expand Up @@ -162,8 +162,7 @@ The data property contains all additional information obtained from Asterisk, li

- Adding initialization parameters for Telnet options like Output_log, Waittime, Dump_log, timeout;
- Adding test cases for better code coverage;
- Refactoring response.rb;

- Refactoring of ruby-asterisk.rb, adding method_missing for the purpose of supporting as much AMI commands as possible

## Development

Expand Down
83 changes: 43 additions & 40 deletions lib/ruby-asterisk.rb
@@ -1,14 +1,17 @@
require "ruby-asterisk/version"
require "ruby-asterisk/request"
require "ruby-asterisk/response"

require 'ruby-asterisk/version'
require 'ruby-asterisk/request'
require 'ruby-asterisk/response'
require 'net/telnet'

module RubyAsterisk
##
#
# Ruby-asterisk main classes
#
class AMI
attr_accessor :host, :port, :connected

def initialize(host,port)
def initialize(host, port)
self.host = host.to_s
self.port = port.to_i
self.connected = false
Expand All @@ -17,7 +20,7 @@ def initialize(host,port)

def connect
begin
@session = Net::Telnet::new("Host" => self.host,"Port" => self.port)
@session = Net::Telnet::new('Host' => self.host, 'Port' => self.port, 'Timeout' => 10)
self.connected = true
rescue Exception => ex
false
Expand All @@ -35,113 +38,113 @@ def disconnect
end
end

def login(username,password)
def login(username, password)
self.connect unless self.connected
execute "Login", {"Username" => username, "Secret" => password, "Event" => "On"}
execute 'Login', {'Username' => username, 'Secret' => password, 'Event' => 'On'}
end

def command(command)
execute "Command", {"Command" => command}
execute 'Command', {'Command' => command}
end

def core_show_channels
execute "CoreShowChannels"
execute 'CoreShowChannels'
end

def meet_me_list
execute "MeetMeList"
execute 'MeetMeList'
end

def parked_calls
execute "ParkedCalls"
execute 'ParkedCalls'
end

def extension_state(exten, context, action_id=nil)
execute "ExtensionState", {"Exten" => exten, "Context" => context, "ActionID" => action_id}
def extension_state(exten, context, action_id = nil)
execute 'ExtensionState', {'Exten' => exten, 'Context' => context, 'ActionID' => action_id}
end

def skinny_devices
execute "SKINNYdevices"
execute 'SKINNYdevices'
end

def skinny_lines
execute "SKINNYlines"
execute 'SKINNYlines'
end

def status(channel=nil,action_id=nil)
execute "Status", {"Channel" => channel, "ActionID" => action_id}
def status(channel = nil, action_id = nil)
execute 'Status', {'Channel' => channel, 'ActionID' => action_id}
end

def originate(caller,context,callee,priority,variable=nil)
execute "Originate", {"Channel" => caller, "Context" => context, "Exten" => callee, "Priority" => priority, "Callerid" => caller, "Timeout" => "30000", "Variable" => variable }
def originate(caller, context, callee, priority, variable = nil)
execute 'Originate', {'Channel' => caller, 'Context' => context, 'Exten' => callee, 'Priority' => priority, 'Callerid' => caller, 'Timeout' => '30000', 'Variable' => variable }
end

def channels
execute "Command", { "Command" => "show channels" }
execute 'Command', { 'Command' => 'show channels' }
end

def redirect(caller,context,callee,priority,variable=nil)
execute "Redirect", {"Channel" => caller, "Context" => context, "Exten" => callee, "Priority" => priority, "Callerid" => caller, "Timeout" => "30000", "Variable" => variable}
execute 'Redirect', {'Channel' => caller, 'Context' => context, 'Exten' => callee, 'Priority' => priority, 'Callerid' => caller, 'Timeout' => '30000', 'Variable' => variable}
end

def queues
execute "Queues", {}
execute 'Queues', {}
end

def queue_add(queue, exten, penalty = 2, paused = false, member_name = '')
execute "QueueAdd", {"Queue" => queue, "Interface" => exten, "Penalty" => penalty, "Paused" => paused, "MemberName" => member_name}
execute 'QueueAdd', {'Queue' => queue, 'Interface' => exten, 'Penalty' => penalty, 'Paused' => paused, 'MemberName' => member_name}
end

def queue_pause(exten, paused)
execute "QueuePause", {"Interface" => exten, "Paused" => paused}
execute 'QueuePause', {'Interface' => exten, 'Paused' => paused}
end

def queue_remove(queue, exten)
execute "QueueRemove", {"Queue" => queue, "Interface" => exten}
execute 'QueueRemove', {'Queue' => queue, 'Interface' => exten}
end

def queue_status
execute "QueueStatus"
execute 'QueueStatus'
end

def queue_summary(queue)
execute "QueueSummary", {"Queue" => queue}
execute 'QueueSummary', {'Queue' => queue}
end

def mailbox_status(exten, context="default")
execute "MailboxStatus", {"Mailbox" => "#{exten}@#{context}"}
def mailbox_status(exten, context='default')
execute 'MailboxStatus', {'Mailbox' => "#{exten}@#{context}"}
end

def mailbox_count(exten, context="default")
execute "MailboxCount", {"Mailbox" => "#{exten}@#{context}"}
def mailbox_count(exten, context='default')
execute 'MailboxCount', {'Mailbox' => "#{exten}@#{context}"}
end

def queue_pause(interface,paused,queue,reason='none')
execute "QueuePause", {"Interface" => interface, "Paused" => paused, "Queue" => queue, "Reason" => reason}
execute 'QueuePause', {'Interface' => interface, 'Paused' => paused, 'Queue' => queue, 'Reason' => reason}
end

def ping
execute "Ping"
execute 'Ping'
end

def event_mask(event_mask="off")
execute "Events", {"EventMask" => event_mask}
def event_mask(event_mask='off')
execute 'Events', {'EventMask' => event_mask}
end

def sip_peers
execute "SIPpeers"
execute 'SIPpeers'
end

private
def execute(command, options={})
def execute(command, options = {})
request = Request.new(command, options)
request.commands.each do |command|
@session.write(command)
end
@session.waitfor("Match" => /ActionID: #{request.action_id}.*?\n\n/m, "Timeout" => 10) do |data|
@session.waitfor('Match' => /ActionID: #{request.action_id}.*?\n\n/m) do |data|
request.response_data << data
end
Response.new(command,request.response_data)
Response.new(command, request.response_data)
end
end
end
21 changes: 12 additions & 9 deletions lib/ruby-asterisk/request.rb
@@ -1,32 +1,35 @@
module RubyAsterisk
##
#
# Class responsible of building commands structure
#
class Request
attr_accessor :action, :action_id, :parameters, :response_data

def initialize(action,parameters={})
def initialize(action, parameters = {})
self.action = action
self.action_id = self.generate_action_id
self.action_id = Request.generate_action_id
self.parameters = parameters
self.response_data = ''
end

def commands
_commands=["Action: #{self.action}\r\n","ActionID: #{self.action_id}\r\n"]
self.parameters.each do |key,value|
_commands<<key+": #{value}\r\n" unless value.nil?
_commands = ["Action: #{self.action}\r\n", "ActionID: #{self.action_id}\r\n"]
self.parameters.each do |key, value|
_commands << key + ": #{value}\r\n" unless value.nil?
end
_commands[_commands.length-1]<<"\r\n"
_commands[_commands.length - 1] << "\r\n"
_commands
end

protected

def generate_action_id
if RUBY_VERSION.start_with?("1.9")
def self.generate_action_id
if RUBY_VERSION.start_with?('1.9')
Random.rand(999).to_s
else
rand(999).to_s
end
end
end
end
5 changes: 2 additions & 3 deletions lib/ruby-asterisk/response_parser.rb
Expand Up @@ -27,10 +27,9 @@ def self._add_status(exten_array)
def self._parse_objects(response, parse_params)
object_array = []
object_regex = Regexp.new(/#{parse_params[:search_for]}\n(.*)\n\n/m)
object_regex.match(response) do |m|
object_regex.match(response) do |match|
object = {}
lines = m[0].split(/\n/)
lines.each do |line|
match[0].split(/\n/).each do |line|
tokens = line.split(':', 2)
object[tokens[0].strip]=tokens[1].strip unless tokens[1].nil?
end
Expand Down
14 changes: 7 additions & 7 deletions spec/ruby-asterisk/response_spec.rb
Expand Up @@ -5,7 +5,7 @@
def sip_peers_response
"Response: Success
Message: Peer status list will follow
Event: PeerEntry
Channeltype: SIP
ObjectName: 9915057
Expand Down Expand Up @@ -97,9 +97,9 @@ def extension_state_response
end

describe ".new" do

describe "receiving a Core Show Channels request" do
it "should parse correctly data coming from Asterisk about channels" do
it "should parse correctly data coming from Asterisk about channels" do
@response = RubyAsterisk::Response.new("CoreShowChannels",core_show_channels_response)
@response.data[:channels].should_not be_empty
end
Expand All @@ -114,7 +114,7 @@ def extension_state_response
@response.data[:channels].count.should eq(0)
end
end

describe "receiving a Parked Calls request" do
it "should parse correctly data coming from Asterisk about calls" do
@response = RubyAsterisk::Response.new("ParkedCalls",parked_calls_response)
Expand All @@ -127,7 +127,7 @@ def extension_state_response
end
end

describe "receiving a Originate request" do
describe "receiving a Originate request" do
it "should parse correctly data coming from Asterisk about the call" do
@response = RubyAsterisk::Response.new("Originate",originate_response)
@response.data[:dial].should_not be_empty
Expand All @@ -143,7 +143,7 @@ def extension_state_response
@response.raw_response.should eq(originate_response)
end
end

describe "receiving a MeetMeList request" do
it "should parse correctly data coming from Asterisk about the conference room" do
@response = RubyAsterisk::Response.new("MeetMeList",meet_me_list_response)
Expand Down Expand Up @@ -175,7 +175,7 @@ def extension_state_response
@response = RubyAsterisk::Response.new("SIPpeers",sip_peers_response)
@response.data[:peers].should_not be_empty
end

it "should correctly fill the fields" do
@response = RubyAsterisk::Response.new("SIPpeers",sip_peers_response)
@response.data[:peers][0]["ObjectName"].should eq("9915057")
Expand Down

0 comments on commit da27ed4

Please sign in to comment.