Class: Chespirito::RouteConstraintChecker

Inherits:
Object
  • Object
show all
Defined in:
lib/chespirito/routes/route_constraint_checker.rb

Instance Method Summary collapse

Constructor Details

#initialize(request) ⇒ RouteConstraintChecker

Returns a new instance of RouteConstraintChecker.



7
8
9
# File 'lib/chespirito/routes/route_constraint_checker.rb', line 7

def initialize(request)
  @request = request
end

Instance Method Details

#extract_params(route) ⇒ Object



18
19
20
21
22
23
24
25
# File 'lib/chespirito/routes/route_constraint_checker.rb', line 18

def extract_params(route)
  return {} unless route.respond_to?(:path)
  return {} unless match_route?(route)

  route_constraints(route)
    .zip(request_constraints(route))
    .to_h
end

#match_route?(route) ⇒ Boolean

Returns:

  • (Boolean)


11
12
13
14
15
16
# File 'lib/chespirito/routes/route_constraint_checker.rb', line 11

def match_route?(route)
  return false unless route.respond_to?(:path)
  return false if route_constraints(route).empty?

  route_constraints(route).size == request_constraints(route).size
end

#request_constraints(route) ⇒ Object



28
# File 'lib/chespirito/routes/route_constraint_checker.rb', line 28

def request_constraints(route) = request_parts - route_parts(route)

#request_partsObject



31
# File 'lib/chespirito/routes/route_constraint_checker.rb', line 31

def request_parts      = RouteUtils.words(@request.path)

#route_constraints(route) ⇒ Object



27
# File 'lib/chespirito/routes/route_constraint_checker.rb', line 27

def route_constraints(route)   = RouteUtils.constraints(route.path)

#route_parts(route) ⇒ Object



30
# File 'lib/chespirito/routes/route_constraint_checker.rb', line 30

def route_parts(route) = RouteUtils.parts(route.path)