adhearsion 0.7.7 → 0.8.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (179) hide show
  1. data/CHANGELOG +9 -42
  2. data/EVENTS +11 -0
  3. data/README.txt +5 -0
  4. data/Rakefile +94 -84
  5. data/adhearsion.gemspec +148 -0
  6. data/app_generators/ahn/USAGE +5 -0
  7. data/app_generators/ahn/ahn_generator.rb +87 -0
  8. data/app_generators/ahn/templates/.ahnrc +34 -0
  9. data/app_generators/ahn/templates/README +8 -0
  10. data/app_generators/ahn/templates/Rakefile +23 -0
  11. data/app_generators/ahn/templates/components/ami_remote/ami_remote.rb +15 -0
  12. data/app_generators/ahn/templates/components/disabled/HOW_TO_ENABLE +7 -0
  13. data/app_generators/ahn/templates/components/disabled/stomp_gateway/README.markdown +47 -0
  14. data/app_generators/ahn/templates/components/disabled/stomp_gateway/config.yml +12 -0
  15. data/app_generators/ahn/templates/components/disabled/stomp_gateway/stomp_gateway.rb +34 -0
  16. data/app_generators/ahn/templates/components/restful_rpc/README.markdown +11 -0
  17. data/app_generators/ahn/templates/components/restful_rpc/config.yml +34 -0
  18. data/app_generators/ahn/templates/components/restful_rpc/example-client.rb +48 -0
  19. data/app_generators/ahn/templates/components/restful_rpc/restful_rpc.rb +87 -0
  20. data/app_generators/ahn/templates/components/restful_rpc/spec/restful_rpc_spec.rb +263 -0
  21. data/app_generators/ahn/templates/components/simon_game/simon_game.rb +56 -0
  22. data/app_generators/ahn/templates/config/startup.rb +50 -0
  23. data/app_generators/ahn/templates/dialplan.rb +3 -0
  24. data/app_generators/ahn/templates/events.rb +32 -0
  25. data/bin/ahn +28 -0
  26. data/bin/ahnctl +68 -0
  27. data/bin/jahn +42 -0
  28. data/examples/asterisk_manager_interface/standalone.rb +51 -0
  29. data/lib/adhearsion.rb +35 -953
  30. data/lib/adhearsion/cli.rb +223 -0
  31. data/lib/adhearsion/component_manager.rb +208 -0
  32. data/lib/adhearsion/component_manager/component_tester.rb +55 -0
  33. data/lib/adhearsion/component_manager/spec_framework.rb +24 -0
  34. data/lib/adhearsion/events_support.rb +84 -0
  35. data/lib/adhearsion/foundation/all.rb +9 -0
  36. data/lib/adhearsion/foundation/blank_slate.rb +5 -0
  37. data/lib/adhearsion/foundation/custom_daemonizer.rb +45 -0
  38. data/lib/adhearsion/foundation/event_socket.rb +203 -0
  39. data/lib/adhearsion/foundation/future_resource.rb +36 -0
  40. data/lib/adhearsion/foundation/global.rb +1 -0
  41. data/lib/adhearsion/foundation/metaprogramming.rb +17 -0
  42. data/lib/adhearsion/foundation/numeric.rb +13 -0
  43. data/lib/adhearsion/foundation/pseudo_guid.rb +10 -0
  44. data/lib/adhearsion/foundation/relationship_properties.rb +42 -0
  45. data/lib/adhearsion/foundation/string.rb +26 -0
  46. data/lib/adhearsion/foundation/synchronized_hash.rb +96 -0
  47. data/lib/adhearsion/foundation/thread_safety.rb +7 -0
  48. data/lib/adhearsion/host_definitions.rb +67 -0
  49. data/lib/adhearsion/initializer.rb +373 -0
  50. data/lib/adhearsion/initializer/asterisk.rb +81 -0
  51. data/lib/adhearsion/initializer/configuration.rb +254 -0
  52. data/lib/adhearsion/initializer/database.rb +49 -0
  53. data/lib/adhearsion/initializer/drb.rb +31 -0
  54. data/lib/adhearsion/initializer/freeswitch.rb +22 -0
  55. data/lib/adhearsion/initializer/rails.rb +40 -0
  56. data/lib/adhearsion/logging.rb +92 -0
  57. data/lib/adhearsion/tasks.rb +16 -0
  58. data/lib/adhearsion/tasks/database.rb +5 -0
  59. data/lib/adhearsion/tasks/deprecations.rb +59 -0
  60. data/lib/adhearsion/tasks/generating.rb +20 -0
  61. data/lib/adhearsion/tasks/lint.rb +4 -0
  62. data/lib/adhearsion/tasks/testing.rb +37 -0
  63. data/lib/adhearsion/version.rb +9 -0
  64. data/lib/adhearsion/voip/asterisk.rb +4 -0
  65. data/lib/adhearsion/voip/asterisk/agi_server.rb +81 -0
  66. data/lib/adhearsion/voip/asterisk/commands.rb +1284 -0
  67. data/lib/adhearsion/voip/asterisk/config_generators/agents.conf.rb +140 -0
  68. data/lib/adhearsion/voip/asterisk/config_generators/config_generator.rb +101 -0
  69. data/lib/adhearsion/voip/asterisk/config_generators/queues.conf.rb +250 -0
  70. data/lib/adhearsion/voip/asterisk/config_generators/voicemail.conf.rb +240 -0
  71. data/lib/adhearsion/voip/asterisk/config_manager.rb +71 -0
  72. data/lib/adhearsion/voip/asterisk/manager_interface.rb +562 -0
  73. data/lib/adhearsion/voip/asterisk/manager_interface/ami_lexer.rb +1754 -0
  74. data/lib/adhearsion/voip/asterisk/manager_interface/ami_lexer.rl.rb +286 -0
  75. data/lib/adhearsion/voip/asterisk/manager_interface/ami_messages.rb +78 -0
  76. data/lib/adhearsion/voip/asterisk/manager_interface/ami_protocol_lexer_machine.rl +87 -0
  77. data/lib/adhearsion/voip/asterisk/special_dial_plan_managers.rb +80 -0
  78. data/lib/adhearsion/voip/asterisk/super_manager.rb +19 -0
  79. data/lib/adhearsion/voip/call.rb +440 -0
  80. data/lib/adhearsion/voip/call_routing.rb +64 -0
  81. data/lib/adhearsion/voip/commands.rb +9 -0
  82. data/lib/adhearsion/voip/constants.rb +39 -0
  83. data/lib/adhearsion/voip/conveniences.rb +18 -0
  84. data/lib/adhearsion/voip/dial_plan.rb +218 -0
  85. data/lib/adhearsion/voip/dsl/dialing_dsl.rb +151 -0
  86. data/lib/adhearsion/voip/dsl/dialing_dsl/dialing_dsl_monkey_patches.rb +37 -0
  87. data/lib/adhearsion/voip/dsl/dialplan/control_passing_exception.rb +27 -0
  88. data/lib/adhearsion/voip/dsl/dialplan/dispatcher.rb +124 -0
  89. data/lib/adhearsion/voip/dsl/dialplan/parser.rb +71 -0
  90. data/lib/adhearsion/voip/dsl/dialplan/thread_mixin.rb +16 -0
  91. data/lib/adhearsion/voip/dsl/numerical_string.rb +117 -0
  92. data/lib/adhearsion/voip/freeswitch/basic_connection_manager.rb +48 -0
  93. data/lib/adhearsion/voip/freeswitch/event_handler.rb +58 -0
  94. data/lib/adhearsion/voip/freeswitch/freeswitch_dialplan_command_factory.rb +129 -0
  95. data/lib/adhearsion/voip/freeswitch/inbound_connection_manager.rb +38 -0
  96. data/lib/adhearsion/voip/freeswitch/oes_server.rb +195 -0
  97. data/lib/adhearsion/voip/menu_state_machine/calculated_match.rb +80 -0
  98. data/lib/adhearsion/voip/menu_state_machine/matchers.rb +123 -0
  99. data/lib/adhearsion/voip/menu_state_machine/menu_builder.rb +58 -0
  100. data/lib/adhearsion/voip/menu_state_machine/menu_class.rb +149 -0
  101. data/lib/theatre.rb +151 -0
  102. data/lib/theatre/README.markdown +64 -0
  103. data/lib/theatre/callback_definition_loader.rb +84 -0
  104. data/lib/theatre/guid.rb +23 -0
  105. data/lib/theatre/invocation.rb +121 -0
  106. data/lib/theatre/namespace_manager.rb +153 -0
  107. data/lib/theatre/version.rb +2 -0
  108. metadata +160 -140
  109. data/.version +0 -1
  110. data/TODO +0 -71
  111. data/ahn +0 -223
  112. data/apps/default/Rakefile +0 -65
  113. data/apps/default/config/adhearsion.sqlite3 +0 -0
  114. data/apps/default/config/adhearsion.yml +0 -95
  115. data/apps/default/config/database.rb +0 -50
  116. data/apps/default/config/database.yml +0 -12
  117. data/apps/default/config/helpers/drb_server.yml +0 -43
  118. data/apps/default/config/helpers/factorial.alien.c.yml +0 -1
  119. data/apps/default/config/helpers/growler.yml +0 -21
  120. data/apps/default/config/helpers/lookup.yml +0 -1
  121. data/apps/default/config/helpers/manager_proxy.yml +0 -8
  122. data/apps/default/config/helpers/micromenus.yml +0 -1
  123. data/apps/default/config/helpers/micromenus/collab.rb +0 -60
  124. data/apps/default/config/helpers/micromenus/images/arrow-off.gif +0 -0
  125. data/apps/default/config/helpers/micromenus/images/arrow-on.gif +0 -0
  126. data/apps/default/config/helpers/micromenus/images/error.gif +0 -0
  127. data/apps/default/config/helpers/micromenus/images/folder-off.gif +0 -0
  128. data/apps/default/config/helpers/micromenus/images/folder-on.gif +0 -0
  129. data/apps/default/config/helpers/micromenus/images/folder.png +0 -0
  130. data/apps/default/config/helpers/micromenus/images/ggbridge.jpg +0 -0
  131. data/apps/default/config/helpers/micromenus/images/green.png +0 -0
  132. data/apps/default/config/helpers/micromenus/images/microbrowser.bg.gif +0 -0
  133. data/apps/default/config/helpers/micromenus/images/red.png +0 -0
  134. data/apps/default/config/helpers/micromenus/images/tux.bmp +0 -0
  135. data/apps/default/config/helpers/micromenus/images/url-off.gif +0 -0
  136. data/apps/default/config/helpers/micromenus/images/url-on.gif +0 -0
  137. data/apps/default/config/helpers/micromenus/images/yellow.png +0 -0
  138. data/apps/default/config/helpers/micromenus/javascripts/animation.js +0 -1341
  139. data/apps/default/config/helpers/micromenus/javascripts/carousel.js +0 -1238
  140. data/apps/default/config/helpers/micromenus/javascripts/columnav.js +0 -306
  141. data/apps/default/config/helpers/micromenus/javascripts/connection.js +0 -965
  142. data/apps/default/config/helpers/micromenus/javascripts/container.js +0 -4727
  143. data/apps/default/config/helpers/micromenus/javascripts/container_core.js +0 -2915
  144. data/apps/default/config/helpers/micromenus/javascripts/dom.js +0 -892
  145. data/apps/default/config/helpers/micromenus/javascripts/dragdrop.js +0 -2958
  146. data/apps/default/config/helpers/micromenus/javascripts/event.js +0 -1771
  147. data/apps/default/config/helpers/micromenus/javascripts/yahoo.js +0 -433
  148. data/apps/default/config/helpers/micromenus/stylesheets/carousel.css +0 -78
  149. data/apps/default/config/helpers/micromenus/stylesheets/columnav.css +0 -135
  150. data/apps/default/config/helpers/micromenus/stylesheets/microbrowsers.css +0 -42
  151. data/apps/default/config/helpers/multi_messenger.yml +0 -9
  152. data/apps/default/config/helpers/weather.yml +0 -1
  153. data/apps/default/config/helpers/xbmc.yml +0 -2
  154. data/apps/default/config/migration.rb +0 -59
  155. data/apps/default/extensions.rb +0 -41
  156. data/apps/default/helpers/factorial.alien.c +0 -32
  157. data/apps/default/helpers/growler.rb +0 -53
  158. data/apps/default/helpers/lookup.rb +0 -44
  159. data/apps/default/helpers/manager_proxy.rb +0 -112
  160. data/apps/default/helpers/micromenus.rb +0 -514
  161. data/apps/default/helpers/multi_messenger.rb +0 -53
  162. data/apps/default/helpers/oscar_wilde_quotes.rb +0 -197
  163. data/apps/default/helpers/weather.rb +0 -85
  164. data/apps/default/helpers/xbmc.rb +0 -39
  165. data/apps/default/logs/adhearsion.log +0 -0
  166. data/apps/default/logs/database.log +0 -0
  167. data/lib/constants.rb +0 -24
  168. data/lib/core_extensions.rb +0 -180
  169. data/lib/drb_server.rb +0 -101
  170. data/lib/logging.rb +0 -85
  171. data/lib/phone_number.rb +0 -85
  172. data/lib/rami.rb +0 -823
  173. data/lib/servlet_container.rb +0 -174
  174. data/lib/sexy_migrations.rb +0 -70
  175. data/test/asterisk_module_test.rb +0 -14
  176. data/test/core_extensions_test.rb +0 -26
  177. data/test/dial_test.rb +0 -43
  178. data/test/specs/numerical_string_spec.rb +0 -53
  179. data/test/test_micromenus.rb +0 -0
