Class: SitePrism::AddressableUrlMatcher

Inherits:
Object
  • Object
show all
Defined in:
lib/site_prism/addressable_url_matcher.rb

Constant Summary collapse

COMPONENT_NAMES =
%w(scheme user password host port path query fragment).map(&:to_sym).freeze
COMPONENT_PREFIXES =
{
  query: '?',
  fragment: '#'
}.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(pattern) ⇒ AddressableUrlMatcher


14
15
16
# File 'lib/site_prism/addressable_url_matcher.rb', line 14

def initialize(pattern)
  @pattern = pattern
end

Instance Attribute Details

#patternObject (readonly)

Returns the value of attribute pattern


12
13
14
# File 'lib/site_prism/addressable_url_matcher.rb', line 12

def pattern
  @pattern
end

Instance Method Details

#mappings(url) ⇒ Object

or nil if the URL doesn't conform to the matcher template.


20
21
22
23
24
25
26
27
28
29
# File 'lib/site_prism/addressable_url_matcher.rb', line 20

def mappings(url)
  uri = Addressable::URI.parse(url)
  result = {}
  COMPONENT_NAMES.each do |component|
    component_result = component_matches(component, uri)
    result = component_result ? result.merge!(component_result) : nil
    break unless result
  end
  result
end

#matches?(url, expected_mappings = {}) ⇒ Boolean

Determine whether URL matches our pattern, and optionally whether the extracted mappings match a hash of expected values. You can specify values as strings, numbers or regular expressions.


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

def matches?(url, expected_mappings = {})
  actual_mappings = mappings(url)
  if actual_mappings
    expected_mappings.empty? ? true : all_expected_mappings_match?(expected_mappings, actual_mappings)
  else
    false
  end
end