Class: URIPattern::PatternString

Inherits:
Object
  • Object
show all
Includes:
Canonicalization
Defined in:
lib/uri_pattern/pattern_string.rb

Overview

Generates the WHATWG “component pattern string” returned by the component getters (protocol, hostname, pathname, …). It parses the raw component pattern into a part list — applying the same per-component canonicalization used for matching — and re-serialises it (“generate a pattern string”), so wildcards become “*”, hostnames are punycoded, fixed text is percent-encoded, redundant “{}” groups are dropped, and so on.

This is a port of the path-to-regexp-derived parse()/partsToPattern() used by the reference URLPattern implementation.

Defined Under Namespace

Classes: AdaptedToken, Part

Constant Summary collapse

FULL_WILDCARD_REGEXP =
".*"
IDENTIFIER_PART =

Identifier continuation code points. The reference uses /[$_‌‍pID_Continue]/u; in Ruby “_”, ZWNJ and ZWJ are already in pID_Continue, so only “$” needs to be added (avoids a duplicate-range warning).

/[$\p{ID_Continue}]/u

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Canonicalization

#canonicalize_hostname, #canonicalize_ipv6, #canonicalize_port, #encode_run

Constructor Details

#initialize(pattern_string, component:, opaque_path: false, ipv6: false) ⇒ PatternString

Returns a new instance of PatternString.



33
34
35
36
37
38
39
40
# File 'lib/uri_pattern/pattern_string.rb', line 33

def initialize(pattern_string, component:, opaque_path: false, ipv6: false)
  @input = pattern_string
  @component = component
  @opaque_path = opaque_path
  @ipv6 = ipv6
  @delimiter, @prefixes = options_for(component, opaque_path)
  @segment_wildcard_regexp = "[^#{escape_regexp_string(@delimiter)}]+?"
end

Class Method Details

.generate(pattern_string, component:, opaque_path: false, ipv6: false) ⇒ Object



29
30
31
# File 'lib/uri_pattern/pattern_string.rb', line 29

def self.generate(pattern_string, component:, opaque_path: false, ipv6: false)
  new(pattern_string, component: component, opaque_path: opaque_path, ipv6: ipv6).generate
end

Instance Method Details

#generateObject



42
43
44
# File 'lib/uri_pattern/pattern_string.rb', line 42

def generate
  parts_to_pattern(parse)
end