Class: Sparrow::RouteParser

Inherits:
Object
  • Object
show all
Defined in:
lib/sparrow/route_parser.rb

Overview

Simple class the takes a list of routes and provides check methods to determine if a path is excluded (ignored) or not, i.e. the path is within the list as defined on initialization.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(excluded_routes = Sparrow.configuration.excluded_routes) ⇒ RouteParser

Create a new Routeparser.

Parameters:

  • excluded_routes (Array<String, Regexp>) (defaults to: Sparrow.configuration.excluded_routes)

    A list of routes (path) that shall not be parsed. Each route must not start with a leading slash (/).



14
15
16
17
18
19
20
21
22
# File 'lib/sparrow/route_parser.rb', line 14

def initialize(excluded_routes = Sparrow.configuration.excluded_routes)
  self.excluded_routes = excluded_routes.map do |route|
    if route.is_a?(Regexp)
      route
    else
      Regexp.new(route.to_s)
    end
  end
end

Instance Attribute Details

#excluded_routesObject

Returns the value of attribute excluded_routes.



7
8
9
# File 'lib/sparrow/route_parser.rb', line 7

def excluded_routes
  @excluded_routes
end

Instance Method Details

#allow?(path) ⇒ Boolean

States if the given path is not within the excluded_routes

Parameters:

  • path (String)

    the path to be checked

Returns:

  • (Boolean)

    path allowed

See Also:



31
32
33
# File 'lib/sparrow/route_parser.rb', line 31

def allow?(path)
  !exclude?(path)
end

#exclude?(path) ⇒ Boolean

States if the given path is within the excluded_routes, i.e. it may be ignored when parsing.

Parameters:

  • path (String)

    the path to be checked

Returns:

  • (Boolean)

    is path excluded



41
42
43
44
45
46
47
# File 'lib/sparrow/route_parser.rb', line 41

def exclude?(path)
  normalized_path = normalize_path(path)
  excluded_routes.each do |route|
    return true if normalized_path =~ route
  end
  return false
end