Class: SiteHub::LocationRewriter

Inherits:
Object
  • Object
show all
Includes:
Equality
Defined in:
lib/sitehub/location_rewriter.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Equality

#==, #_clazz, included

Constructor Details

#initialize(matcher, path_template) ⇒ LocationRewriter

Returns a new instance of LocationRewriter.



8
9
10
11
# File 'lib/sitehub/location_rewriter.rb', line 8

def initialize(matcher, path_template)
  @matcher = matcher
  @path_template = path_template
end

Instance Attribute Details

#matcherObject (readonly)

Returns the value of attribute matcher.



6
7
8
# File 'lib/sitehub/location_rewriter.rb', line 6

def matcher
  @matcher
end

#path_templateObject (readonly)

Returns the value of attribute path_template.



6
7
8
# File 'lib/sitehub/location_rewriter.rb', line 6

def path_template
  @path_template
end

Instance Method Details

#apply(downstream_url, source_url) ⇒ Object



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

def apply(downstream_url, source_url)
  url_components = matcher.match(downstream_url).captures

  path = path_template.dup.tap do |template|
    url_components.each.with_index(1) do |component, index|
      template.gsub!(RequestMapping::CAPTURE_GROUP_REFERENCE % index, component)
    end
  end

  "#{source_url[RequestMapping::BASE_URL_MATCHER]}#{path}"
end

#match?(url) ⇒ Boolean

Returns:

  • (Boolean)


13
14
15
# File 'lib/sitehub/location_rewriter.rb', line 13

def match?(url)
  matcher.match(url).is_a?(MatchData)
end