lib/flipper/api/action.rb in flipper-api-0.15.0 vs lib/flipper/api/action.rb in flipper-api-0.16.0
- old
+ new
@@ -4,10 +4,20 @@
require 'json'
module Flipper
module Api
class Action
+ module FeatureNameFromRoute
+ def feature_name
+ @feature_name ||= begin
+ match = request.path_info.match(self.class.route_regex)
+ match ? match[:feature_name] : nil
+ end
+ end
+ private :feature_name
+ end
+
extend Forwardable
VALID_REQUEST_METHOD_NAMES = Set.new([
'get'.freeze,
'post'.freeze,
@@ -19,25 +29,30 @@
#
# regex - The Regexp that this action should run for.
#
# Returns nothing.
def self.route(regex)
- @regex = regex
+ @route_regex = regex
end
+ # Internal: Does this action's route match the path.
+ def self.route_match?(path)
+ path.match(route_regex)
+ end
+
+ # 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.
#
# flipper - The Flipper::DSL instance.
# request - The Rack::Request that was sent.
#
# Returns result of Action#run.
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