Class: FayeRails::RackAdapter::RoutingExtension

Inherits:
Object
  • Object
show all
Defined in:
lib/faye-rails/rack_adapter.rb

Instance Method Summary collapse

Constructor Details

#initializeRoutingExtension

Returns a new instance of RoutingExtension


93
94
95
96
# File 'lib/faye-rails/rack_adapter.rb', line 93

def initialize
  @default = :allow
  @mappings = {}
end

Instance Method Details

#allow_unknown_channels!Object


128
129
130
# File 'lib/faye-rails/rack_adapter.rb', line 128

def allow_unknown_channels!
  @default = :allow
end

#block_unknown_channels!Object


120
121
122
# File 'lib/faye-rails/rack_adapter.rb', line 120

def block_unknown_channels!
  @default = :block
end

#drop_unknown_channels!Object


124
125
126
# File 'lib/faye-rails/rack_adapter.rb', line 124

def drop_unknown_channels!
  @default = :drop
end

#incoming(message, callback) ⇒ Object


98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/faye-rails/rack_adapter.rb', line 98

def incoming(message, callback)
  if message['channel'] == '/meta/subscribe'
    take_action_for message, callback, message['subscription']
  elsif message['channel'] == '/meta/unsubscribe'
    take_action_for message, callback, message['subscription']
  elsif FayeRails::Matcher.match? '/meta/*', message['channel']
    callback.call(message)
  elsif FayeRails::Matcher.match? '/service/**', message['channel']
    callback.call(message)
  else
    take_action_for message, callback, message['channel']
  end
end

#map(channel, controller) ⇒ Object


112
113
114
115
116
117
118
# File 'lib/faye-rails/rack_adapter.rb', line 112

def map(channel, controller)
  if FayeRails::Matcher.match? '/**', channel
    (@mappings[channel] ||= []) << controller
  else
    raise ArgumentError, "Invalid channel name: #{channel}"
  end
end

#take_action_for(message, callback, test = '') ⇒ Object


132
133
134
135
136
137
138
139
140
141
142
143
144
145
# File 'lib/faye-rails/rack_adapter.rb', line 132

def take_action_for(message, callback, test='')
  if @mappings.keys.select { |glob| FayeRails::Matcher.match? glob, test }.size > 0
    callback.call(message)
  elsif @default == :block
    message['error'] = "Permission denied"
    callback.call(message)
  elsif @default == :drop
    callback.call(nil)
  elsif @default == :allow
    callback.call(message)
  else
    callback.call(nil)
  end
end