class Flipper::Api::Action

Constants

VALID_REQUEST_METHOD_NAMES

Attributes

flipper[R]

Public: The instance of the Flipper::DSL the middleware was initialized with.

request[R]

Public: The Rack::Request to provide a response for.

Public Class Methods

new(flipper, request) click to toggle source
# File lib/flipper/api/action.rb, line 67
def initialize(flipper, request)
  @flipper = flipper
  @request = request
  @code = 200
  @headers = { 'Content-Type' => Api::CONTENT_TYPE }
end
route(regex) click to toggle source

Public: Call this in subclasses so the action knows its route.

regex - The Regexp that this action should run for.

Returns nothing.

# File lib/flipper/api/action.rb, line 33
def self.route(regex)
  @route_regex = regex
end
route_match?(path) click to toggle source

Internal: Does this action's route match the path.

# File lib/flipper/api/action.rb, line 38
def self.route_match?(path)
  path.match(route_regex)
end
route_regex() click to toggle source

Internal: The regex that matches which routes this action will work for.

# File lib/flipper/api/action.rb, line 43
def self.route_regex
  @route_regex || raise("#{name}.route is not set")
end
run(flipper, request) click to toggle source

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.

# File lib/flipper/api/action.rb, line 53
def self.run(flipper, request)
  new(flipper, request).run
end

Public Instance Methods

halt(response) click to toggle source

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.

# File lib/flipper/api/action.rb, line 105
def halt(response)
  throw :halt, response
end
header(name, value) click to toggle source

Public: Set a header.

name - The String name of the header. value - The value of the header.

# File lib/flipper/api/action.rb, line 143
def header(name, value)
  @headers[name] = value
end
json_error_response(error_key) click to toggle source

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

# File lib/flipper/api/action.rb, line 127
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) click to toggle source

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

# File lib/flipper/api/action.rb, line 115
def json_response(object, status = 200)
  header 'Content-Type', Api::CONTENT_TYPE
  status(status)
  body = JSON.dump(object)
  halt [@code, @headers, [body]]
end
run() click to toggle source

Public: Runs the request method for the provided request.

Returns whatever the request method returns in the action.

# File lib/flipper/api/action.rb, line 77
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) click to toggle source

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.

# File lib/flipper/api/action.rb, line 97
def run_other_action(action_class)
  action_class.new(flipper, request).run
end
status(code) click to toggle source

Public: Set the status code for the response.

code - The Integer code you would like the response to return.

# File lib/flipper/api/action.rb, line 135
def status(code)
  @code = code.to_i
end

Private Instance Methods

path_parts() click to toggle source

Private: split request path by “/” Example: “features/feature_name” => ['features', 'feature_name']

# File lib/flipper/api/action.rb, line 156
def path_parts
  @request.path.split('/')
end
request_method_name() click to toggle source

Private: Returns the request method converted to an action method.

# File lib/flipper/api/action.rb, line 150
def request_method_name
  @request_method_name ||= @request.request_method.downcase
end
valid_request_method?() click to toggle source
# File lib/flipper/api/action.rb, line 160
def valid_request_method?
  VALID_REQUEST_METHOD_NAMES.include?(request_method_name)
end