class Lookout::Rack::Session
A [Rack](rack.rubyforge.org/) session for use with [Lookout](disu.se/software/lookout/). Given a Rack “app”, it’ll allow you to make {#get}, {#post}, {#put}, and {#delete} requests (and actually {#dispatch} arbitrary requests, if you desire), check the sent {#request}, check the received {#response}, follow {#redirect!}s, add {#cookie}s, and {#clear} cookies.
Public Class Methods
Sets up a new session for APP. You’ll most likely not call this yourself, leaving it up to {Methods#session} to do so.
@param [#call] app
# File lib/lookout-rack-1.0/session.rb, line 13 def initialize(app) @app = app @request = nil @response = nil clear end
Public Instance Methods
Clears all cookies from the session. @return [self]
# File lib/lookout-rack-1.0/session.rb, line 111 def clear @cookies = Lookout::Rack::Cookies.new self end
Dispatches a METHOD {#request} to URI with PARAMS in ENV and gets its {#response}, storing any returned {#cookie}s. For more information on ENV, see [Rack::MockRequest](rack.rubyforge.org/doc/classes/Rack/MockRequest.html).
@param [String] method @param [String] uri @param [Hash] params @param [Hash] env @return [self]
# File lib/lookout-rack-1.0/session.rb, line 58 def dispatch(method, uri = '', params = {}, env = {}) uri = URI(uri) uri.host ||= Lookout::Rack::DefaultHost env = Rack::MockRequest.env_for(uri.to_s, env.merge(:method => method, :params => params)) env['HTTP_COOKIE'] ||= @cookies.for(uri) @request = Rack::Request.new(env) errors = env['rack.errors'] status, headers, body = *(env[:lint] ? Rack::Lint.new(@app) : @app).call(env) @response = Rack::MockResponse.new(status, headers, body, errors.flush) body.close if body.respond_to?(:close) @cookies.merge! @response.headers['Set-Cookie'], uri if @response.headers['Set-Cookie'] self end
Redirects to the most recent response’s redirected location by performing a {#get} request to the “Location” header of the response.
@raise [RequestError] If no response has been received yet @raise [RedirectError] If the last response wasn’t a redirect @return [self]
# File lib/lookout-rack-1.0/session.rb, line 92 def redirect! response.redirect? or raise Lookout::Rack::RedirectError, 'most recent response was not a redirect' get(response['Location']) end
@return [Rack::Request] The Rack request that was most recently sent during
this session
@raise [RequestError] If no request has been sent yet
# File lib/lookout-rack-1.0/session.rb, line 75 def request @request or raise Lookout::Rack::RequestError, 'no request has been sent yet' end
@return [Rack::MockResponse] The Rack response that was most recently
received during this session
@raise [RequestError] If no response has been received yet
# File lib/lookout-rack-1.0/session.rb, line 82 def response @response or raise Lookout::Rack::ResponseError, 'no response has been received yet' end