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.



142
143
144
145
146
147
148
# File 'lib/tina4/router.rb', line 142

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.



140
141
142
# File 'lib/tina4/router.rb', line 140

def handler
  @handler
end

#param_namesObject (readonly)

Returns the value of attribute param_names.



140
141
142
# File 'lib/tina4/router.rb', line 140

def param_names
  @param_names
end

#pathObject (readonly)

Returns the value of attribute path.



140
141
142
# File 'lib/tina4/router.rb', line 140

def path
  @path
end

#path_regexObject (readonly)

Returns the value of attribute path_regex.



140
141
142
# File 'lib/tina4/router.rb', line 140

def path_regex
  @path_regex
end

Instance Method Details

#match?(request_path) ⇒ Boolean

Returns params hash if matched, false otherwise

Returns:

  • (Boolean)


151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
# File 'lib/tina4/router.rb', line 151

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