Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Added Action#status and Action#headers.

Using those instead of options for the json response stuff.
  • Loading branch information...
commit 1fc4fe7987b9851f97ac8b02c62395e4f01f62d7 1 parent 8e44971
@jnunemaker authored
Showing with 52 additions and 42 deletions.
  1. +44 −22 lib/flipper/ui/action.rb
  2. +8 −20 lib/flipper/ui/actions/gate.rb
View
66 lib/flipper/ui/action.rb
@@ -54,11 +54,7 @@ def self.public_path
def initialize(flipper, request)
@flipper, @request = flipper, request
@code = 200
- @headers = {'Content-Type' => 'text/html'}
- end
-
- def request_method_name
- @request_method_name ||= @request.request_method.downcase
+ @headers = {}
end
# Public: Runs the request method for the provided request.
@@ -66,22 +62,12 @@ def request_method_name
# Returns whatever the request method returns in the action.
def run
if respond_to?(request_method_name)
- catch(:halt) {
- send(request_method_name)
- }
+ catch(:halt) { send(request_method_name) }
else
raise UI::RequestMethodNotSupported, "#{self.class} does not support request method #{request_method_name.inspect}"
end
end
- # Public: Call this with a response to immediately stop the current action
- # and respond however you want.
- #
- # response - The Rack::Response you would like to return.
- def halt(response)
- throw :halt, response
- end
-
# Public: Runs another action from within the request method of a
# different action.
#
@@ -97,21 +83,52 @@ def run_other_action(action_class)
action_class.new(flipper, request).run
end
+ # Public: Call this with a response to immediately stop the current action
+ # and respond however you want.
+ #
+ # response - The Rack::Response you would like to return.
+ def halt(response)
+ throw :halt, response
+ end
+
+ # Public: Compiles a view and returns rack response with that as the body.
+ #
+ # name - The Symbol name of the view.
+ #
+ # Returns a Rack::Response.
def view_response(name)
+ header 'Content-Type', 'text/html'
body = view_with_layout { view_without_layout name }
Rack::Response.new(body, @code, @headers)
end
- def json_response(object, options = {})
+ # Public: Dumps an object as json and returns rack response with that as
+ # the body. Automatically sets Content-Type to "application/json".
+ #
+ # object - The Object that should be dumped as json.
+ #
+ # Returns a Rack::Response.
+ def json_response(object)
+ header 'Content-Type', 'application/json'
body = MultiJson.dump(object)
- code = options.fetch(:code, @code)
- headers = @headers.merge({
- 'Content-Type' => 'application/json',
- })
+ Rack::Response.new(body, @code, @headers)
+ end
- Rack::Response.new(body, code, headers)
+ # Public: Set the status code for the response.
+ #
+ # code - The Integer code you would like the response to return.
+ def status(code)
+ @code = code.to_i
+ end
+
+ # Public: Set a header.
+ #
+ # name - The String name of the header.
+ # value - The value of the header.
+ def header(name, value)
+ @headers[name] = value
end
# Private
@@ -141,6 +158,11 @@ def views_path
def public_path
self.class.public_path
end
+
+ # Private: Returns the request method converted to an action method.
+ def request_method_name
+ @request_method_name ||= @request.request_method.downcase
+ end
end
end
end
View
28 lib/flipper/ui/actions/gate.rb
@@ -103,11 +103,8 @@ def invalid_actor_value(value)
message: "#{value.inspect} is not a valid actor value.",
}
- options = {
- code: 422,
- }
-
- halt json_response(response, options)
+ status 422
+ halt json_response(response)
end
# Private: Returns error response for invalid percentage value.
@@ -117,11 +114,8 @@ def invalid_percentage(value, exception)
message: exception.message,
}
- options = {
- code: 422,
- }
-
- halt json_response(response, options)
+ status 422
+ halt json_response(response)
end
# Private: Returns error response that group was not registered.
@@ -131,11 +125,8 @@ def group_not_registered(group_name)
message: "The group named #{group_name.inspect} has not been registered.",
}
- options = {
- code: 404,
- }
-
- halt json_response(response, options)
+ status 404
+ halt json_response(response)
end
# Private: Returns error response that gate update method is not defined.
@@ -145,11 +136,8 @@ def update_gate_method_undefined(gate_name)
message: "I have no clue how to update the gate named #{gate_name.inspect}.",
}
- options = {
- code: 404,
- }
-
- halt json_response(response, options)
+ status 404
+ halt json_response(response)
end
end
end

0 comments on commit 1fc4fe7

Please sign in to comment.
Something went wrong with that request. Please try again.