@@ -0,0 +1,37 @@
1
+ module Adhearsion
2
+ module VoIP
3
+ module DSL
4
+ class DialingDSL
5
+ module MonkeyPatches
6
+ module RegexpMonkeyPatch
7
+
8
+ def |(other)
9
+ case other
10
+ when RouteRule
11
+ other.unshift_pattern self
12
+ other
13
+ when Regexp
14
+ RouteRule.new :patterns => [self, other]
15
+ else
16
+ raise ArgumentError, "Unsupported pattern type #{other.inspect}"
17
+ end
18
+ end
19
+
20
+ def >>(other)
21
+ case other
22
+ when ProviderDefinition
23
+ RouteRule.new :patterns => self, :providers => other
24
+ when RouteRule
25
+ returning other do |route|
26
+ route.unshift_pattern self
27
+ end
28
+ else raise ArgumentError, "Unsupported route definition #{other.inspect}"
29
+ end
30
+ end
31
+
32
+ end
33
+ end
34
+ end
35
+ end
36
+ end
37
+ end
@@ -0,0 +1,27 @@
1
+ module Adhearsion
2
+ module VoIP
3
+ module DSL
4
+ module Dialplan
5
+ # A ControlPassingException is used internally to stop execution of one
6
+ # dialplan context and begin execution of another proc instead. It is
7
+ # most notably used by the ~@ unary operator that can be called on a
8
+ # context name within a dialplan to transfer control entirely to that
9
+ # particular context. The serve() method in the servlet_container actually
10
+ # rescues these exceptions specifically and then does +e.target to execute
11
+ # that code.
12
+ class ControlPassingException < Exception
13
+
14
+ attr_reader :target
15
+
16
+ def initialize(target)
17
+ super()
18
+ @target = target
19
+ end
20
+
21
+ end
22
+
23
+ class ContextNotFoundException < Exception; end
24
+ end
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,124 @@
1
+ module Adhearsion
2
+ module VoIP
3
+ module DSL
4
+ module Dialplan
5
+
6
+ class ReturnValue < Exception
7
+ attr_reader :obj
8
+ def initialize(obj)
9
+ @obj = obj
10
+ super
11
+ end
12
+ end
13
+
14
+ class Hangup < Exception; end
15
+
16
+ # Instantiated and returned in every dialplan command
17
+ class EventCommand
18
+
19
+ attr_accessor :app, :args, :response_block, :returns, :on_keypress
20
+
21
+ def initialize(app, *args, &block)
22
+ @hash = args.pop if args.last.kind_of?(Hash)
23
+ @app, @args = app, args
24
+
25
+ if @hash
26
+ @returns = @hash[:returns] || :raw
27
+ @on_keypress = @hash[:on_keypress]
28
+ end
29
+
30
+ @response_block = block if block_given?
31
+ end
32
+
33
+ def on_keypress(&block)
34
+ block_given? ? @on_keypress = block : @on_keypress
35
+ end
36
+
37
+ def on_break(&block)
38
+ block_given? ? @on_break = block : @on_break
39
+ end
40
+
41
+ end
42
+
43
+ class NoOpEventCommand < EventCommand
44
+ attr_reader :timeout, :on_keypress
45
+ def initialize(timeout=nil, hash={})
46
+ @timeout = timeout
47
+ @on_keypress = hash[:on_keypress]
48
+ end
49
+ end
50
+
51
+ class ExitingEventCommand < EventCommand; end
52
+
53
+ # Serves as a EventCommand proxy for handling EventCommands. Useful
54
+ # if doing any pre-processing.
55
+ #
56
+ # For example, when sending commands over the event socket to FreeSwitch,
57
+ # every command must start with "api", but, when doing an originate, it
58
+ # must begin with an ampersand. These two pre-processors would be developed
59
+ # as separate CommandDispatchers.
60
+ class CommandDispatcher
61
+
62
+ attr_reader :factory, :context
63
+
64
+ def initialize(factory, context=nil)
65
+ @context = context
66
+ @factory = factory.new context
67
+ end
68
+
69
+ def dispatch!(event_command)
70
+ raise NotImplementedError, "Must subclass #{self.class} and override this!"
71
+ end
72
+
73
+ def method_missing(name, *args, &block)
74
+ *commands = *@factory.send(name, *args, &block)
75
+ commands.map do |command|
76
+ if command.kind_of? Proc
77
+ instance_eval(&command)
78
+ elsif command.kind_of? EventCommand
79
+ dispatched_command = dispatch! command
80
+ if command.response_block
81
+ while (new_cmd = command.response_block.call(dispatched_command)).kind_of? EventCommand
82
+ dispatched_command = dispatch! new_cmd
83
+ end
84
+ end
85
+ dispatched_command
86
+ else
87
+ command
88
+ end
89
+ end.last
90
+ rescue ReturnValue => r
91
+ return r.obj
92
+ end
93
+
94
+ def return!(obj)
95
+ raise DSL::Dialplan::ReturnValue.new(obj)
96
+ end
97
+
98
+ def break!(uuid=@context)
99
+ raise NotImplementedError, "Must subclass #{self.class} and override this!"
100
+ end
101
+
102
+ # Takes a Hash and meta_def()'s a method for each key that returns
103
+ # the key's value in the Hash.
104
+ def def_keys!(hash)
105
+ hash.each_pair do |k,v|
106
+ meta_def(k) { v } rescue nil
107
+ end
108
+ end
109
+
110
+ def clone
111
+ returning super do |nemesis|
112
+ instance_variables.each do |iv|
113
+ value = instance_variable_get(iv)
114
+ nemesis.instance_variable_set iv, value.clone if value
115
+ end
116
+ end
117
+ end
118
+
119
+ end
120
+
121
+ end
122
+ end
123
+ end
124
+ end
@@ -0,0 +1,71 @@
1
+ require 'ostruct'
2
+
3
+ module Adhearsion
4
+ module VoIP
5
+ module DSL
6
+ module Dialplan
7
+ #TODO: This is obsolete, but we still need it for Freeswitch until we port that to the new 0.8.0 APIs
8
+ module DialplanParser
9
+
10
+ # Create a container and then clone that. when the container is created
11
+ # it should have all the contexts meta_def'd into it. for each call coming
12
+ # in, the cloned copy has the variables meta_def'd and set as instance
13
+ # variables
14
+
15
+ #TODO: separate into smaller pieces
16
+ def self.get_contexts
17
+ envelope = ContextsEnvelope.new
18
+
19
+ dialplans = AHN_CONFIG.files_from_setting "paths", "dialplan"
20
+ ahn_log.dialplan.warn "No dialplan files were found!" if dialplans.empty?
21
+
22
+ returning({}) do |contexts|
23
+ dialplans.each do |file|
24
+ raise "Dialplan file #{file} does not exist!" unless File.exists? file
25
+ envelope.instance_eval do
26
+ eval File.read(file)
27
+ end
28
+ current_contexts = envelope.parsed_contexts
29
+ current_contexts.each_pair do |name, block|
30
+ if contexts.has_key? name
31
+ warn %'Dialplan context "#{name}" exists in both #{contexts[name].file} and #{file}.' +
32
+ %' Using the "#{name}" context from #{contexts[name].file}.'
33
+ else
34
+ contexts[name] = returning OpenStruct.new do |metadata|
35
+ metadata.file = file
36
+ metadata.name = name
37
+ metadata.block = block
38
+ end
39
+ end
40
+ end
41
+ end
42
+ end
43
+ end
44
+ end
45
+
46
+ class ContextsEnvelope
47
+
48
+ keep = %w"__send__ __id__ define_method instance_eval meta_def meta_eval metaclass"
49
+ (instance_methods - keep).each do |m|
50
+ undef_method m
51
+ end
52
+
53
+ def initialize
54
+ @parsed_contexts = {}
55
+ end
56
+
57
+ attr_reader :parsed_contexts
58
+
59
+ def method_missing(name, *args, &block)
60
+ super unless block_given?
61
+ @parsed_contexts[name] = block
62
+ meta_def(name) { block }
63
+ end
64
+
65
+ end
66
+
67
+ end
68
+
69
+ end
70
+ end
71
+ end
@@ -0,0 +1,16 @@
1
+ require 'ostruct'
2
+ module Adhearsion
3
+ module VoIP
4
+ module DSL
5
+ module Dialplan
6
+ module ThreadMixin
7
+
8
+ def call
9
+ @thread_call_struct ||= OpenStruct.new
10
+ end
11
+
12
+ end
13
+ end
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,117 @@
1
+ module Adhearsion
2
+ module VoIP
3
+ module DSL
4
+ # In Ruby, a number with a leading zero such as 023.45 or 023 does not evaluate as expected.
5
+ # In the case of the float 023.45, you get a syntax error. In the case of 023 the number is
6
+ # parsed as being in octal form and the number 19 is returned.
7
+ #
8
+ # In Adhearsion, various strings that are entirely numeric might start with zero and that zero
9
+ # should be preserved when converting that string of numbers into a number. The numerical string
10
+ # retains the zero while still allowing it to be compared to other numbers.
11
+ #
12
+ # [[I think this leading zero thing is only part of the reason that NumericalString exists. I'm
13
+ # currently writing tests for this leading zero stuff so I thought I'd dump some of my assumptions
14
+ # about it here in the documentation.]]
15
+ class NumericalString
16
+ class << self
17
+ def starts_with_leading_zero?(string)
18
+ string.to_s[/^0\d+/]
19
+ end
20
+ end
21
+
22
+ (instance_methods - %w"__id__ __send__ __real_num __real_string").each do |m|
23
+ undef_method m
24
+ end
25
+
26
+ attr_reader :__real_num, :__real_string
27
+
28
+ def initialize(str)
29
+ @__real_string = str.to_s
30
+ @__real_num = str.to_i if @__real_string =~ /^\d+$/
31
+ end
32
+
33
+ def method_missing(name, *args, &block)
34
+ @__real_string.__send__ name, *args, &block
35
+ end
36
+
37
+ def respond_to?(m)
38
+ @__real_string.respond_to?(m) || m == :__real_num || m == :__real_string
39
+ end
40
+
41
+ end
42
+
43
+ # The PhoneNumber class is used by one object throughout Adhearsion: the AGI
44
+ # "extension" variable. Using some clevery Ruby hackery, the Extension class allows
45
+ # dialplan writers to use the best of Fixnum and String usage such as
46
+ #
47
+ # - Dialing international numbers
48
+ # - Using a regexp in a case statement for "extension"
49
+ # - Using a numerical range against extension -- e.g. (100...200)
50
+ # - Using the thousands separator
51
+ class PhoneNumber < NumericalString
52
+
53
+ # Checks against a pattern identifying US local numbers (i.e numbers
54
+ # without an area code seven digits long)
55
+ def us_local_number?() to_s =~ Adhearsion::VoIP::Constants::US_LOCAL_NUMBER end
56
+
57
+ # Checks against a pattern identifying US domestic numbers.
58
+ def us_national_number?() to_s =~ Adhearsion::VoIP::Constants::US_NATIONAL_NUMBER end
59
+
60
+ # Checks against a pattern identifying an ISN number. See http://freenum.org
61
+ # for more info.
62
+ def isn?() to_s =~ Adhearsion::VoIP::Constants::ISN end
63
+
64
+ # Useful for dialing those 1-800-FUDGEME type numbers with letters in them. Letters
65
+ # in the argument will be converted to their appropriate keypad key.
66
+ def self.from_vanity str
67
+ str.gsub(/\W/, '').upcase.tr('A-Z', '22233344455566677778889999')
68
+ end
69
+
70
+ end
71
+
72
+ end
73
+ end
74
+ end
75
+
76
+ # These monkey patches are necessary for the NumericalString to work, unfortunately.
77
+ class Class
78
+ def alias_method_once(new_name, old_name)
79
+ unless instance_methods.include?(new_name.to_s)
80
+ alias_method(new_name, old_name)
81
+ end
82
+ end
83
+ end
84
+
85
+ [Object, Range, class << String; self; end].each do |klass|
86
+ klass.alias_method_once(:pre_modified_threequal, :===)
87
+ end
88
+
89
+ class Object
90
+ def ===(arg)
91
+ if arg.respond_to? :__real_string
92
+ arg = arg.__real_num if kind_of?(Numeric) || kind_of?(Range)
93
+ pre_modified_threequal arg
94
+ else
95
+ pre_modified_threequal arg
96
+ end
97
+ end
98
+ end
99
+
100
+ class Range
101
+ alias_method_once(:original_threequal, :===)
102
+ def ===(arg)
103
+ if (arg.respond_to? :__real_string) && !arg.__real_num.nil?
104
+ arg = arg.__real_num
105
+ original_threequal arg
106
+ else
107
+ original_threequal arg
108
+ end
109
+ end
110
+ end
111
+
112
+ class << String
113
+ alias_method_once(:original_threequal, :===)
114
+ def ===(arg)
115
+ arg.respond_to?(:__real_string) || original_threequal(arg)
116
+ end
117
+ end
@@ -0,0 +1,48 @@
1
+ module Adhearsion
2
+ module VoIP
3
+ module FreeSwitch
4
+
5
+ class BasicConnectionManager
6
+
7
+ def initialize(io)
8
+ @io = io
9
+ end
10
+
11
+ # The send-command operator
12
+ def <<(str)
13
+ @io.write str + "\n\n"
14
+ end
15
+
16
+ def get_header
17
+ separate_pairs get_raw_header
18
+ end
19
+
20
+ def get_raw_header
21
+ (returning [] do |lines|
22
+ until line = @io.gets and line.chomp.empty?
23
+ lines << line.chomp
24
+ end
25
+ end) * "\n"
26
+ end
27
+
28
+ def next_event
29
+ header = get_raw_header
30
+ length = header.first[/\d+$/].to_i
31
+ # puts "Reading an event of #{length} bytes"
32
+ separate_pairs @io.read(length)
33
+ end
34
+
35
+ def separate_pairs(lines)
36
+ lines.inject({}) do |h,line|
37
+ returning h do |hash|
38
+ k,v = line.split(/\s*:\s*/)
39
+ hash[k] = URI.unescape(v).strip if k && v
40
+ end
41
+ end
42
+ end
43
+
44
+ end
45
+
46
+ end
47
+ end
48
+ end