freeswitcher 0.4.7 → 0.4.8

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -6,7 +6,7 @@ require 'fsr/listener/header_and_content_response.rb'
6
6
  module FSR
7
7
  module Listener
8
8
  class Inbound < EventMachine::Protocols::HeaderAndContentProtocol
9
- attr_reader :auth, :hooks, :server, :port
9
+ attr_reader :auth, :hooks, :server, :port, :subscribed_events, :subscribed_sub_events
10
10
 
11
11
  HOOKS = {}
12
12
 
@@ -15,6 +15,8 @@ module FSR
15
15
  @auth = args[:auth] || "ClueCon"
16
16
  @host = args[:host]
17
17
  @port = args[:port]
18
+ @subscribed_events = []
19
+ @subscribed_sub_events = []
18
20
  @hooks = {}
19
21
  end
20
22
 
@@ -47,19 +49,13 @@ module FSR
47
49
  def authorize_and_register_for_events
48
50
  FSR::Log.info "Connection established. Authorizing..."
49
51
  say("auth #{@auth}")
50
- add_class_hooks
51
52
  before_session
52
53
  end
53
54
 
54
55
  def before_session
55
56
  end
56
57
 
57
- def add_class_hooks
58
- HOOKS.each do |(key, value)|
59
- add_event(key, &value)
60
- end
61
- end
62
- private :before_session, :add_class_hooks
58
+ private :before_session
63
59
 
64
60
  # receive_request is the callback method when data is recieved from the socket
65
61
  #
@@ -71,12 +67,30 @@ module FSR
71
67
  event = HeaderAndContentResponse.new({:headers => hash_header, :content => hash_content})
72
68
  event_name = event.content[:event_name].to_s.strip
73
69
  unless event_name.empty?
70
+ # Special case for ALL in instance level @hooks
71
+ if hook = @hooks[:ALL]
72
+ hook.call(event)
73
+ end
74
+ # Special case for ALL in class level HOOKS
75
+ if hook = HOOKS[:ALL]
76
+ case hook.arity
77
+ when 1
78
+ hook.call(event)
79
+ when 2
80
+ hook.call(self, event)
81
+ end
82
+ end
83
+ # General event matching, only on Event-Name, for instance level @hooks
74
84
  if hook = @hooks[event_name.to_sym]
85
+ hook.call(event)
86
+ end
87
+ # General event matching, only on Event-Name, for class-level HOOKS
88
+ if hook = HOOKS[event_name.to_sym]
75
89
  case hook.arity
76
90
  when 1
77
- @hooks[event_name.to_sym].call(event)
91
+ hook.call(event)
78
92
  when 2
79
- @hooks[event_name.to_sym].call(self, event)
93
+ hook.call(self, event)
80
94
  end
81
95
  end
82
96
  end
@@ -90,6 +104,17 @@ module FSR
90
104
  send_data("#{line}\r\n\r\n")
91
105
  end
92
106
 
107
+ def subscribe_to_event(event, sub_events = [])
108
+ sub_events = [sub_events] unless sub_events.respond_to?(:each)
109
+ @subscribed_events << event
110
+ @subscribed_sub_events += sub_events
111
+ if custom = @subscribed_events.delete(:CUSTOM)
112
+ say "event plain #{@subscribed_events.join(" ")} CUSTOM #{@subscribed_sub_events.join(" ")}"
113
+ else
114
+ say "event plain #{@subscribed_events.join(" ")}"
115
+ end
116
+ end
117
+
93
118
  # api encapsulates #say("api blah") for the user
94
119
  #
95
120
  # param line Line of text to send to the socket proceeding api
@@ -109,7 +134,8 @@ module FSR
109
134
  #
110
135
  # @param event The event to trigger the block on. Examples, :CHANNEL_CREATE, :CHANNEL_DESTROY, etc
111
136
  # @param block The block to execute when the event is triggered
112
- def self.add_event_hook(event, &block)
137
+ def self.add_event_hook(event, sub_events = [], &block)
138
+ ObjectSpace.each_object { |e| e.subscribe_to_event(event, sub_events) if e.class.ancestors.include?(FSR::Listener::Inbound) }
113
139
  HOOKS[event] = block
114
140
  end
115
141
 
@@ -124,7 +150,8 @@ module FSR
124
150
  #
125
151
  # @param event The event to trigger the block on. Examples, :CHANNEL_CREATE, :CHANNEL_DESTROY, etc
126
152
  # @param block The block to execute when the event is triggered
127
- def add_event(event, &block)
153
+ def add_event(event, sub_events = [], &block)
154
+ subscribe_to_event(event, sub_events)
128
155
  @hooks[event] = block
129
156
  end
130
157
 
@@ -64,7 +64,7 @@ EM.describe InboundListener do
64
64
  done
65
65
  end
66
66
 
67
- should "be able to add custom event hooks in the pre_session" do
67
+ should "be able to add custom event hooks on instances in the pre_session (before_session)" do
68
68
  @listener.receive_data("Content-Length: 22\n\nEvent-Name: CUSTOM\n\n")
69
69
  @listener.custom_event.should.equal @listener.recvd_event.first
70
70
  @listener2.receive_data("Content-Length: 22\n\nEvent-Name: TEST_EVENT\n\n")
@@ -72,10 +72,17 @@ EM.describe InboundListener do
72
72
  done
73
73
  end
74
74
 
75
- should "be able to add custom event hooks" do
75
+ should "be able to add custom event hooks on classes, before instantiation" do
76
76
  FSL::Inbound.add_event_hook(:HANGUP_EVENT) { |instance, event| instance.test_event = event }
77
77
  listener = InboundListener2.new(1234, {:auth => 'SecretPassword'})
78
- listener.test_event.should.equal nil
78
+ listener.receive_data("Content-Length: 24\n\nEvent-Name: HANGUP_EVENT\n\n")
79
+ listener.test_event.content[:event_name].should.equal "HANGUP_EVENT"
80
+ done
81
+ end
82
+
83
+ should "be able to add custom event hooks on classes, after instantiation" do
84
+ listener = InboundListener2.new(1234, {:auth => 'SecretPassword'})
85
+ FSL::Inbound.add_event_hook(:HANGUP_EVENT) { |instance, event| instance.test_event = event }
79
86
  listener.receive_data("Content-Length: 24\n\nEvent-Name: HANGUP_EVENT\n\n")
80
87
  listener.test_event.content[:event_name].should.equal "HANGUP_EVENT"
81
88
  done
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: freeswitcher
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.7
4
+ version: 0.4.8
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jayson Vaughn