Class: SitePrism::AddressableUrlMatcher

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(pattern) ⇒ AddressableUrlMatcher

Returns a new instance of AddressableUrlMatcher.



10
11
12
# File 'lib/site_prism/addressable_url_matcher.rb', line 10

def initialize(pattern)
  @pattern = pattern
end

Instance Attribute Details

#patternObject (readonly)

Returns the value of attribute pattern.



8
9
10
# File 'lib/site_prism/addressable_url_matcher.rb', line 8

def pattern
  @pattern
end

Instance Method Details

#mappings(url) ⇒ Object

parsing the provided URL according to our pattern, or nil if the URL doesn’t conform to the matcher template.

Returns:

  • the hash of extracted mappings from



17
18
19
20
21
22
23
24
25
26
27
# File 'lib/site_prism/addressable_url_matcher.rb', line 17

def mappings(url)
  uri = Addressable::URI.parse(url)
  result = {}
  component_names.each do |component|
    component_result = component_matches(component, uri)
    return nil unless component_result

    result.merge!(component_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.

Returns:

  • (Boolean)


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

def matches?(url, expected_mappings = {})
  actual_mappings = mappings(url)
  return false unless actual_mappings

  expected_mappings.empty? ||
    all_expected_mappings_match?(expected_mappings, actual_mappings)
end