Class: Flipper::Api::Action
- Inherits:
-
Object
- Object
- Flipper::Api::Action
- Extended by:
- Forwardable
- Defined in:
- lib/flipper/api/action.rb
Direct Known Subclasses
V1::Actions::Actors, V1::Actions::ActorsGate, V1::Actions::BooleanGate, V1::Actions::ClearFeature, V1::Actions::ExpressionGate, V1::Actions::Feature, V1::Actions::Features, V1::Actions::GroupsGate, V1::Actions::Import, V1::Actions::PercentageOfActorsGate, V1::Actions::PercentageOfTimeGate
Defined Under Namespace
Modules: FeatureNameFromRoute
Constant Summary collapse
- VALID_REQUEST_METHOD_NAMES =
Set.new([ 'head'.freeze, 'get'.freeze, 'post'.freeze, 'put'.freeze, 'delete'.freeze, ]).freeze
Instance Attribute Summary collapse
-
#flipper ⇒ Object
readonly
Public: The instance of the Flipper::DSL the middleware was initialized with.
-
#request ⇒ Object
readonly
Public: The Rack::Request to provide a response for.
Class Method Summary collapse
-
.route(regex) ⇒ Object
Public: Call this in subclasses so the action knows its route.
-
.route_match?(path) ⇒ Boolean
Internal: Does this action’s route match the path.
-
.route_regex ⇒ Object
Internal: The regex that matches which routes this action will work for.
-
.run(flipper, request) ⇒ Object
Internal: Initializes and runs an action for a given request.
Instance Method Summary collapse
-
#halt(response) ⇒ Object
Public: Call this with a response to immediately stop the current action and respond however you want.
-
#header(name, value) ⇒ Object
Public: Set a header.
-
#initialize(flipper, request) ⇒ Action
constructor
A new instance of Action.
-
#json_error_response(error_key) ⇒ Object
Public: Call this with an ErrorResponse::ERRORS key to respond with the serialized error object as response body.
-
#json_response(object, status = 200) ⇒ Object
Public: Call this with a json serializable object (i.e. Hash) to serialize object and respond to request.
-
#run ⇒ Object
Public: Runs the request method for the provided request.
-
#run_other_action(action_class) ⇒ Object
Public: Runs another action from within the request method of a different action.
-
#status(code) ⇒ Object
Public: Set the status code for the response.
Constructor Details
#initialize(flipper, request) ⇒ Action
Returns a new instance of Action.
68 69 70 71 72 73 |
# File 'lib/flipper/api/action.rb', line 68 def initialize(flipper, request) @flipper = flipper @request = request @code = 200 @headers = {Rack::CONTENT_TYPE => Api::CONTENT_TYPE} end |
Instance Attribute Details
#flipper ⇒ Object (readonly)
Public: The instance of the Flipper::DSL the middleware was initialized with.
60 61 62 |
# File 'lib/flipper/api/action.rb', line 60 def flipper @flipper end |
#request ⇒ Object (readonly)
Public: The Rack::Request to provide a response for.
63 64 65 |
# File 'lib/flipper/api/action.rb', line 63 def request @request end |
Class Method Details
.route(regex) ⇒ Object
Public: Call this in subclasses so the action knows its route.
regex - The Regexp that this action should run for.
Returns nothing.
34 35 36 |
# File 'lib/flipper/api/action.rb', line 34 def self.route(regex) @route_regex = regex end |
.route_match?(path) ⇒ Boolean
Internal: Does this action’s route match the path.
39 40 41 |
# File 'lib/flipper/api/action.rb', line 39 def self.route_match?(path) path.match(route_regex) end |
.route_regex ⇒ Object
Internal: The regex that matches which routes this action will work for.
44 45 46 |
# File 'lib/flipper/api/action.rb', line 44 def self.route_regex @route_regex || raise("#{name}.route is not set") end |
.run(flipper, request) ⇒ Object
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.
54 55 56 |
# File 'lib/flipper/api/action.rb', line 54 def self.run(flipper, request) new(flipper, request).run end |
Instance Method Details
#halt(response) ⇒ Object
Public: Call this with a response to immediately stop the current action and respond however you want.
response - The response you would like to return.
106 107 108 |
# File 'lib/flipper/api/action.rb', line 106 def halt(response) throw :halt, response end |
#header(name, value) ⇒ Object
Public: Set a header.
name - The String name of the header. value - The value of the header.
144 145 146 |
# File 'lib/flipper/api/action.rb', line 144 def header(name, value) @headers[name] = value end |
#json_error_response(error_key) ⇒ Object
Public: Call this with an ErrorResponse::ERRORS key to respond with the serialized error object as response body
error_key - key to lookup error object
128 129 130 131 |
# File 'lib/flipper/api/action.rb', line 128 def json_error_response(error_key) error = ErrorResponse::ERRORS.fetch(error_key.to_sym) json_response(error.as_json, error.http_status) end |
#json_response(object, status = 200) ⇒ Object
Public: Call this with a json serializable object (i.e. Hash) to serialize object and respond to request
object - json serializable object status - http status code
116 117 118 119 120 121 |
# File 'lib/flipper/api/action.rb', line 116 def json_response(object, status = 200) header Rack::CONTENT_TYPE, Api::CONTENT_TYPE status(status) body = Typecast.to_json(object) halt [@code, @headers, [body]] end |
#run ⇒ Object
Public: Runs the request method for the provided request.
Returns whatever the request method returns in the action.
78 79 80 81 82 83 84 85 |
# File 'lib/flipper/api/action.rb', line 78 def run if valid_request_method? && respond_to?(request_method_name) catch(:halt) { send(request_method_name) } else raise Api::RequestMethodNotSupported, "#{self.class} does not support request method #{request_method_name.inspect}" end end |
#run_other_action(action_class) ⇒ Object
Public: Runs another action from within the request method of a different action.
action_class - The class of the other action to run.
Examples
run_other_action Home
# => result of running Home action
Returns result of other action.
98 99 100 |
# File 'lib/flipper/api/action.rb', line 98 def run_other_action(action_class) action_class.new(flipper, request).run end |
#status(code) ⇒ Object
Public: Set the status code for the response.
code - The Integer code you would like the response to return.
136 137 138 |
# File 'lib/flipper/api/action.rb', line 136 def status(code) @code = code.to_i end |