diff --git a/lib/librevox/commands.rb b/lib/librevox/commands.rb index 40b7fff..4b8fa14 100644 --- a/lib/librevox/commands.rb +++ b/lib/librevox/commands.rb @@ -85,12 +85,13 @@ def sched_hangup(time, uuid, cause = nil) # Delete a scheduled task by its id, or every task in a group. A call's # scheduled tasks (sched_hangup, sched_transfer, …) are grouped by its uuid. - # FreeSWITCH replies "+OK Deleted: ". + # Returns how many tasks were deleted, from FreeSWITCH's + # "+OK Deleted: ". # @example - # socket.sched_del "592567a2-1be4-11df-a036-19bfdab2092f" + # socket.sched_del "592567a2-1be4-11df-a036-19bfdab2092f" # => 1 # @see http://wiki.freeswitch.org/wiki/Mod_commands#sched_del def sched_del(id) - command "sched_del", id + command("sched_del", id).content[/\A\+OK Deleted: (\d+)/, 1].to_i end # Bridge two call legs together. At least one leg must be answered. diff --git a/lib/librevox/listener/inbound.rb b/lib/librevox/listener/inbound.rb index 7517bef..a6c94c7 100644 --- a/lib/librevox/listener/inbound.rb +++ b/lib/librevox/listener/inbound.rb @@ -11,8 +11,14 @@ def events(events) @subscribe_events = events end - def filters(filters) - @subscribe_filters = filters + # The header filters to add on connecting, as a hash, or a block that + # returns one when the connection starts (for values not known when + # the class loads). + # @example + # filters "Event-Name" => "CHANNEL_PARK" + # filters { { "variable_app" => App.current.name } } + def filters(filters = nil, &block) + @subscribe_filters = block || filters end end @@ -35,6 +41,7 @@ def run_session # connection has no filters, so subscribing first would let the whole # node's events through until the filters land. filters = self.class.subscribe_filters || {} + filters = instance_exec(&filters) if filters.is_a?(Proc) filters.each do |header, values| [*values].each do |value| send_message "filter #{header} #{value}" diff --git a/test/functional/librevox/listener/inbound_filter_test.rb b/test/functional/librevox/listener/inbound_filter_test.rb index cf770fc..06ad891 100644 --- a/test/functional/librevox/listener/inbound_filter_test.rb +++ b/test/functional/librevox/listener/inbound_filter_test.rb @@ -46,3 +46,38 @@ def test_sends_auth_then_filters_then_events assert_nil @listener.outgoing_data.shift end end + +class InboundBlockFilterTestListener < Librevox::Listener::Inbound + events ['CUSTOM'] + + filters { { 'Caller-Context' => context } } + + def context + 'evaluated-on-connect' + end +end + +class TestInboundListenerWithBlockFilters < Minitest::Test + prepend Librevox::Test::AsyncTest + include Librevox::Test::ListenerHelpers + + def setup + @listener = InboundBlockFilterTestListener.new(MockConnection.new) + @session_task = Async { @listener.run_session } + command_reply "Reply-Text" => "+OK accepted" + command_reply "Reply-Text" => "+OK filter added" + command_reply "Reply-Text" => "+OK event listener enabled plain" + end + + def teardown + @session_task&.stop + super + end + + # The block runs on the listener when the connection starts. + def test_a_filter_block_is_evaluated_on_connect + assert_equal "auth ClueCon", @listener.outgoing_data.shift + assert_equal "filter Caller-Context evaluated-on-connect", @listener.outgoing_data.shift + assert_equal "event plain CUSTOM", @listener.outgoing_data.shift + end +end diff --git a/test/unit/librevox/commands_test.rb b/test/unit/librevox/commands_test.rb index f7e65d0..2c17686 100644 --- a/test/unit/librevox/commands_test.rb +++ b/test/unit/librevox/commands_test.rb @@ -110,9 +110,22 @@ def test_sched_hangup assert_equal "+3600 1234-abcd ALLOTTED_TIMEOUT", cmd[:args] end - def test_sched_del - cmd = C.sched_del "1234-abcd" - assert_equal "sched_del", cmd[:name] - assert_equal "1234-abcd", cmd[:args] + # sched_del reads its count from FreeSWITCH's reply. + module SchedDelReply + include Librevox::Commands + + extend self + + attr_accessor :sent + + def command(name, args = "") + self.sent = [name, args] + Librevox::Protocol::Response.new("Content-Type: api/response", "+OK Deleted: 1\n") + end + end + + def test_sched_del_returns_how_many_tasks_it_deleted + assert_equal 1, SchedDelReply.sched_del("1234-abcd") + assert_equal ["sched_del", "1234-abcd"], SchedDelReply.sent end end