Class: Flipper::Api::Action

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/flipper/api/action.rb

Constant Summary collapse

VALID_REQUEST_METHOD_NAMES =
Set.new([
  "get".freeze,
  "post".freeze,
  "put".freeze,
  "delete".freeze,
]).freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(flipper, request) ⇒ Action

Returns a new instance of Action.



51
52
53
54
55
# File 'lib/flipper/api/action.rb', line 51

def initialize(flipper, request)
  @flipper, @request = flipper, request
  @code = 200
  @headers = {"Content-Type" => Api::CONTENT_TYPE }
end

Instance Attribute Details

#flipperObject (readonly)

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



43
44
45
# File 'lib/flipper/api/action.rb', line 43

def flipper
  @flipper
end

#requestObject (readonly)

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



46
47
48
# File 'lib/flipper/api/action.rb', line 46

def request
  @request
end

Class Method Details

.regexObject

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



37
38
39
# File 'lib/flipper/api/action.rb', line 37

def self.regex
  @regex || raise("#{name}.route is not set")
end

.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.



22
23
24
# File 'lib/flipper/api/action.rb', line 22

def self.route(regex)
  @regex = regex
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.



32
33
34
# File 'lib/flipper/api/action.rb', line 32

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.



87
88
89
# File 'lib/flipper/api/action.rb', line 87

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.



109
110
111
# File 'lib/flipper/api/action.rb', line 109

def header(name, value)
  @headers[name] = value
end

#json_response(object, status = 200) ⇒ Object



91
92
93
94
95
96
# File 'lib/flipper/api/action.rb', line 91

def json_response(object, status = 200)
  header 'Content-Type', Api::CONTENT_TYPE
  status(status)
  body = JSON.dump(object)
  halt [@code, @headers, [body]]
end

#runObject

Public: Runs the request method for the provided request.

Returns whatever the request method returns in the action.



60
61
62
63
64
65
66
# File 'lib/flipper/api/action.rb', line 60

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.



79
80
81
# File 'lib/flipper/api/action.rb', line 79

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.



101
102
103
# File 'lib/flipper/api/action.rb', line 101

def status(code)
  @code = code.to_i
end