Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions lib/librevox/commands.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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: <count>".
# Returns how many tasks were deleted, from FreeSWITCH's
# "+OK Deleted: <count>".
# @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.
Expand Down
11 changes: 9 additions & 2 deletions lib/librevox/listener/inbound.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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}"
Expand Down
35 changes: 35 additions & 0 deletions test/functional/librevox/listener/inbound_filter_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
21 changes: 17 additions & 4 deletions test/unit/librevox/commands_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Loading