AsteriskRuby 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,160 @@
1
+ =begin rdoc
2
+ Copyright (c) 2007, Vonage Holdings
3
+
4
+ All rights reserved.
5
+
6
+ Redistribution and use in source and binary forms, with or without
7
+ modification, are permitted provided that the following conditions are met:
8
+
9
+ * Redistributions of source code must retain the above copyright
10
+ notice, this list of conditions and the following disclaimer.
11
+ * Redistributions in binary form must reproduce the above copyright
12
+ notice, this list of conditions and the following disclaimer in the
13
+ documentation and/or other materials provided with the distribution.
14
+ * Neither the name of Vonage Holdings nor the names of its
15
+ contributors may be used to endorse or promote products derived from this
16
+ software without specific prior written permission.
17
+
18
+ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19
+ AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20
+ IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21
+ ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
22
+ LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23
+ CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24
+ SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25
+ INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26
+ CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27
+ ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28
+ POSSIBILITY OF SUCH DAMAGE.
29
+
30
+ Author: Michael Komitee <mkomitee@gmail.com>
31
+ =end
32
+ #AGIResponse implements a simple response object which stores Asterisk's native result, and provided data, to be evaluated in various ways.
33
+
34
+ #AGIResponse implements a simple response object which stores Asterisk's native result, and provided data, to be evaluated in various ways.
35
+ class AGIResponse
36
+ attr_accessor :native, :data, :success
37
+ def initialize
38
+ @native = nil
39
+ @data = nil
40
+ @success = false
41
+ end
42
+ #Allows the response object to be evaluated as a string, returning the data.
43
+ def to_s
44
+ return @data.to_s
45
+ end
46
+ alias_method :to_str, :to_s
47
+
48
+ #Allows the response object to be evaluated as an integer, returning the data if it can be evaluated as such
49
+ def to_i
50
+ if @data.respond_to?(:to_i)
51
+ return @data.to_i
52
+ else
53
+ return nil
54
+ end
55
+ end
56
+
57
+ #Allows the response object to be compared with a regular expression, if possible
58
+ def =~(regexp)
59
+ return unless regexp.instance_of?(Regexp)
60
+ if @data.respond_to?(:to_s)
61
+ return @data.to_s =~ regexp
62
+ else
63
+ return nil
64
+ end
65
+ end
66
+
67
+ #Allows the object to be compared for equality.
68
+ def ==(string)
69
+ @data == string
70
+ end
71
+
72
+ #Can check the response object to see if the associated command was successful.
73
+ def success?
74
+ @success
75
+ end
76
+
77
+ #Check to see if the object is nil returns whether the data is nil
78
+ def nil?
79
+ @data.nil?
80
+ end
81
+
82
+ #Allow addition for numbers (Integer, Float, Fixnum)
83
+ def +(rvalue)
84
+ values = coerce(rvalue)
85
+ return ( values[1] + values[0])
86
+ end
87
+
88
+ #Allow subtraction for numbers (Integer, Float, Fixnum)
89
+ def -(rvalue)
90
+ values = coerce(rvalue)
91
+ return ( values[1] - values[0])
92
+ end
93
+
94
+ #Allow multiplication of numbers (Integer, Float, Fixnum)
95
+ def *(rvalue)
96
+ values = coerce(rvalue)
97
+ return ( values[1] * values[0])
98
+ end
99
+
100
+ #Allow division of numbers (Integer, Float, Fixnum)
101
+ def /(rvalue)
102
+ values = coerce(rvalue)
103
+ return ( values[1] / values[0])
104
+ end
105
+
106
+ #Allow exponentiation of numbers (Integer, Float, Fixnum)
107
+ def **(rvalue)
108
+ values = coerce(rvalue)
109
+ return ( values[1] ** values[0])
110
+ end
111
+
112
+ #Allow modulus of numbers (Integer, Float, Fixnum)
113
+ def %(rvalue)
114
+ values = coerce(rvalue)
115
+ return ( values[1] % values[0])
116
+ end
117
+
118
+
119
+ private
120
+ def coerce(other)
121
+ if Integer === other
122
+ [other, Integer(@data)]
123
+ elsif Float === other
124
+ [other, Float(@data)]
125
+ elsif Fixnum === other
126
+ [other, Fixnum(@data)]
127
+ end
128
+ end
129
+
130
+ end
131
+
132
+ =begin
133
+ Copyright (c) 2007, Vonage Holdings
134
+
135
+ All rights reserved.
136
+
137
+ Redistribution and use in source and binary forms, with or without
138
+ modification, are permitted provided that the following conditions are met:
139
+
140
+ * Redistributions of source code must retain the above copyright
141
+ notice, this list of conditions and the following disclaimer.
142
+ * Redistributions in binary form must reproduce the above copyright
143
+ notice, this list of conditions and the following disclaimer in the
144
+ documentation and/or other materials provided with the distribution.
145
+ * Neither the name of Vonage Holdings nor the names of its
146
+ contributors may be used to endorse or promote products derived from this
147
+ software without specific prior written permission.
148
+
149
+ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
150
+ AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
151
+ IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
152
+ ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
153
+ LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
154
+ CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
155
+ SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
156
+ INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
157
+ CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
158
+ ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
159
+ POSSIBILITY OF SUCH DAMAGE.
160
+ =end
@@ -0,0 +1,72 @@
1
+ =begin rdoc
2
+ Copyright (c) 2007, Vonage Holdings
3
+
4
+ All rights reserved.
5
+
6
+ Redistribution and use in source and binary forms, with or without
7
+ modification, are permitted provided that the following conditions are met:
8
+
9
+ * Redistributions of source code must retain the above copyright
10
+ notice, this list of conditions and the following disclaimer.
11
+ * Redistributions in binary form must reproduce the above copyright
12
+ notice, this list of conditions and the following disclaimer in the
13
+ documentation and/or other materials provided with the distribution.
14
+ * Neither the name of Vonage Holdings nor the names of its
15
+ contributors may be used to endorse or promote products derived from this
16
+ software without specific prior written permission.
17
+
18
+ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19
+ AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20
+ IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21
+ ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
22
+ LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23
+ CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24
+ SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25
+ INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26
+ CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27
+ ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28
+ POSSIBILITY OF SUCH DAMAGE.
29
+
30
+ Author: Michael Komitee <mkomitee@gmail.com>
31
+ The AGIRoute class is meant to be subclassed and have various methods added which will work like rails controller actions, being called by the router when agi requests to certain similarly named uri's occur. For example, an asterisk request to the agi with a url of agi://server:port/TestRoutes/test/?foo=bar would execute the test action in the TestRoutes agi route subclass.
32
+ =end
33
+
34
+ #The AGIRoute class is meant to be subclassed and have various methods added which will work like rails controller actions, being called by the router when agi requests to certain similarly named uri's occur. For example, an asterisk request to the agi with a url of agi://server:port/TestRoutes/test/?foo=bar would execute the test action in the TestRoutes agi route subclass.
35
+ class AGIRoute
36
+ attr_reader :agi, :params, :request
37
+ def initialize(data)
38
+ @agi = data[:agi]
39
+ @params = data[:params]
40
+ @request = data[:request]
41
+ end
42
+ end
43
+
44
+ =begin
45
+ Copyright (c) 2007, Vonage Holdings
46
+
47
+ All rights reserved.
48
+
49
+ Redistribution and use in source and binary forms, with or without
50
+ modification, are permitted provided that the following conditions are met:
51
+
52
+ * Redistributions of source code must retain the above copyright
53
+ notice, this list of conditions and the following disclaimer.
54
+ * Redistributions in binary form must reproduce the above copyright
55
+ notice, this list of conditions and the following disclaimer in the
56
+ documentation and/or other materials provided with the distribution.
57
+ * Neither the name of Vonage Holdings nor the names of its
58
+ contributors may be used to endorse or promote products derived from this
59
+ software without specific prior written permission.
60
+
61
+ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
62
+ AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
63
+ IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
64
+ ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
65
+ LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
66
+ CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
67
+ SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
68
+ INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
69
+ CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
70
+ ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
71
+ POSSIBILITY OF SUCH DAMAGE.
72
+ =end
@@ -0,0 +1,153 @@
1
+ =begin rdoc
2
+ Copyright (c) 2007, Vonage Holdings
3
+
4
+ All rights reserved.
5
+
6
+ Redistribution and use in source and binary forms, with or without
7
+ modification, are permitted provided that the following conditions are met:
8
+
9
+ * Redistributions of source code must retain the above copyright
10
+ notice, this list of conditions and the following disclaimer.
11
+ * Redistributions in binary form must reproduce the above copyright
12
+ notice, this list of conditions and the following disclaimer in the
13
+ documentation and/or other materials provided with the distribution.
14
+ * Neither the name of Vonage Holdings nor the names of its
15
+ contributors may be used to endorse or promote products derived from this
16
+ software without specific prior written permission.
17
+
18
+ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19
+ AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20
+ IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21
+ ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
22
+ LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23
+ CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24
+ SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25
+ INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26
+ CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27
+ ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28
+ POSSIBILITY OF SUCH DAMAGE.
29
+
30
+ Author: Michael Komitee <mkomitee@gmail.com>
31
+ The AGIRouter is used to route clients to the proper AGIRoute method. It parses the URI presented to the AGIServer by asterisk in the request channel parameter, and if the AGIRoute object responds to the requested method/route, it calls it.
32
+ =end
33
+
34
+ #The AGIRouter is used to route clients to the proper AGIRoute method. It parses the URI presented to the AGIServer by asterisk in the request channel parameter, and if the AGIRoute object responds to the requested method/route, it calls it.
35
+ require 'logger'
36
+ require 'uri'
37
+ require 'AGIRoute'
38
+
39
+ #The AGIRouter is used to route clients to the proper AGIRoute method. It parses the URI presented to the AGIServer by asterisk in the request channel parameter, and if the AGIRoute object responds to the requested method/route, it calls it.
40
+ class AGIRouter
41
+ #Logger for the AGIRouter class
42
+ @@logger = Logger.new(STDERR)
43
+
44
+ #Takes the request URI, and parses it into the requested method, a supplied id, and supplied options.
45
+ def initialize(candidate_uri)
46
+ @uri = URI.parse(candidate_uri)
47
+ @controller, @method, @id = parse_path(@uri.path)
48
+ @options = parse_query(@uri.query)
49
+ end
50
+
51
+ #Can be used to reset the AGIRouter classes logger object.
52
+ def AGIRouter.logger(logger)
53
+ @@logger = logger
54
+ end
55
+
56
+ #Takes an agi, and an optional parameters hash, and attempts to route to the requested method. If the method does not exist or is private, uses the agi to change the channel extension to invalid and the priority to 1.
57
+ def route(agi, params=nil)
58
+ request = { :uri => @uri,
59
+ :controller => @controller,
60
+ :method => @method,
61
+ :id => @id,
62
+ :options => @options
63
+ }
64
+
65
+ if controller = get_controller(@controller)
66
+ if check_controller(controller)
67
+ if check_route(controller, @method)
68
+ @@logger.info{"AGIRouter Routing Request to #{@controller} #{@method} by #{@uri}"}
69
+ controller.new({:agi => agi, :params => params, :request => request}).method(@method).call()
70
+ else
71
+ @@logger.warn{"AGIRouter was asked to route a call to controller with unroutable method #{@controller} #{@method}"}
72
+ agi.set_extension('i')
73
+ agi.set_priority('1')
74
+ end
75
+ else
76
+ @@logger.warn{"AGIRouter was asked to route a call to an invalid controller #{@controller}"}
77
+ agi.set_extension('i')
78
+ agi.set_priority('1')
79
+ end
80
+ else
81
+ @@logger.warn{"AGIRouter was asked to route a call to a nonexistant controller #{@controller}"}
82
+ agi.set_extension('i')
83
+ agi.set_priority('1')
84
+ end
85
+ end
86
+
87
+ private
88
+ def parse_path(uri_path)
89
+ return nil if uri_path.nil?
90
+ result = uri_path.split('/')[1,3]
91
+ end
92
+ def parse_query(uri_query)
93
+ return nil if uri_query.nil?
94
+ options = {}
95
+ tuples = uri_query.split('&')
96
+ tuples.each { |tuple| pair = tuple.split('='); options[pair[0]] = pair[1]}
97
+ return options
98
+ end
99
+
100
+ def get_controller(requested_controller)
101
+ begin
102
+ return Module.const_get(requested_controller)
103
+ rescue NameError
104
+ # NameError when requested_controller doesn't exist as a class
105
+ return nil
106
+ end
107
+ end
108
+
109
+ def check_controller(candidate_controller)
110
+ while candidate_controller = candidate_controller.superclass do
111
+ return true if candidate_controller == AGIRoute
112
+ end
113
+ return nil
114
+ end
115
+
116
+ def check_route(controller, requested_method)
117
+ if controller.public_method_defined?(requested_method)
118
+ return true
119
+ else
120
+ return false
121
+ end
122
+ end
123
+ end
124
+
125
+ =begin
126
+ Copyright (c) 2007, Vonage Holdings
127
+
128
+ All rights reserved.
129
+
130
+ Redistribution and use in source and binary forms, with or without
131
+ modification, are permitted provided that the following conditions are met:
132
+
133
+ * Redistributions of source code must retain the above copyright
134
+ notice, this list of conditions and the following disclaimer.
135
+ * Redistributions in binary form must reproduce the above copyright
136
+ notice, this list of conditions and the following disclaimer in the
137
+ documentation and/or other materials provided with the distribution.
138
+ * Neither the name of Vonage Holdings nor the names of its
139
+ contributors may be used to endorse or promote products derived from this
140
+ software without specific prior written permission.
141
+
142
+ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
143
+ AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
144
+ IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
145
+ ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
146
+ LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
147
+ CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
148
+ SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
149
+ INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
150
+ CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
151
+ ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
152
+ POSSIBILITY OF SUCH DAMAGE.
153
+ =end
@@ -0,0 +1,155 @@
1
+ =begin rdoc
2
+ Copyright (c) 2007, Vonage Holdings
3
+
4
+ All rights reserved.
5
+
6
+ Redistribution and use in source and binary forms, with or without
7
+ modification, are permitted provided that the following conditions are met:
8
+
9
+ * Redistributions of source code must retain the above copyright
10
+ notice, this list of conditions and the following disclaimer.
11
+ * Redistributions in binary form must reproduce the above copyright
12
+ notice, this list of conditions and the following disclaimer in the
13
+ documentation and/or other materials provided with the distribution.
14
+ * Neither the name of Vonage Holdings nor the names of its
15
+ contributors may be used to endorse or promote products derived from this
16
+ software without specific prior written permission.
17
+
18
+ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19
+ AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20
+ IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21
+ ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
22
+ LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23
+ CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24
+ SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25
+ INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26
+ CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27
+ ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28
+ POSSIBILITY OF SUCH DAMAGE.
29
+
30
+ Author: Michael Komitee <mkomitee@gmail.com>
31
+
32
+ AGISelection can be used to get a string of digits from an asterisk channel. You configure it similarly to an AGIMenu, but it takes a max_digits paramater and only accepts a single audio file. AGIMenu only accepts a single dtmf digit, whereas AGISelection can accept multiple digits. It would be used to have a user input a PIN or a telephone number or something similar.
33
+
34
+ agi = AGI.new()
35
+ yaml_hash = {:audio => 'tt-monkeys', :max_digits => 4}
36
+ foo = AGISelection.new(yaml_hash)
37
+ foo.read(:agi => AGI.new())
38
+
39
+ =end
40
+
41
+ #AGISelection implements the AGI function get_data via an externally configurable YAML file
42
+ require 'yaml'
43
+ require 'AGI'
44
+ #AGISelection implements the AGI function get_data via an externally configurable YAML file
45
+ class AGISelection
46
+ @@sounds_dir = nil
47
+ # :audio The name of the file that you wish to play
48
+ attr_accessor :audio
49
+ attr_reader :params
50
+ #Creates an AGISelection Object based on the provided YAML Hash.
51
+ #* :audio The name of the file that you wish to play
52
+ #* :default_timeout set to 600 seconds. This can be overridden by passing a :timeout paramerter in your YAML hash
53
+ #* :max_digits no maximum (nil) by default
54
+ def initialize(input=nil)
55
+ @audio = nil
56
+ @default_timeout = 600
57
+ @max_digits = nil
58
+ @params = input
59
+ configure(input)
60
+ end
61
+
62
+ #Configure all the basic variables that AGISelection needs
63
+ #* :audio The name of the file that you wish to play
64
+ #* :default_timeout set to 600 seconds. This can be overridden by passing a :timeout paramerter in your YAML hash
65
+ #* :max_digits no maximum (nil) by default
66
+ def configure(input)
67
+ if input.respond_to?('read')
68
+ config = YAML::load(input)
69
+ elsif input.respond_to?(:to_hash)
70
+ config = input
71
+ elsif input.respond_to?(:to_s) and File.exists?(input.to_s) and File.readable?(input.to_s)
72
+ config = File.open( input.to_s ) { |f| YAML::load( f ) }
73
+ end
74
+ if config.respond_to?(:to_hash)
75
+ @params = config
76
+ @audio = config[:audio]
77
+ @default_timeout = config[:timeout] || @default_timeout
78
+ @max_digits = config[:max_digits] || @max_digits
79
+ end
80
+ end
81
+
82
+ #Read returns the result of executing agi.get_data()
83
+ #You must pass an AGI object as a parameter in your YAML hash
84
+ #For example:
85
+ # require 'AGI'
86
+ # require 'AGISelection'
87
+ #
88
+ # agi = AGI.new({})
89
+ # # Initialize the AGI or bad things will happen
90
+ # agi.init()
91
+ # yaml_hash = {:audio => 'tt-monkeys', :max_digits => 4}
92
+ # foo = AGISelection.new(yaml_hash)
93
+ # # Override the Audio file from the YAML hash
94
+ # foo.audio = 'somethingelse'
95
+ # foo.read(:agi => agi)
96
+ def read(conf={:timeout => @default_timeout, :max_digits => @max_digits})
97
+ if !conf[:agi].respond_to?(:get_data)
98
+ raise ArgumentError, "agi required, must be an AsteriskAGI"
99
+ elsif @audio.nil?
100
+ raise ArgumentError, "You must supply an audio file"
101
+ end
102
+ conf[:timeout] = @default_timeout unless conf.has_key?(:timeout)
103
+ conf[:max_digits] = @max_digits unless conf.has_key?(:max_digits)
104
+ agi = conf[:agi]
105
+ if @@sounds_dir then
106
+ audio = @@sounds_dir + '/' + @audio
107
+ else
108
+ audio = @audio
109
+ end
110
+ begin
111
+ result = agi.get_data(audio, conf[:timeout], conf[:max_digits])
112
+ rescue AGITimeoutError
113
+ nil
114
+ end
115
+ end
116
+
117
+ alias :execute :read
118
+ alias :get :read
119
+ alias :get_data :read
120
+
121
+ def AGISelection.sounds_dir=(sounds_dir)
122
+ @@sounds_dir = sounds_dir
123
+ end
124
+ end
125
+
126
+
127
+ =begin
128
+ Copyright (c) 2007, Vonage Holdings
129
+
130
+ All rights reserved.
131
+
132
+ Redistribution and use in source and binary forms, with or without
133
+ modification, are permitted provided that the following conditions are met:
134
+
135
+ * Redistributions of source code must retain the above copyright
136
+ notice, this list of conditions and the following disclaimer.
137
+ * Redistributions in binary form must reproduce the above copyright
138
+ notice, this list of conditions and the following disclaimer in the
139
+ documentation and/or other materials provided with the distribution.
140
+ * Neither the name of Vonage Holdings nor the names of its
141
+ contributors may be used to endorse or promote products derived from this
142
+ software without specific prior written permission.
143
+
144
+ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
145
+ AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
146
+ IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
147
+ ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
148
+ LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
149
+ CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
150
+ SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
151
+ INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
152
+ CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
153
+ ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
154
+ POSSIBILITY OF SUCH DAMAGE.
155
+ =end