Class: SitePrism::AddressableUrlMatcher

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

Overview

SitePrism::AddressableUrlMatcher

Used in a couple of places to allow users to …

Specify patterns for loading webpages
Pass in hashable args or query parameters for loading dynamic pages

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(pattern) ⇒ AddressableUrlMatcher

Returns a new instance of 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

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

Returns:

  • Hash



22
23
24
25
26
27
28
29
30
31
32
# File 'lib/site_prism/addressable_url_matcher.rb', line 22

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)

    Boolean



40
41
42
43
44
45
# File 'lib/site_prism/addressable_url_matcher.rb', line 40

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