diff --git a/README.md b/README.md index eeecbdf..6f1015c 100644 --- a/README.md +++ b/README.md @@ -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: @@ -54,7 +60,7 @@ class MyInbound < Librevox::Listener::Inbound event :channel_bridge do |e| ... end - + def do_something ... end diff --git a/TODO b/TODO index c18d42f..3e09ef6 100644 --- a/TODO +++ b/TODO @@ -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 diff --git a/lib/librevox/listener/inbound.rb b/lib/librevox/listener/inbound.rb index 96165ae..08f6228 100644 --- a/lib/librevox/listener/inbound.rb +++ b/lib/librevox/listener/inbound.rb @@ -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 @@ -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 diff --git a/spec/librevox/listener/spec_inbound.rb b/spec/librevox/listener/spec_inbound.rb index 03429af..7da7540 100644 --- a/spec/librevox/listener/spec_inbound.rb +++ b/spec/librevox/listener/spec_inbound.rb @@ -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) @@ -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