Class: Rack::Action
Constant Summary collapse
- VERSION =
'0.3.0'
Instance Attribute Summary collapse
-
#env ⇒ Object
Returns the value of attribute env.
-
#params ⇒ Object
Returns the value of attribute params.
-
#request ⇒ Object
Returns the value of attribute request.
-
#response ⇒ Object
Returns the value of attribute response.
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.
-
#pretty_json(data = {}, options = {}) ⇒ String
This is a convenience method that sets the Content-Type headers and writes the pretty-formatted JSON String to the response.
-
#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.
Methods included from Filters
after_filter, after_filters, before_filter, before_filters, skip_after_filter, skip_before_filter
Constructor Details
#initialize(env) ⇒ Action
Returns a new instance of Action.
37 38 39 |
# File 'lib/rack/action.rb', line 37 def initialize(env) @env = env end |
Instance Attribute Details
#env ⇒ Object
Returns the value of attribute env.
35 36 37 |
# File 'lib/rack/action.rb', line 35 def env @env end |
#params ⇒ Object
Returns the value of attribute params.
35 36 37 |
# File 'lib/rack/action.rb', line 35 def params @params end |
#request ⇒ Object
Returns the value of attribute request.
35 36 37 |
# File 'lib/rack/action.rb', line 35 def request @request end |
#response ⇒ Object
Returns the value of attribute response.
35 36 37 |
# File 'lib/rack/action.rb', line 35 def response @response end |
Class Method Details
.call(env) ⇒ Array<Numeric, Hash, #each>
This implements the Rack interface
31 32 33 |
# File 'lib/rack/action.rb', line 31 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.
165 166 167 |
# File 'lib/rack/action.rb', line 165 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.
75 76 77 78 79 80 81 82 |
# File 'lib/rack/action.rb', line 75 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
139 140 141 |
# File 'lib/rack/action.rb', line 139 def forbidden respond_with 403 end |
#format ⇒ Object
56 57 58 59 60 61 62 63 64 |
# File 'lib/rack/action.rb', line 56 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.
101 102 103 104 105 |
# File 'lib/rack/action.rb', line 101 def json(data={}, ={}) response[CONTENT_TYPE] = APPLICATION_JSON response.status = [:status] if .has_key?(:status) response.write JSON.generate(data) end |
#not_found ⇒ Object
Convenience method to return a 404
134 135 136 |
# File 'lib/rack/action.rb', line 134 def not_found respond_with 404 end |
#pretty_json(data = {}, options = {}) ⇒ String
This is a convenience method that sets the Content-Type headers and writes the pretty-formatted JSON String to the response.
114 115 116 117 118 |
# File 'lib/rack/action.rb', line 114 def pretty_json(data={}, ={}) response[CONTENT_TYPE] = APPLICATION_JSON response.status = [:status] if .has_key?(:status) response.write JSON.pretty_generate(data) 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.
126 127 128 129 130 131 |
# File 'lib/rack/action.rb', line 126 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.
90 91 92 |
# File 'lib/rack/action.rb', line 90 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.
147 148 149 150 151 |
# File 'lib/rack/action.rb', line 147 def respond_with(status_code) response.status = status_code response.write '' nil end |