Skip to content
This repository was archived by the owner on Nov 10, 2022. It is now read-only.
Merged
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
10 changes: 8 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,16 @@ techniques:
require 'librevox'

class MyInbound < Librevox::Listener::Inbound
# Events to listen for (default is ALL)
events ['CHANNEL_EXECUTE', 'CUSTOM foo']

# Filters to apply (optional)
filter 'Caller-Context' => ['default', 'example'], 'Caller-Privacy-Hide-Name' => 'no'

def on_event e
puts "Got event: #{e.content[:event_name]}"
end

# You can add a hook for a certain event:
event :channel_hangup do
# It is instance_eval'ed, so you can use your instance methods etc:
Expand All @@ -54,7 +60,7 @@ class MyInbound < Librevox::Listener::Inbound
event :channel_bridge do |e|
...
end

def do_something
...
end
Expand Down
2 changes: 0 additions & 2 deletions TODO
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@

- check command/application input and raise ArgumentError, possibly.

- filter events

- move test suite from bacon -> test/unit.

- test how this works in async mode - I imagine that it might mess with
Expand Down
25 changes: 24 additions & 1 deletion lib/librevox/listener/inbound.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,19 @@
module Librevox
module Listener
class Inbound < Base
class << self
attr_reader :subscribe_events
attr_reader :subscribe_filters

def events(events)
@subscribe_events = events
end

def filters(filters)
@subscribe_filters = filters
end
end

def initialize args={}
super

Expand All @@ -15,8 +28,18 @@ def initialize args={}
def connection_completed
Librevox.logger.info "Connected."
super

send_data "auth #{@auth}\n\n"
send_data "event plain ALL\n\n"

events = self.class.subscribe_events || ['ALL']
send_data "event plain #{events.join(' ')}\n\n"

filters = self.class.subscribe_filters || {}
filters.each do |header, values|
[*values].each do |value|
send_data "filter #{header} #{value}\n\n"
end
end
end

def unbind
Expand Down
26 changes: 26 additions & 0 deletions spec/librevox/listener/spec_inbound.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,12 @@
class InboundTestListener < Librevox::Listener::Inbound
end

class InboundFilterTestListener < Librevox::Listener::Inbound
events ['CUSTOM', 'CHANNEL_EXECUTE']

filters 'Caller-Context' => ['default', 'example'], 'Caller-Privacy-Hide-Name' => 'no'
end

describe "Inbound listener" do
before do
@listener = InboundTestListener.new(nil)
Expand All @@ -18,5 +24,25 @@ class InboundTestListener < Librevox::Listener::Inbound
@listener.connection_completed
@listener.outgoing_data.shift.should == "auth ClueCon\n\n"
@listener.outgoing_data.shift.should == "event plain ALL\n\n"
@listener.outgoing_data.shift.should == nil
end
end

describe "Inbound listener with filtering" do
before do
@listener = InboundFilterTestListener.new(nil)
end

behaves_like "events"
behaves_like "api commands"

should "authorize and subscribe to events" do
@listener.connection_completed
@listener.outgoing_data.shift.should == "auth ClueCon\n\n"
@listener.outgoing_data.shift.should == "event plain CUSTOM CHANNEL_EXECUTE\n\n"
@listener.outgoing_data.shift.should == "filter Caller-Context default\n\n"
@listener.outgoing_data.shift.should == "filter Caller-Context example\n\n"
@listener.outgoing_data.shift.should == "filter Caller-Privacy-Hide-Name no\n\n"
@listener.outgoing_data.shift.should == nil
end
end