asterisk_manager 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/Gemfile +3 -0
- data/README.md +26 -0
- data/Rakefile +8 -0
- data/lib/asterisk_manager.rb +2 -0
- data/lib/asterisk_manager/client.rb +108 -0
- data/lib/asterisk_manager/version.rb +3 -0
- data/spec/asterisk_manager/client_spec.rb +66 -0
- data/spec/spec_helper.rb +8 -0
- metadata +179 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 28c86c4ef2ed94a4023c9fe1a1c5245c1dd6444d
|
4
|
+
data.tar.gz: 212000fd410995fc4570635b52bcb9170e3adec9
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: ed23e41f58af140006c6ad8b4362ba092f7b05f6db56aa096cc63346510637a01dc61134ef741edd40257ef9b6508362ce5b0c25bf8fc0969e5bc3e883e87fea
|
7
|
+
data.tar.gz: de51250b37c273f3bc9778959e2ac5ac44b71f79feb04417254570844ca65e0f208c8f0b14255661e40d410f0953a426ab4382d0bdc246beeac28a96005c9c91
|
data/Gemfile
ADDED
data/README.md
ADDED
@@ -0,0 +1,26 @@
|
|
1
|
+
# Asterisk Manager
|
2
|
+
|
3
|
+
[![Build Status](https://travis-ci.org/kieranj/asterisk_manager.svg?branch=master)](https://travis-ci.org/kieranj/asterisk_manager)
|
4
|
+
[![Code Climate](https://codeclimate.com/github/kieranj/asterisk_manager/badges/gpa.svg)](https://codeclimate.com/github/kieranj/asterisk_manager)
|
5
|
+
[![Coverage Status](https://coveralls.io/repos/kieranj/asterisk_manager/badge.png)](https://coveralls.io/r/kieranj/asterisk_manager)
|
6
|
+
[![Dependency Status](https://gemnasium.com/kieranj/asterisk_manager.svg)](https://gemnasium.com/kieranj/asterisk_manager)
|
7
|
+
|
8
|
+
## Overview
|
9
|
+
|
10
|
+
A block based DSL for interacting with the Asterisk Manager through a TCP connection.
|
11
|
+
|
12
|
+
## Installation
|
13
|
+
|
14
|
+
$ gem install asterisk_manager
|
15
|
+
|
16
|
+
## Usage
|
17
|
+
|
18
|
+
require 'asterisk_manager'
|
19
|
+
|
20
|
+
AsteriskManager::Client.start('host', 'user', 'secret') do |asterisk|
|
21
|
+
asterisk.dial('SIP/123testphone', '7275551212')
|
22
|
+
end
|
23
|
+
|
24
|
+
## Copyright
|
25
|
+
|
26
|
+
Copyright (c) 2009 Kieran Johnson. See LICENSE for details.
|
data/Rakefile
ADDED
@@ -0,0 +1,108 @@
|
|
1
|
+
module AsteriskManager
|
2
|
+
|
3
|
+
# = Overview:
|
4
|
+
#
|
5
|
+
# A block based DSL for interacting with the Asterisk Manager through a TCP connection.
|
6
|
+
|
7
|
+
# = Usage:
|
8
|
+
#
|
9
|
+
# AsteriskManager::Client.start('host', 'user', 'secret') do |asterisk|
|
10
|
+
# asterisk.originate('SIP/123testphone', '7275551212')
|
11
|
+
# end
|
12
|
+
|
13
|
+
class Client
|
14
|
+
|
15
|
+
# <tt>host</tt>:: Asterisk host/IP.
|
16
|
+
# <tt>port</tt>:: Defaults to 5038
|
17
|
+
|
18
|
+
def initialize(host, port=5038)
|
19
|
+
@host = host
|
20
|
+
@port = port
|
21
|
+
@socket = TCPSocket.new(@host, @port)
|
22
|
+
end
|
23
|
+
|
24
|
+
class << self
|
25
|
+
|
26
|
+
# Convience method for new().start()
|
27
|
+
#
|
28
|
+
# <tt>host</tt>:: Asterisk host/IP.
|
29
|
+
# <tt>username</tt>:: Asterisk username.
|
30
|
+
# <tt>secret</tt>:: Asterisk secret.
|
31
|
+
# <tt>port</tt>:: Defaults to 5038
|
32
|
+
|
33
|
+
def start(host, username, secret, port=5038, &block)
|
34
|
+
new(host, port).start(username, secret, &block)
|
35
|
+
end
|
36
|
+
|
37
|
+
end
|
38
|
+
|
39
|
+
# Logs in, yields the block, then logs off and closes the socket
|
40
|
+
# <tt>host</tt>:: Asterisk host/IP.
|
41
|
+
# <tt>username</tt>:: Asterisk username.
|
42
|
+
|
43
|
+
# TODO: Check authentication response, flag logged in or raise error
|
44
|
+
def start(username, secret, &block)
|
45
|
+
return unless block_given?
|
46
|
+
begin
|
47
|
+
login(username, secret)
|
48
|
+
return yield(self)
|
49
|
+
ensure
|
50
|
+
logout
|
51
|
+
@socket.close
|
52
|
+
end
|
53
|
+
end
|
54
|
+
|
55
|
+
# Sends the ping command
|
56
|
+
|
57
|
+
def ping
|
58
|
+
send_action "Ping"
|
59
|
+
end
|
60
|
+
|
61
|
+
# Dials the extension from the channel.
|
62
|
+
# Required fields are:
|
63
|
+
# <tt>channel</tt>:: Your Asterisk device.
|
64
|
+
# <tt>extension</tt>:: The number to dial.
|
65
|
+
# Options
|
66
|
+
# <tt>context</tt>:: Context
|
67
|
+
# <tt>priority</tt>:: Priority
|
68
|
+
# <tt>caller_id</tt>:: Caller ID
|
69
|
+
|
70
|
+
def originate(channel, extension, options={})
|
71
|
+
options = {:context => "phones", :priority => 1, :callerid => "Asterisk Automatic Wardial"}.merge(options)
|
72
|
+
send_action "Originate", {
|
73
|
+
:channel => channel,
|
74
|
+
:context => options[:context],
|
75
|
+
:exten => extension,
|
76
|
+
:priority => options[:priority],
|
77
|
+
:callerid => options[:callerid],
|
78
|
+
}
|
79
|
+
end
|
80
|
+
|
81
|
+
protected
|
82
|
+
|
83
|
+
# <tt>username</tt>:: Asterisk manager username.
|
84
|
+
# <tt>secret</tt>:: Asterisk manager secret.
|
85
|
+
|
86
|
+
def login(username, secret)
|
87
|
+
send_action "Login", {
|
88
|
+
:username => username,
|
89
|
+
:secret => secret
|
90
|
+
}
|
91
|
+
end
|
92
|
+
|
93
|
+
def logout
|
94
|
+
send_action "Logoff"
|
95
|
+
end
|
96
|
+
|
97
|
+
# Send the action and options follow by extra new line.
|
98
|
+
|
99
|
+
def send_action(action, options={})
|
100
|
+
action = "Action: #{action}\r\n"
|
101
|
+
action += options.map { |k,v| "#{k.to_s.capitalize}: #{v}"}.join("\r\n")
|
102
|
+
action += options.any? ? "\r\n\r\n" : "\r\n"
|
103
|
+
@socket.print(action)
|
104
|
+
end
|
105
|
+
|
106
|
+
end
|
107
|
+
|
108
|
+
end
|
@@ -0,0 +1,66 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe AsteriskManager::Client do
|
4
|
+
|
5
|
+
let(:socket) { instance_double('TCPSocket', print: true, close: true) }
|
6
|
+
|
7
|
+
describe "a new session" do
|
8
|
+
|
9
|
+
before do
|
10
|
+
allow(TCPSocket).to receive(:new).and_return(socket)
|
11
|
+
end
|
12
|
+
|
13
|
+
it "creates a new socket" do
|
14
|
+
expect(TCPSocket).to receive(:new).and_return(socket)
|
15
|
+
AsteriskManager::Client.new("127.0.0.1")
|
16
|
+
end
|
17
|
+
|
18
|
+
it "returns if no block is given" do
|
19
|
+
expect(socket).not_to receive(:print)
|
20
|
+
AsteriskManager::Client.start("127.0.0.1", "tester", "secret")
|
21
|
+
end
|
22
|
+
|
23
|
+
it "logs in at the start of a session" do
|
24
|
+
expect(socket).to receive(:print).with("Action: Login\r\nUsername: tester\r\nSecret: secret\r\n\r\n")
|
25
|
+
AsteriskManager::Client.start('127.0.0.1', 'tester', 'secret') { |asterisk| asterisk.ping }
|
26
|
+
end
|
27
|
+
|
28
|
+
it "yields the block" do
|
29
|
+
expect(socket).to receive(:print).with("Action: Ping\r\n\r\n")
|
30
|
+
AsteriskManager::Client.start("127.0.0.1", "tester", "secret") { |asterisk| asterisk.ping }
|
31
|
+
end
|
32
|
+
|
33
|
+
it "logs off at the end of the session" do
|
34
|
+
expect(socket).to receive(:print).with("Action: Logoff\r\n\r\n")
|
35
|
+
AsteriskManager::Client.start("127.0.0.1", "tester", "secret") { |asterisk| asterisk.ping }
|
36
|
+
end
|
37
|
+
|
38
|
+
it "ensures the socket is closed when the session is closed" do
|
39
|
+
expect(socket).to receive(:close)
|
40
|
+
AsteriskManager::Client.start("127.0.0.1", "tester", "secret") { |asterisk| asterisk.ping }
|
41
|
+
end
|
42
|
+
|
43
|
+
end
|
44
|
+
|
45
|
+
describe 'originating a call' do
|
46
|
+
before do
|
47
|
+
allow(TCPSocket).to receive(:new).and_return(socket)
|
48
|
+
end
|
49
|
+
|
50
|
+
let(:channel) { 'SIP/101test' }
|
51
|
+
let(:extension) { '8135551212' }
|
52
|
+
let(:options) {
|
53
|
+
{
|
54
|
+
context: 'default',
|
55
|
+
priority: 1,
|
56
|
+
callerid: '3125551212'
|
57
|
+
}
|
58
|
+
}
|
59
|
+
|
60
|
+
it 'sends an originate command' do
|
61
|
+
expect(socket).to receive(:print).with("Action: Originate\r\nChannel: #{channel}\r\nContext: #{options[:context]}\r\nExten: #{extension}\r\nPriority: #{options[:priority]}\r\nCallerid: #{options[:callerid]}\r\n\r\n")
|
62
|
+
AsteriskManager::Client.start('127.0.0.1', 'tester', 'secret') { |asterisk| asterisk.originate(channel, extension, options) }
|
63
|
+
end
|
64
|
+
end
|
65
|
+
|
66
|
+
end
|
data/spec/spec_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,179 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: asterisk_manager
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.2.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Kieran Johnson
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2014-09-09 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: rake
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '0'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rspec
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ">="
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: simplecov
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ">="
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: coveralls
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - ">="
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - ">="
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: guard
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - ">="
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '0'
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - ">="
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '0'
|
83
|
+
- !ruby/object:Gem::Dependency
|
84
|
+
name: guard-rspec
|
85
|
+
requirement: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - ">="
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: '0'
|
90
|
+
type: :development
|
91
|
+
prerelease: false
|
92
|
+
version_requirements: !ruby/object:Gem::Requirement
|
93
|
+
requirements:
|
94
|
+
- - ">="
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: '0'
|
97
|
+
- !ruby/object:Gem::Dependency
|
98
|
+
name: fuubar
|
99
|
+
requirement: !ruby/object:Gem::Requirement
|
100
|
+
requirements:
|
101
|
+
- - ">="
|
102
|
+
- !ruby/object:Gem::Version
|
103
|
+
version: '0'
|
104
|
+
type: :development
|
105
|
+
prerelease: false
|
106
|
+
version_requirements: !ruby/object:Gem::Requirement
|
107
|
+
requirements:
|
108
|
+
- - ">="
|
109
|
+
- !ruby/object:Gem::Version
|
110
|
+
version: '0'
|
111
|
+
- !ruby/object:Gem::Dependency
|
112
|
+
name: rb-inotify
|
113
|
+
requirement: !ruby/object:Gem::Requirement
|
114
|
+
requirements:
|
115
|
+
- - ">="
|
116
|
+
- !ruby/object:Gem::Version
|
117
|
+
version: '0'
|
118
|
+
type: :development
|
119
|
+
prerelease: false
|
120
|
+
version_requirements: !ruby/object:Gem::Requirement
|
121
|
+
requirements:
|
122
|
+
- - ">="
|
123
|
+
- !ruby/object:Gem::Version
|
124
|
+
version: '0'
|
125
|
+
- !ruby/object:Gem::Dependency
|
126
|
+
name: rb-fsevent
|
127
|
+
requirement: !ruby/object:Gem::Requirement
|
128
|
+
requirements:
|
129
|
+
- - ">="
|
130
|
+
- !ruby/object:Gem::Version
|
131
|
+
version: '0'
|
132
|
+
type: :development
|
133
|
+
prerelease: false
|
134
|
+
version_requirements: !ruby/object:Gem::Requirement
|
135
|
+
requirements:
|
136
|
+
- - ">="
|
137
|
+
- !ruby/object:Gem::Version
|
138
|
+
version: '0'
|
139
|
+
description:
|
140
|
+
email:
|
141
|
+
- hello@invisiblelines.com
|
142
|
+
executables: []
|
143
|
+
extensions: []
|
144
|
+
extra_rdoc_files: []
|
145
|
+
files:
|
146
|
+
- Gemfile
|
147
|
+
- README.md
|
148
|
+
- Rakefile
|
149
|
+
- lib/asterisk_manager.rb
|
150
|
+
- lib/asterisk_manager/client.rb
|
151
|
+
- lib/asterisk_manager/version.rb
|
152
|
+
- spec/asterisk_manager/client_spec.rb
|
153
|
+
- spec/spec_helper.rb
|
154
|
+
homepage: http://github.com/kieranj/asterisk_manager
|
155
|
+
licenses: []
|
156
|
+
metadata: {}
|
157
|
+
post_install_message:
|
158
|
+
rdoc_options: []
|
159
|
+
require_paths:
|
160
|
+
- lib
|
161
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
162
|
+
requirements:
|
163
|
+
- - ">="
|
164
|
+
- !ruby/object:Gem::Version
|
165
|
+
version: '0'
|
166
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
167
|
+
requirements:
|
168
|
+
- - ">="
|
169
|
+
- !ruby/object:Gem::Version
|
170
|
+
version: '0'
|
171
|
+
requirements: []
|
172
|
+
rubyforge_project:
|
173
|
+
rubygems_version: 2.2.2
|
174
|
+
signing_key:
|
175
|
+
specification_version: 4
|
176
|
+
summary: ''
|
177
|
+
test_files:
|
178
|
+
- spec/asterisk_manager/client_spec.rb
|
179
|
+
- spec/spec_helper.rb
|