Class: Simple::Authorisation::RouteRuleFinder

Inherits:
Object
  • Object
show all
Defined in:
lib/simple-authorisation/route_rule_finder.rb

Direct Known Subclasses

ExactRouteRuleFinder

Instance Method Summary collapse

Constructor Details

#initialize(routes) ⇒ RouteRuleFinder

Returns a new instance of RouteRuleFinder.



5
6
7
8
# File 'lib/simple-authorisation/route_rule_finder.rb', line 5

def initialize(routes)
  @routes = routes
  @find_by = [:route_by_wild_card, :route_starts_with]
end

Instance Method Details

#find(route_name) ⇒ Object

Raises:



18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/simple-authorisation/route_rule_finder.rb', line 18

def find(route_name)
  matching_route = nil
  @find_by.each do |method|
    matching_route = send(method, route_name)
    break unless matching_route.nil?
  end

  route_settings = @routes[matching_route]
  raise NoSettingsForRoute.new(route_name) if route_settings.nil?

  route_settings
end

#route_by_wild_card(route_name) ⇒ Object



10
11
12
# File 'lib/simple-authorisation/route_rule_finder.rb', line 10

def route_by_wild_card(route_name)
  (@routes.keys.sort.reverse.select{|route | route_name =~ /^#{route.gsub('*', '.+')}$/}).first
end

#route_starts_with(route_name) ⇒ Object



14
15
16
# File 'lib/simple-authorisation/route_rule_finder.rb', line 14

def route_starts_with(route_name)
  (@routes.keys.sort.reverse.select { |route| route_name.start_with?(route) }).first
end