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.



155
156
157
158
159
160
161
# File 'lib/tina4/router.rb', line 155

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.



153
154
155
# File 'lib/tina4/router.rb', line 153

def handler
  @handler
end

#param_namesObject (readonly)

Returns the value of attribute param_names.



153
154
155
# File 'lib/tina4/router.rb', line 153

def param_names
  @param_names
end

#pathObject (readonly)

Returns the value of attribute path.



153
154
155
# File 'lib/tina4/router.rb', line 153

def path
  @path
end

#path_regexObject (readonly)

Returns the value of attribute path_regex.



153
154
155
# File 'lib/tina4/router.rb', line 153

def path_regex
  @path_regex
end

Instance Method Details

#match?(request_path) ⇒ Boolean

Returns params hash if matched, false otherwise

Returns:

  • (Boolean)


164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
# File 'lib/tina4/router.rb', line 164

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