Class: URIPattern::ComponentPattern

Inherits:
Object
  • Object
show all
Defined in:
lib/uri_pattern/component_pattern.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(pattern_string, component:, ignore_case: false, opaque_path: false) ⇒ ComponentPattern

Returns a new instance of ComponentPattern.



7
8
9
10
11
12
13
14
15
16
17
18
# File 'lib/uri_pattern/component_pattern.rb', line 7

def initialize(pattern_string, component:, ignore_case: false, opaque_path: false)
  tokens = Tokenizer.new(pattern_string, policy: :strict).tokenize
  ipv6 = component == :hostname && ipv6_hostname_pattern?(pattern_string)
  compiled = Compiler.new(tokens, component: component, ignore_case: ignore_case,
                           opaque_path: opaque_path, ipv6: ipv6).compile
  @regexp = compiled[:regexp]
  @wildcard_name_map = compiled[:wildcard_name_map]
  # The getter exposes the canonicalized "component pattern string", not the
  # raw input (see PatternString).
  @pattern = PatternString.generate(pattern_string, component: component,
                                    opaque_path: opaque_path, ipv6: ipv6)
end

Instance Attribute Details

#patternObject (readonly)

Returns the value of attribute pattern.



5
6
7
# File 'lib/uri_pattern/component_pattern.rb', line 5

def pattern
  @pattern
end

Instance Method Details

#groups_for(string) ⇒ Object



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

def groups_for(string)
  md = @regexp.match(string)
  return nil unless md
  caps = md.named_captures
  @wildcard_name_map.each do |internal, external|
    caps[external] = caps.delete(internal) if caps.key?(internal)
  end
  caps
end

#ipv6_hostname_pattern?(str) ⇒ Boolean

WHATWG “hostname pattern is an IPv6 address”: true when the pattern starts with “[” (optionally wrapped in a “{” group). Such hostnames use the IPv6 encode callback (lowercase hex + char validation) instead of host parsing.

Returns:

  • (Boolean)


23
24
25
26
# File 'lib/uri_pattern/component_pattern.rb', line 23

def ipv6_hostname_pattern?(str)
  return false if str.length < 2
  str[0] == "[" || (str[0] == "{" && str[1] == "[")
end

#match(string) ⇒ Object



28
29
30
# File 'lib/uri_pattern/component_pattern.rb', line 28

def match(string)
  @regexp.match(string)
end