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.



102
103
104
105
# File 'lib/faye-rails/rack_adapter.rb', line 102

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

Instance Method Details

#allow_unknown_channels!Object



137
138
139
# File 'lib/faye-rails/rack_adapter.rb', line 137

def allow_unknown_channels!
  @default = :allow
end

#block_unknown_channels!Object



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

def block_unknown_channels!
  @default = :block
end

#drop_unknown_channels!Object



133
134
135
# File 'lib/faye-rails/rack_adapter.rb', line 133

def drop_unknown_channels!
  @default = :drop
end

#incoming(message, callback) ⇒ Object



107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/faye-rails/rack_adapter.rb', line 107

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 File.fnmatch?('/meta/*', message['channel'])
    callback.call(message)
  elsif File.fnmatch?('/service/**', message['channel'])
    callback.call(message)
  else
    take_action_for message, callback, message['channel']
  end
end

#map(channel, controller) ⇒ Object



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

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

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



141
142
143
144
145
146
147
148
149
150
151
152
153
154
# File 'lib/faye-rails/rack_adapter.rb', line 141

def take_action_for(message, callback, test='')
  if @mappings.keys.select { |glob| File.fnmatch?(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