Module: Apirelio::Route

Defined in:
lib/apirelio/route.rb

Constant Summary collapse

UUID =
/\A[0-9a-f]{8}-[0-9a-f]{4}-[1-8][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}\z/i
ULID =
/\A[0-9A-HJKMNP-TV-Z]{26}\z/i
INTEGER =
/\A\d+\z/
HEX_IDENTIFIER =
/\A[0-9a-f]{16,}\z/i

Class Method Summary collapse

Class Method Details

.capture?(value, include_routes: [], exclude_routes: []) ⇒ Boolean

Returns:

  • (Boolean)


20
21
22
23
24
# File 'lib/apirelio/route.rb', line 20

def capture?(value, include_routes: [], exclude_routes: [])
  included = include_routes.empty? || include_routes.any? { |pattern| match?(value, pattern) }
  excluded = exclude_routes.any? { |pattern| match?(value, pattern) }
  included && !excluded
end

.match?(value, pattern) ⇒ Boolean

Returns:

  • (Boolean)


26
27
28
29
30
31
32
33
34
# File 'lib/apirelio/route.rb', line 26

def match?(value, pattern)
  route = normalize(value)
  expected = normalized_pattern(pattern)
  return route == expected unless expected.end_with?("/**")

  prefix = expected[0...-3]
  prefix = "/" if prefix.empty?
  route == prefix || route.start_with?(prefix == "/" ? "/" : "#{prefix}/")
end

.normalize(value) ⇒ Object



12
13
14
15
16
17
18
# File 'lib/apirelio/route.rb', line 12

def normalize(value)
  path = value.to_s.split(/[?#]/, 2).first.to_s.strip
  path = "/" if path.empty?
  path = "/#{path}" unless path.start_with?("/")
  normalized = path.split("/", -1).map { |segment| identifier?(segment) ? "{id}" : segment }.join("/")
  normalized[0, 500]
end