Class: SoarSc::Rack::Router::Route

Inherits:
Object
  • Object
show all
Defined in:
lib/soar_sc/rack/router/route.rb

Constant Summary collapse

PARAMETER_PREFIX =
":"
PARAMETER_VALID_REGEXP =
/:[a-z_][a-z0-9_]*/
REQUEST_METHOD =
"REQUEST_METHOD"
SCRIPT_NAME =
"SCRIPT_NAME"
PATH_INFO =
"PATH_INFO"
EMPTY_HASH =
{}.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(method, path, action) ⇒ Route

Returns a new instance of Route.



18
19
20
21
22
# File 'lib/soar_sc/rack/router/route.rb', line 18

def initialize(method, path, action)
  validate_path(path)
  @method, @path, @action = method, path, action
  @static = components(path).none? { |c| c.start_with?(PARAMETER_PREFIX) }
end

Instance Attribute Details

#actionObject (readonly)

Returns the value of attribute action.



16
17
18
# File 'lib/soar_sc/rack/router/route.rb', line 16

def action
  @action
end

#methodObject (readonly)

Returns the value of attribute method.



16
17
18
# File 'lib/soar_sc/rack/router/route.rb', line 16

def method
  @method
end

#pathObject (readonly)

Returns the value of attribute path.



16
17
18
# File 'lib/soar_sc/rack/router/route.rb', line 16

def path
  @path
end

Instance Method Details

#extract_path_parameters(env) ⇒ Object



41
42
43
44
45
46
47
48
49
50
# File 'lib/soar_sc/rack/router/route.rb', line 41

def extract_path_parameters(env)
  return EMPTY_HASH if @static

  req_components = components(request_path(env))
  parameters = {}
  components(path).each_with_index do |c, i|
    parameters[c[1..-1]] = req_components[i] if c.start_with?(PARAMETER_PREFIX)
  end
  parameters
end

#matches?(env) ⇒ Boolean

Returns:

  • (Boolean)


24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/soar_sc/rack/router/route.rb', line 24

def matches?(env)
  return false unless method == env[REQUEST_METHOD] or method == HttpMethod::ANY

  return request_path(env) == path if @static

  route_components = components(path)
  req_components = components(request_path(env))

  return false unless route_components.size == req_components.size

  route_components.each_with_index do |c, i|
    return false unless c.start_with?(PARAMETER_PREFIX) or c == req_components[i]
  end

  return true
end