Module: Renee::Core::Responding
- Included in:
- Renee::Core
- Defined in:
- lib/renee_core/responding.rb
Overview
Collection of useful methods for responding within a Renee::Core app.
Constant Summary collapse
- HTTP_CODES =
Codes used by Symbol lookup in interpret_response.
{ :ok => 200, :created => 201, :accepted => 202, :no_content => 204, :no_content => 204, :bad_request => 400, :unauthorized => 401, :payment_required => 403, :not_found => 404, :method_not_found => 405, :not_acceptable => 406, :gone => 410, :error => 500, :not_implemented => 501}.freeze
Instance Method Summary collapse
-
#halt(*response) ⇒ Object
Halts current processing to the top-level calling Renee application and uses that as a response.
-
#interpret_response(response) ⇒ Object
Interprets responses returns by #halt.
-
#redirect(path, code = 302) ⇒ Object
Returns a rack-based response for redirection.
-
#redirect!(path, code = 302) ⇒ Object
Halts with a rack-based response for redirection.
-
#respond(body = [], status = 200, header = {}, &blk) ⇒ Object
Creates a response by allowing the response header, body and status to be passed into the block.
-
#respond!(*args, &blk) ⇒ Object
Creates a response by allowing the response header, body and status to be passed into the block.
Instance Method Details
#halt(*response) ⇒ Object
Halts current processing to the top-level calling Renee application and uses that as a response.
28 29 30 |
# File 'lib/renee_core/responding.rb', line 28 def halt(*response) throw :halt, interpret_response(response.size == 1 ? response.first : response) end |
#interpret_response(response) ⇒ Object
Interprets responses returns by #halt.
- If it is a Symbol, it will be looked up in HTTP_CODES.
- If it is a Symbol, it will use Rack::Response to return the value.
- If it is a Symbol, it will either be used as a Rack response or as a body and status code.
- If it is an Integer, it will use Rack::Response to return the status code.
- Otherwise, #to_s will be called on it and it will be treated as a Symbol.
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 |
# File 'lib/renee_core/responding.rb', line 70 def interpret_response(response) case response when Array then case response.size when 3 then response when 2 then Renee::Core::Response.new(response[1], HTTP_CODES[response[0]] || response[0]).finish else raise "I don't know how to render #{response.inspect}" end when String then Renee::Core::Response.new(response).finish when Integer then Renee::Core::Response.new("Status code #{response}", response).finish when Symbol then interpret_response(HTTP_CODES[response] || response.to_s) when Proc then instance_eval(&response) else response # pass through response end end |
#redirect(path, code = 302) ⇒ Object
Returns a rack-based response for redirection.
92 93 94 95 96 |
# File 'lib/renee_core/responding.rb', line 92 def redirect(path, code = 302) response = ::Rack::Response.new response.redirect(path, code) response.finish end |
#redirect!(path, code = 302) ⇒ Object
Halts with a rack-based response for redirection.
105 106 107 |
# File 'lib/renee_core/responding.rb', line 105 def redirect!(path, code = 302) halt redirect(path, code) end |
#respond(body = [], status = 200, header = {}, &blk) ⇒ Object
Creates a response by allowing the response header, body and status to be passed into the block.
44 45 46 |
# File 'lib/renee_core/responding.rb', line 44 def respond(body=[], status=200, header={}, &blk) Renee::Core::Response.new(body, status, header).tap { |r| r.instance_eval(&blk) if block_given? }.finish end |
#respond!(*args, &blk) ⇒ Object
Creates a response by allowing the response header, body and status to be passed into the block.
56 57 58 |
# File 'lib/renee_core/responding.rb', line 56 def respond!(*args, &blk) halt respond(*args, &blk) end |