Class: Tina4::WebSocketRoute

Inherits:
Object
  • Object
show all
Defined in:
lib/tina4/router.rb

Overview

A registered WebSocket route with path pattern matching (reuses Route’s compile logic)

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path, handler) ⇒ WebSocketRoute

Returns a new instance of WebSocketRoute.



176
177
178
179
180
181
182
# File 'lib/tina4/router.rb', line 176

def initialize(path, handler)
  @path = normalize_path(path).freeze
  @handler = handler
  @param_names = []
  @path_regex = compile_pattern(@path)
  @param_names.freeze
end

Instance Attribute Details

#handlerObject (readonly)

Returns the value of attribute handler.



174
175
176
# File 'lib/tina4/router.rb', line 174

def handler
  @handler
end

#param_namesObject (readonly)

Returns the value of attribute param_names.



174
175
176
# File 'lib/tina4/router.rb', line 174

def param_names
  @param_names
end

#pathObject (readonly)

Returns the value of attribute path.



174
175
176
# File 'lib/tina4/router.rb', line 174

def path
  @path
end

#path_regexObject (readonly)

Returns the value of attribute path_regex.



174
175
176
# File 'lib/tina4/router.rb', line 174

def path_regex
  @path_regex
end

Instance Method Details

#match?(request_path) ⇒ Boolean

Returns params hash if matched, false otherwise

Returns:

  • (Boolean)


185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
# File 'lib/tina4/router.rb', line 185

def match?(request_path)
  match = @path_regex.match(request_path)
  return false unless match

  if @param_names.empty?
    {}
  else
    params = {}
    @param_names.each_with_index do |param_def, i|
      raw_value = match[i + 1]
      params[param_def[:name]] = raw_value
    end
    params
  end
end