Class: FayeRails::Filter::DSL
- Inherits:
-
Object
- Object
- FayeRails::Filter::DSL
- Defined in:
- lib/faye-rails/filter.rb
Instance Attribute Summary collapse
-
#callback ⇒ Object
readonly
A small wrapper class around filter blocks to add some sugar to ease filter (Faye extension) creation.
-
#channel ⇒ Object
readonly
A small wrapper class around filter blocks to add some sugar to ease filter (Faye extension) creation.
-
#direction ⇒ Object
readonly
A small wrapper class around filter blocks to add some sugar to ease filter (Faye extension) creation.
-
#message ⇒ Object
readonly
A small wrapper class around filter blocks to add some sugar to ease filter (Faye extension) creation.
-
#original_message ⇒ Object
readonly
A small wrapper class around filter blocks to add some sugar to ease filter (Faye extension) creation.
Instance Method Summary collapse
-
#block(reason = "Message blocked by filter") ⇒ Object
Syntactic sugar around callback.call which adds an error message to the message and passes it back to Faye, which will send back a rejection message to the sending client.
- #channel_matches?(glob, test) ⇒ Boolean
- #client_id?(x = nil) ⇒ Boolean
- #data ⇒ Object
- #data? ⇒ Boolean
-
#drop ⇒ Object
Syntactic sugar around callback.call which returns nil to Faye - effectively dropping the message.
- #incoming? ⇒ Boolean (also: #in?)
-
#initialize(block, message, channel = '/**', callback, direction) ⇒ DSL
constructor
Called by FayeRails::Filter when Faye passes messages in for evaluation.
- #meta? ⇒ Boolean
-
#modify(new_message) ⇒ Object
Syntactic sugar around callback.call which passes the passed argument back to Faye in place of the original message.
- #outgoing? ⇒ Boolean (also: #out?)
-
#pass ⇒ Object
Syntactic sugar around callback.call which passes back the original message unmodified.
- #service? ⇒ Boolean
-
#subscribing? ⇒ Boolean
Easier than testing message every time.
- #subscription?(channel) ⇒ Boolean
- #unsubscribing? ⇒ Boolean
Constructor Details
#initialize(block, message, channel = '/**', callback, direction) ⇒ DSL
Called by FayeRails::Filter when Faye passes messages in for evaluation.
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 |
# File 'lib/faye-rails/filter.rb', line 75 def initialize(block, , channel='/**', callback, direction) raise ArgumentError, "Block cannot be nil" unless block @channel = channel @original_message = .dup @message = @callback = callback @direction = direction if channel_matches?(@channel, @original_message['channel']) || (subscribing? && subscription?(@channel)) || (unsubscribing? && subscription?(@channel)) instance_eval(&block) else pass end end |
Instance Attribute Details
#callback ⇒ Object (readonly)
A small wrapper class around filter blocks to add some sugar to ease filter (Faye extension) creation.
66 67 68 |
# File 'lib/faye-rails/filter.rb', line 66 def callback @callback end |
#channel ⇒ Object (readonly)
A small wrapper class around filter blocks to add some sugar to ease filter (Faye extension) creation.
66 67 68 |
# File 'lib/faye-rails/filter.rb', line 66 def channel @channel end |
#direction ⇒ Object (readonly)
A small wrapper class around filter blocks to add some sugar to ease filter (Faye extension) creation.
66 67 68 |
# File 'lib/faye-rails/filter.rb', line 66 def direction @direction end |
#message ⇒ Object (readonly)
A small wrapper class around filter blocks to add some sugar to ease filter (Faye extension) creation.
66 67 68 |
# File 'lib/faye-rails/filter.rb', line 66 def @message end |
#original_message ⇒ Object (readonly)
A small wrapper class around filter blocks to add some sugar to ease filter (Faye extension) creation.
66 67 68 |
# File 'lib/faye-rails/filter.rb', line 66 def @original_message end |
Instance Method Details
#block(reason = "Message blocked by filter") ⇒ Object
Syntactic sugar around callback.call which adds an error message to the message and passes it back to Faye, which will send back a rejection message to the sending client.
164 165 166 167 168 |
# File 'lib/faye-rails/filter.rb', line 164 def block(reason="Message blocked by filter") = ['error'] = reason callback.call() end |
#channel_matches?(glob, test) ⇒ Boolean
135 136 137 |
# File 'lib/faye-rails/filter.rb', line 135 def channel_matches?(glob,test) FayeRails::Matcher.match? glob, test end |
#client_id?(x = nil) ⇒ Boolean
127 128 129 130 131 132 133 |
# File 'lib/faye-rails/filter.rb', line 127 def client_id?(x=nil) if !!x ['client_id'] == x else !!['client_id'] end end |
#data ⇒ Object
119 120 121 |
# File 'lib/faye-rails/filter.rb', line 119 def data ['data'] end |
#data? ⇒ Boolean
123 124 125 |
# File 'lib/faye-rails/filter.rb', line 123 def data? !!data end |
#drop ⇒ Object
Syntactic sugar around callback.call which returns nil to Faye - effectively dropping the message.
172 173 174 |
# File 'lib/faye-rails/filter.rb', line 172 def drop callback.call(nil) end |
#incoming? ⇒ Boolean Also known as: in?
109 110 111 |
# File 'lib/faye-rails/filter.rb', line 109 def incoming? direction == :incoming end |
#meta? ⇒ Boolean
101 102 103 |
# File 'lib/faye-rails/filter.rb', line 101 def ['channel'][0..5] == '/meta/' end |
#modify(new_message) ⇒ Object
Syntactic sugar around callback.call which passes the passed argument back to Faye in place of the original message.
154 155 156 |
# File 'lib/faye-rails/filter.rb', line 154 def modify() callback.call() end |
#outgoing? ⇒ Boolean Also known as: out?
114 115 116 |
# File 'lib/faye-rails/filter.rb', line 114 def outgoing? direction == :outgoing end |
#pass ⇒ Object
Syntactic sugar around callback.call which passes back the original message unmodified.
145 146 147 |
# File 'lib/faye-rails/filter.rb', line 145 def pass callback.call() end |
#service? ⇒ Boolean
105 106 107 |
# File 'lib/faye-rails/filter.rb', line 105 def service? ['channel'][0..8] == '/service/' end |
#subscribing? ⇒ Boolean
Easier than testing message every time
93 94 95 |
# File 'lib/faye-rails/filter.rb', line 93 def subscribing? ['channel'] == '/meta/subscribe' end |
#subscription?(channel) ⇒ Boolean
139 140 141 |
# File 'lib/faye-rails/filter.rb', line 139 def subscription?(channel) ['subscription'] && channel_matches?(channel, ['subscription']) end |
#unsubscribing? ⇒ Boolean
97 98 99 |
# File 'lib/faye-rails/filter.rb', line 97 def unsubscribing? ['channel'] == '/meta/unsubscribe' end |