Class: Rack::Action
Constant Summary collapse
- VERSION =
"0.7.0".freeze
Class Attribute Summary collapse
Instance Attribute Summary collapse
-
#env ⇒ Object
Returns the value of attribute env.
- #params ⇒ Object
- #request ⇒ Object
- #response ⇒ Object
Class Method Summary collapse
-
.call(env) ⇒ Array<Numeric, Hash, #each>
This implements the Rack interface.
Instance Method Summary collapse
-
#absolute_url(url, options = {}) ⇒ String
Generate an absolute url from the url.
-
#call ⇒ Array<Numeric, Hash, #each>
This is the main method responsible for generating a Rack response.
-
#forbidden ⇒ Object
Convenience method to return a 403.
- #format ⇒ Object
-
#initialize(env) ⇒ Action
constructor
A new instance of Action.
-
#json(data = {}, options = {}) ⇒ String
This is a convenience method that sets the Content-Type headers and writes the JSON String to the response.
-
#not_found ⇒ Object
Convenience method to return a 404.
-
#redirect_to(url, options = {}) ⇒ String
This is a convenience method that forms an absolute URL based on the url parameter, which can be a relative or absolute URL, and then sets the headers and the body appropriately to do a 302 redirect.
-
#respond ⇒ String
This is the main method that you should override in your action.
-
#respond_with(status_code) ⇒ Object
This is a convenience method to set the response code and set the response so that it stops respond process.
Constructor Details
#initialize(env) ⇒ Action
Returns a new instance of Action.
39 40 41 |
# File 'lib/rack/action.rb', line 39 def initialize(env) @env = env end |
Class Attribute Details
.json_serializer ⇒ Object
229 230 231 232 233 234 235 236 237 238 239 240 |
# File 'lib/rack/action.rb', line 229 def json_serializer if defined? @json_serializer @json_serializer else @json_serializer = if superclass.respond_to?(:json_serializer) superclass.json_serializer else JSON end end end |
.logger ⇒ Object
221 222 223 224 225 226 227 |
# File 'lib/rack/action.rb', line 221 def logger if defined? @logger @logger else @logger = superclass.logger if superclass.respond_to?(:logger) end end |
Instance Attribute Details
#env ⇒ Object
Returns the value of attribute env.
36 37 38 |
# File 'lib/rack/action.rb', line 36 def env @env end |
#params ⇒ Object
51 52 53 54 55 56 57 58 59 60 61 |
# File 'lib/rack/action.rb', line 51 def params @params ||= begin p = request.params.merge(env[RACK_ROUTE_PARAMS] || {}) if request.content_type.to_s.include?(APPLICATION_JSON) body = env[RACK_INPUT].read env[RACK_INPUT].rewind p.merge!(self.class.json_serializer.load(body)) if body.present? end p.respond_to?(:with_indifferent_access) ? p.with_indifferent_access : p end end |
#request ⇒ Object
43 44 45 |
# File 'lib/rack/action.rb', line 43 def request @request ||= Rack::Request.new(env) end |
#response ⇒ Object
47 48 49 |
# File 'lib/rack/action.rb', line 47 def response @response ||= Rack::Response.new end |
Class Method Details
.call(env) ⇒ Array<Numeric, Hash, #each>
This implements the Rack interface
32 33 34 |
# File 'lib/rack/action.rb', line 32 def self.call(env) new(env).call end |
Instance Method Details
#absolute_url(url, options = {}) ⇒ String
Generate an absolute url from the url. If the url is already an absolute url, this will return it unchanged.
159 160 161 |
# File 'lib/rack/action.rb', line 159 def absolute_url(url, ={}) URL.new(env, url, ).to_absolute end |
#call ⇒ Array<Numeric, Hash, #each>
This is the main method responsible for generating a Rack response. You typically won’t need to override this method or call it directly. First this will run the before filters for this action. If none of the before filters generate a response, this will call #respond to generate a response. All after filters for this action are called once the response is genenated. Finally the response is returned.
82 83 84 85 86 87 88 89 |
# File 'lib/rack/action.rb', line 82 def call log_call set_default_headers run_before_filters run_respond run_after_filters finish_response end |
#forbidden ⇒ Object
Convenience method to return a 403
133 134 135 |
# File 'lib/rack/action.rb', line 133 def forbidden respond_with 403 end |
#format ⇒ Object
63 64 65 66 67 68 69 70 71 |
# File 'lib/rack/action.rb', line 63 def format if params[:format] params[:format] elsif env[HTTP_ACCEPT] == APPLICATION_JSON "json" else "html" end end |
#json(data = {}, options = {}) ⇒ String
This is a convenience method that sets the Content-Type headers and writes the JSON String to the response.
108 109 110 111 112 |
# File 'lib/rack/action.rb', line 108 def json(data={}, ={}) response[CONTENT_TYPE] = APPLICATION_JSON response.status = [:status] if .has_key?(:status) response.write self.class.json_serializer.dump(data) end |
#not_found ⇒ Object
Convenience method to return a 404
128 129 130 |
# File 'lib/rack/action.rb', line 128 def not_found respond_with 404 end |
#redirect_to(url, options = {}) ⇒ String
This is a convenience method that forms an absolute URL based on the url parameter, which can be a relative or absolute URL, and then sets the headers and the body appropriately to do a 302 redirect.
120 121 122 123 124 125 |
# File 'lib/rack/action.rb', line 120 def redirect_to(url, ={}) full_url = absolute_url(url, ) response[LOCATION] = full_url respond_with 302 full_url end |
#respond ⇒ String
This is the main method that you should override in your action. You can either write to the response during this method, or simply return a string, which will be written to the response if the response is still empty after this is called.
97 98 99 |
# File 'lib/rack/action.rb', line 97 def respond DEFAULT_RESPONSE end |
#respond_with(status_code) ⇒ Object
This is a convenience method to set the response code and set the response so that it stops respond process.
141 142 143 144 145 |
# File 'lib/rack/action.rb', line 141 def respond_with(status_code) response.status = status_code response.write "" nil end |