Skip to content

Commit

Permalink
Move back to single regex from overly flexible request match
Browse files Browse the repository at this point in the history
We just need to match the path info to a regex. We don't need the whole request.
  • Loading branch information
jnunemaker committed Jul 31, 2018
1 parent f61bbe9 commit 8fa06a5
Show file tree
Hide file tree
Showing 22 changed files with 46 additions and 51 deletions.
25 changes: 11 additions & 14 deletions lib/flipper/api/action.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,21 @@ class Action

# Public: Call this in subclasses so the action knows its route.
#
# block - The Block that determines if this can handle a given request.
# regex - The Regexp that this action should run for.
#
# Returns nothing.
def self.match(&block)
if block_given?
@match = block
else
@match || raise("No match block set for #{name}")
end
def self.route(regex)
@route_regex = regex
end

# Internal: Does this action's route match the path.
def self.route_match?(path)
path.match(route_regex)
end

def self.match?(request)
match.call(request)
# Internal: The regex that matches which routes this action will work for.
def self.route_regex
@route_regex || raise("#{name}.route is not set")
end

# Internal: Initializes and runs an action for a given request.
Expand All @@ -42,11 +44,6 @@ def self.run(flipper, request)
new(flipper, request).run
end

# Internal: The regex that matches which routes this action will work for.
def self.regex
@regex || raise("#{name}.route is not set")
end

# Public: The instance of the Flipper::DSL the middleware was
# initialized with.
attr_reader :flipper
Expand Down
2 changes: 1 addition & 1 deletion lib/flipper/api/action_collection.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ def add(action_class)

def action_for_request(request)
@action_classes.detect do |action_class|
action_class.match?(request)
action_class.route_match?(request.path_info)
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion lib/flipper/api/v1/actions/actors_gate.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ module V1
module Actions
class ActorsGate < Api::Action
REGEX = %r{\A/features/(?<feature_name>.*)/actors/?\Z}
match { |request| request.path_info =~ REGEX }
route REGEX

def post
ensure_valid_params
Expand Down
2 changes: 1 addition & 1 deletion lib/flipper/api/v1/actions/boolean_gate.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ module V1
module Actions
class BooleanGate < Api::Action
REGEX = %r{\A/features/(?<feature_name>.*)/boolean/?\Z}
match { |request| request.path_info =~ REGEX }
route REGEX

def post
feature = flipper[feature_name]
Expand Down
2 changes: 1 addition & 1 deletion lib/flipper/api/v1/actions/clear_feature.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ module V1
module Actions
class ClearFeature < Api::Action
REGEX = %r{\A/features/(?<feature_name>.*)/clear/?\Z}
match { |request| request.path_info =~ REGEX }
route REGEX

def delete
feature = flipper[feature_name]
Expand Down
2 changes: 1 addition & 1 deletion lib/flipper/api/v1/actions/feature.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ module V1
module Actions
class Feature < Api::Action
REGEX = %r{\A/features/(?<feature_name>.*)/?\Z}
match { |request| request.path_info =~ REGEX }
route REGEX

def get
return json_error_response(:feature_not_found) unless feature_exists?(feature_name)
Expand Down
3 changes: 2 additions & 1 deletion lib/flipper/api/v1/actions/features.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ module Api
module V1
module Actions
class Features < Api::Action
match { |request| request.path_info =~ %r{\A/features/?\Z} }
REGEX = %r{\A/features/?\Z}
route REGEX

def get
keys = params['keys']
Expand Down
2 changes: 1 addition & 1 deletion lib/flipper/api/v1/actions/groups_gate.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ module V1
module Actions
class GroupsGate < Api::Action
REGEX = %r{\A/features/(?<feature_name>.*)/groups/?\Z}
match { |request| request.path_info =~ REGEX }
route REGEX

def post
ensure_valid_params
Expand Down
2 changes: 1 addition & 1 deletion lib/flipper/api/v1/actions/percentage_of_actors_gate.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ module V1
module Actions
class PercentageOfActorsGate < Api::Action
REGEX = %r{\A/features/(?<feature_name>.*)/percentage_of_actors/?\Z}
match { |request| request.path_info =~ REGEX }
route REGEX

def post
if percentage < 0 || percentage > 100
Expand Down
2 changes: 1 addition & 1 deletion lib/flipper/api/v1/actions/percentage_of_time_gate.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ module V1
module Actions
class PercentageOfTimeGate < Api::Action
REGEX = %r{\A/features/(?<feature_name>.*)/percentage_of_time/?\Z}
match { |request| request.path_info =~ REGEX }
route REGEX

def post
if percentage < 0 || percentage > 100
Expand Down
20 changes: 11 additions & 9 deletions lib/flipper/ui/action.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,21 @@ class Action

