Class: URIPattern

Inherits:
Object
  • Object
show all
Defined in:
lib/uri_pattern.rb,
lib/uri_pattern/version.rb,
lib/uri_pattern/compiler.rb,
lib/uri_pattern/tokenizer.rb,
lib/uri_pattern/url_parser.rb,
lib/uri_pattern/match_result.rb,
lib/uri_pattern/pattern_string.rb,
lib/uri_pattern/canonicalization.rb,
lib/uri_pattern/component_pattern.rb

Defined Under Namespace

Modules: Canonicalization, URLParser Classes: Compiler, ComponentPattern, ComponentResult, ConstructorStringParser, Error, MatchResult, PatternString, Tokenizer

Constant Summary collapse

COMPONENT_KEYS =
%i[protocol username password hostname port pathname query fragment].freeze
COMPONENT_DEFAULTS =
{
  protocol: "*",
  username: "*",
  password: "*",
  hostname: "*",
  port:     "*",
  pathname: "*",
  query:    "*",
  fragment: "*"
}.freeze
SPECIAL_SCHEMES =
%w[http https ws wss ftp file].freeze
ESCAPED_AUTHORITY =

Authority components inherit the base_url value verbatim (already a literal string); path components inherit it as an escaped pattern string.

%i[protocol hostname port].freeze
ESCAPED_PATH =
%i[pathname query fragment].freeze
IGNORE_CASE_COMPONENTS =

ignoreCase only applies to these three components (per the spec’s create algorithm, which only mixes ignoreCaseOptions into pathname/search/hash).

%i[pathname query fragment].freeze
VERSION =
"0.1.0"

Instance Method Summary collapse

Constructor Details

#initialize(input = {}, base_url = nil, ignore_case: false) ⇒ URIPattern

Returns a new instance of URIPattern.



38
39
40
41
42
43
44
# File 'lib/uri_pattern.rb', line 38

def initialize(input = {}, base_url = nil, ignore_case: false)
  if input.is_a?(Hash)
    init_from_hash(input, base_url, ignore_case: ignore_case)
  else
    init_from_string(input.to_s, base_url, ignore_case: ignore_case)
  end
end

Instance Method Details

#match(input, base_url = nil) ⇒ Object



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/uri_pattern.rb', line 57

def match(input, base_url = nil)
  components = parse_input(input, base_url)
  return nil unless components

  results = {}
  COMPONENT_KEYS.each do |key|
    value = components[key] || ""
    groups = @patterns[key].groups_for(value)
    return nil unless groups
    results[key] = URIPattern::ComponentResult.new(input: value, groups: groups)
  end

  URIPattern::MatchResult.new(
    inputs: base_url.nil? ? [input] : [input, base_url],
    protocol:  results[:protocol],
    username:  results[:username],
    password:  results[:password],
    hostname:  results[:hostname],
    port:      results[:port],
    pathname:  results[:pathname],
    query:     results[:query],
    fragment:  results[:fragment]
  )
end

#match?(input, base_url = nil) ⇒ Boolean

Returns:

  • (Boolean)


46
47
48
49
50
51
52
53
54
55
# File 'lib/uri_pattern.rb', line 46

def match?(input, base_url = nil)
  components = parse_input(input, base_url)
  return false unless components

  COMPONENT_KEYS.all? do |key|
    @patterns[key].match(components[key] || "")
  end
rescue RegexpError => e
  raise URIPattern::Error, e.message
end