kieranj-asterisk-manager 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2009 Kieran Johnson
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,23 @@
1
+ = asterisk-manager
2
+
3
+ = Overview:
4
+
5
+ A block based DSL for interacting with the Asterisk Manager through a TCP connection.
6
+
7
+ == Installation:
8
+
9
+ $ gem sources -a http://gems.github.com/ (you only need to do this once)
10
+ $ gem install kieranj-asterisk-manager
11
+
12
+ = Usage:
13
+
14
+ require 'rubygems'
15
+ require 'asterisk-manager'
16
+
17
+ AsteriskManager.start('host', 'user', 'secret') do |asterisk|
18
+ asterisk.dial('SIP/123testphone', '7275551212')
19
+ end
20
+
21
+ == Copyright
22
+
23
+ Copyright (c) 2009 Kieran Johnson. See LICENSE for details.
@@ -0,0 +1,4 @@
1
+ ---
2
+ :minor: 1
3
+ :patch: 0
4
+ :major: 0
@@ -0,0 +1,97 @@
1
+ require "socket"
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.start('host', 'user', 'secret') do |asterisk|
10
+ # asterisk.dial('SIP/123testphone', '7275551212')
11
+ # end
12
+
13
+ class AsteriskManager
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
+ end
22
+
23
+ class << self
24
+
25
+ def start(host, username, secret, &block)
26
+ new(host).start(host, username, secret, &block)
27
+ end
28
+
29
+ end
30
+
31
+ # <tt>:host</tt>:: Asterisk host/IP.
32
+ # <tt>:username</tt>:: Asterisk username.
33
+ # <tt>:secret</tt>:: Asterisk secret.
34
+ # <tt>:port</tt>:: Defaults to 5038
35
+
36
+ # TODO: Check authentication response, flag logged in or raise error
37
+ def start(host, username, secret, port=5038)
38
+ begin
39
+ @socket = TCPSocket.new(host, port)
40
+ login(username, secret)
41
+ return yield(self)
42
+ ensure
43
+ logout
44
+ @socket.close
45
+ end
46
+ end
47
+
48
+ def ping
49
+ send_action "Ping"
50
+ end
51
+
52
+ # Dials the extension from the channel.
53
+ # Required fields are:
54
+ # <tt>:channel</tt>:: Your Asterisk device.
55
+ # <tt>:extension</tt>:: The number to dial.
56
+ # Options
57
+ # <tt>:context</tt>:: Context
58
+ # <tt>:priority</tt>:: Priority
59
+ # <tt>:caller_id</tt>:: Caller ID
60
+
61
+ def originate(channel, extension, options={})
62
+ options = {:context => "phones", :priority => 1, :callerid => "Asterisk Automatic Wardial"}.merge(options)
63
+ send_action "Originate", {
64
+ :channel => channel,
65
+ :context => options[:context],
66
+ :exten => extension,
67
+ :priority => options[:priority],
68
+ :callerid => options[:callerid],
69
+ }
70
+ end
71
+
72
+ protected
73
+
74
+ # <tt>:username</tt>:: Asterisk manager username.
75
+ # <tt>:secret</tt>:: Asterisk manager secret.
76
+
77
+ def login(username, secret)
78
+ send_action "Login", {
79
+ :username => username,
80
+ :secret => secret
81
+ }
82
+ end
83
+
84
+ def logout
85
+ send_action "Logoff"
86
+ end
87
+
88
+ # Send the action and options follow by extra new line.
89
+
90
+ def send_action(action, options={})
91
+ action = "Action: #{action}\r\n"
92
+ action += options.map { |k,v| "#{k.to_s.capitalize}: #{v}"}.reverse.join("\r\n")
93
+ action += options.any? ? "\r\n\r\n" : "\r\n"
94
+ @socket.print(action)
95
+ end
96
+
97
+ end
@@ -0,0 +1,7 @@
1
+ require 'test_helper'
2
+
3
+ class AsteriskManagerTest < Test::Unit::TestCase
4
+ should "probably rename this file and start testing for real" do
5
+ flunk "hey buddy, you should probably rename this file and start testing for real"
6
+ end
7
+ end
@@ -0,0 +1,10 @@
1
+ require 'rubygems'
2
+ require 'test/unit'
3
+ require 'shoulda'
4
+
5
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
6
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
7
+ require 'asterisk_manager'
8
+
9
+ class Test::Unit::TestCase
10
+ end
metadata ADDED
@@ -0,0 +1,60 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: kieranj-asterisk-manager
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Kieran Johnson
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-07-03 00:00:00 -07:00
13
+ default_executable:
14
+ dependencies: []
15
+
16
+ description:
17
+ email: kieran@invisiblelines.com
18
+ executables: []
19
+
20
+ extensions: []
21
+
22
+ extra_rdoc_files:
23
+ - README.rdoc
24
+ - LICENSE
25
+ files:
26
+ - README.rdoc
27
+ - VERSION.yml
28
+ - lib/asterisk_manager.rb
29
+ - test/asterisk_manager_test.rb
30
+ - test/test_helper.rb
31
+ - LICENSE
32
+ has_rdoc: true
33
+ homepage: http://github.com/kieranj/asterisk-manager
34
+ post_install_message:
35
+ rdoc_options:
36
+ - --inline-source
37
+ - --charset=UTF-8
38
+ require_paths:
39
+ - lib
40
+ required_ruby_version: !ruby/object:Gem::Requirement
41
+ requirements:
42
+ - - ">="
43
+ - !ruby/object:Gem::Version
44
+ version: "0"
45
+ version:
46
+ required_rubygems_version: !ruby/object:Gem::Requirement
47
+ requirements:
48
+ - - ">="
49
+ - !ruby/object:Gem::Version
50
+ version: "0"
51
+ version:
52
+ requirements: []
53
+
54
+ rubyforge_project:
55
+ rubygems_version: 1.2.0
56
+ signing_key:
57
+ specification_version: 2
58
+ summary: TODO
59
+ test_files: []
60
+