# Public: Call this in subclasses so the action knows its route.
#
# block - The Block that determines if this can handle a given request.
# regex - The Regexp that this action should run for.
#
# Returns nothing.
def self.match(&block)
if block_given?
@match = block
else
@match || raise("No match block set for #{name}")
end
def self.route(regex)
@route_regex = regex
end

# Internal: Does this action's route match the path.
def self.route_match?(path)
path.match(route_regex)
end

def self.match?(request)
match.call(request)
# Internal: The regex that matches which routes this action will work for.
def self.route_regex
@route_regex || raise("#{name}.route is not set")
end

# Internal: Initializes and runs an action for a given request.
Expand Down
2 changes: 1 addition & 1 deletion lib/flipper/ui/action_collection.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ def add(action_class)

def action_for_request(request)
@action_classes.detect do |action_class|
action_class.match?(request)
action_class.route_match?(request.path_info)
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion lib/flipper/ui/actions/actors_gate.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ module UI
module Actions
class ActorsGate < UI::Action
REGEX = %r{\A/features/(?<feature_name>.*)/actors/?\Z}
match { |request| request.path_info =~ REGEX }
route REGEX

def get
feature = flipper[feature_name]
Expand Down
5 changes: 2 additions & 3 deletions lib/flipper/ui/actions/add_feature.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,8 @@ module Flipper
module UI
module Actions
class AddFeature < UI::Action
match do |request|
request.path_info =~ %r{\A/features/new/?\Z}
end
REGEX = %r{\A/features/new/?\Z}
route REGEX

def get
unless Flipper::UI.configuration.feature_creation_enabled
Expand Down
2 changes: 1 addition & 1 deletion lib/flipper/ui/actions/boolean_gate.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ module UI
module Actions
class BooleanGate < UI::Action
REGEX = %r{\A/features/(?<feature_name>.*)/boolean/?\Z}
match { |request| request.path_info =~ REGEX }
route REGEX

def post
feature = flipper[feature_name]
Expand Down
2 changes: 1 addition & 1 deletion lib/flipper/ui/actions/feature.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ module UI
module Actions
class Feature < UI::Action
REGEX = %r{\A/features/(?<feature_name>.*)\Z}
match { |request| request.path_info =~ REGEX }
route REGEX

def get
@feature = Decorators::Feature.new(flipper[feature_name])
Expand Down
5 changes: 2 additions & 3 deletions lib/flipper/ui/actions/features.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,8 @@ module Flipper
module UI
module Actions
class Features < UI::Action
match do |request|
request.path_info =~ %r{\A/features/?\Z}
end
REGEX = %r{\A/features/?\Z}
route REGEX

def get
@page_title = 'Features'
Expand Down
5 changes: 2 additions & 3 deletions lib/flipper/ui/actions/file.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,8 @@ module Flipper
module UI
module Actions
class File < UI::Action
match do |request|
request.path_info =~ %r{(images|css|js|octicons|fonts)/.*\Z}
end
REGEX = %r{(images|css|js|octicons|fonts)/.*\Z}
route REGEX

def get
Rack::File.new(public_path).call(request.env)
Expand Down
2 changes: 1 addition & 1 deletion lib/flipper/ui/actions/groups_gate.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ module UI
module Actions
class GroupsGate < UI::Action
REGEX = %r{\A/features/(?<feature_name>.*)/groups/?\Z}
match { |request| request.path_info =~ REGEX }
route REGEX

def get
feature = flipper[feature_name]
Expand Down
4 changes: 1 addition & 3 deletions lib/flipper/ui/actions/home.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,7 @@ module Flipper
module UI
module Actions
class Home < UI::Action
match do |request|
request.path_info =~ %r{\A/?\Z}
end
route %r{\A/?\Z}

def get
redirect_to '/features'
Expand Down
2 changes: 1 addition & 1 deletion lib/flipper/ui/actions/percentage_of_actors_gate.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ module UI
module Actions
class PercentageOfActorsGate < UI::Action
REGEX = %r{\A/features/(?<feature_name>.*)/percentage_of_actors/?\Z}
match { |request| request.path_info =~ REGEX }
route REGEX

def post
feature = flipper[feature_name]
Expand Down
2 changes: 1 addition & 1 deletion lib/flipper/ui/actions/percentage_of_time_gate.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ module UI
module Actions
class PercentageOfTimeGate < UI::Action
REGEX = %r{\A/features/(?<feature_name>.*)/percentage_of_time/?\Z}
match { |request| request.path_info =~ REGEX }
route REGEX

def post
feature = flipper[feature_name]
Expand Down

0 comments on commit 8fa06a5

Please sign in to comment.