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.

Examples:

halt :unauthorized # would return a 401.
{
: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

Instance Method Details

#halt(*response) ⇒ Object

Halts current processing to the top-level calling Renee application and uses that as a response.

Parameters:

  • response (Object...)

    The response to use.

See Also:



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.

Parameters:

  • response (Object)

    This can be either a Symbol, String, Array or any Object.



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.

Examples:

r = Renee.core { get { halt redirect '/index' } }
r.call(Rack::MockResponse("/")) # => [302, {"Location" => "/index"}, []]

Parameters:

  • path (String)

    The URL to redirect to.

  • code (Integer) (defaults to: 302)

    The HTTP code to use.



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.

Examples:

r = Renee.core { get { redirect! '/index' } }
r.call(Rack::MockResponse("/")) # => [302, {"Location" => "/index"}, []]

Parameters:

  • path (String)

    The URL to redirect to.

  • code (Integer) (defaults to: 302)

    The HTTP code to use.

See Also:



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.

Examples:

respond { status 200; body "Yay!" }
respond("Hello", 200, "foo" => "bar")

Parameters:

  • body (Array) (defaults to: [])

    The contents to return.

  • status (Integer) (defaults to: 200)

    The status code to return.

  • header (Hash) (defaults to: {})

    The headers to return.

  • &blk (Proc)

    The response options to specify



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.

Examples:

respond! { status 200; body "Yay!" }

Parameters:

  • body (Array)

    The contents to return.

  • status (Integer)

    The status code to return.

  • header (Hash)

    The headers to return.

  • &blk (Proc)

    The response options to specify

See Also:



56
57
58
# File 'lib/renee_core/responding.rb', line 56

def respond!(*args, &blk)
  halt respond(*args, &blk)
end