Module: Beacon::PathNormalizer

Defined in:
lib/beacon/path_normalizer.rb

Overview

Path normalization fallback — normative, see .doc/definition/06-http-api.md.

Used when the host framework does not expose a route template. Rails apps should set env from the routes layer; everything else falls back to this heuristic.

Constant Summary collapse

NUMERIC =
/\A\d+\z/.freeze
UUID =
/\A[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}\z/.freeze
TOKEN =
/\A[A-Za-z0-9_\-]{22,}\z/.freeze
ID =
":id".freeze
UID =
":uuid".freeze
TOK =
":token".freeze

Class Method Summary collapse

Class Method Details

.normalize(method, raw_path) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/beacon/path_normalizer.rb', line 16

def self.normalize(method, raw_path)
  path = raw_path.to_s
  qs = path.index("?")
  path = path[0, qs] if qs

  segments = path.split("/", -1)
  segments.map! do |seg|
    if seg.empty?
      seg
    elsif seg.match?(NUMERIC)
      ID
    elsif seg.match?(UUID)
      UID
    elsif seg.match?(TOKEN)
      TOK
    else
      seg
    end
  end

  "#{method.to_s.upcase} #{segments.join("/")}"
end