Class: Lookout::Rack::Session

Inherits:
Object
  • Object
show all
Defined in:
lib/lookout-rack-1.0/session.rb

Overview

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 #cookies, and #clear cookies.

Instance Method Summary collapse

Constructor Details

#initialize(app) ⇒ Session

Sets up a new session for APP. You’ll most likely not call this yourself, leaving it up to Methods#session to do so.

Parameters:

  • app (#call)


14
15
16
17
18
19
# File 'lib/lookout-rack-1.0/session.rb', line 14

def initialize(app)
  @app = app
  @request = nil
  @response = nil
  clear
end

Instance Method Details

#clearself

Clears all cookies from the session.

Returns:

  • (self)


112
113
114
115
# File 'lib/lookout-rack-1.0/session.rb', line 112

def clear
  @cookies = Lookout::Rack::Cookies.new
  self
end

Sets COOKIE, being a newline-, comma-, and semicolon-separated list of ‘KEY=VALUE` pairs, for URI, or the default URI, in the session.

Parameters:

  • cookie (String)
  • uri (String, nil) (defaults to: nil)

Returns:

  • (self)


105
106
107
108
# File 'lib/lookout-rack-1.0/session.rb', line 105

def cookie(cookie, uri = nil)
  @cookies.merge! cookie, uri
  self
end

#delete(uri, params = {}, env = {}) ⇒ self

Dispatches a DELETE request.

Parameters:

  • method (String)
  • uri (String)
  • params (Hash) (defaults to: {})
  • env (Hash) (defaults to: {})

Returns:

  • (self)


45
46
47
# File 'lib/lookout-rack-1.0/session.rb', line 45

def delete(uri, params = {}, env = {})
  dispatch('DELETE', uri, params, env)
end

#dispatch(method, uri = '', params = {}, env = {}) ⇒ self

Dispatches a METHOD #request to URI with PARAMS in ENV and gets its #response, storing any returned #cookies. For more information on ENV, see [Rack::MockRequest](rack.rubyforge.org/doc/classes/Rack/MockRequest.html).

Parameters:

  • method (String)
  • uri (String) (defaults to: '')
  • params (Hash) (defaults to: {})
  • env (Hash) (defaults to: {})

Returns:

  • (self)


59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/lookout-rack-1.0/session.rb', line 59

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

#get(uri, params = {}, env = {}) ⇒ self

Dispatches a GET request.

Parameters:

  • method (String)
  • uri (String)
  • params (Hash) (defaults to: {})
  • env (Hash) (defaults to: {})

Returns:

  • (self)


24
25
26
# File 'lib/lookout-rack-1.0/session.rb', line 24

def get(uri, params = {}, env = {})
  dispatch('GET', uri, params, env)
end

#post(uri, params = {}, env = {}) ⇒ self

Dispatches a POST request.

Parameters:

  • method (String)
  • uri (String)
  • params (Hash) (defaults to: {})
  • env (Hash) (defaults to: {})

Returns:

  • (self)


31
32
33
# File 'lib/lookout-rack-1.0/session.rb', line 31

def post(uri, params = {}, env = {})
  dispatch('POST', uri, params, env)
end

#put(uri, params = {}, env = {}) ⇒ self

Dispatches a PUT request.

Parameters:

  • method (String)
  • uri (String)
  • params (Hash) (defaults to: {})
  • env (Hash) (defaults to: {})

Returns:

  • (self)


38
39
40
# File 'lib/lookout-rack-1.0/session.rb', line 38

def put(uri, params = {}, env = {})
  dispatch('PUT', uri, params, env)
end

#redirect!self

Redirects to the most recent response’s redirected location by performing a #get request to the “Location” header of the response.

Returns:

  • (self)

Raises:



93
94
95
96
97
# File 'lib/lookout-rack-1.0/session.rb', line 93

def redirect!
  response.redirect? or
    raise Lookout::Rack::RedirectError, 'most recent response was not a redirect'
  get(response['Location'])
end

#requestRack::Request

Returns The Rack request that was most recently sent during this session.

Returns:

  • (Rack::Request)

    The Rack request that was most recently sent during this session

Raises:



76
77
78
# File 'lib/lookout-rack-1.0/session.rb', line 76

def request
  @request or raise Lookout::Rack::RequestError, 'no request has been sent yet'
end

#responseRack::MockResponse

Returns The Rack response that was most recently received during this session.

Returns:

  • (Rack::MockResponse)

    The Rack response that was most recently received during this session

Raises:



83
84
85
# File 'lib/lookout-rack-1.0/session.rb', line 83

def response
  @response or raise Lookout::Rack::ResponseError, 'no response has been received yet'
end