Class: Lookout::Rack::Session
- Inherits:
-
Object
- Object
- Lookout::Rack::Session
- 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
-
#clear ⇒ self
Clears all cookies from the session.
-
#cookie(cookie, uri = nil) ⇒ self
Sets COOKIE, being a newline-, comma-, and semicolon-separated list of ‘KEY=VALUE` pairs, for URI, or the default URI, in the session.
-
#delete(uri, params = {}, env = {}) ⇒ self
Dispatches a DELETE request.
- #dispatch(method, uri = '', params = {}, env = {}) ⇒ self
-
#get(uri, params = {}, env = {}) ⇒ self
Dispatches a GET request.
-
#initialize(app) ⇒ Session
constructor
Sets up a new session for APP.
-
#post(uri, params = {}, env = {}) ⇒ self
Dispatches a POST request.
-
#put(uri, params = {}, env = {}) ⇒ self
Dispatches a PUT request.
-
#redirect! ⇒ self
Redirects to the most recent response’s redirected location by performing a #get request to the “Location” header of the response.
-
#request ⇒ Rack::Request
The Rack request that was most recently sent during this session.
-
#response ⇒ Rack::MockResponse
The Rack response that was most recently received during this session.
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.
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
#clear ⇒ self
Clears all cookies from the session.
112 113 114 115 |
# File 'lib/lookout-rack-1.0/session.rb', line 112 def clear @cookies = Lookout::Rack::Cookies.new self end |
#cookie(cookie, uri = nil) ⇒ self
Sets COOKIE, being a newline-, comma-, and semicolon-separated list of ‘KEY=VALUE` pairs, for URI, or the default URI, in the session.
105 106 107 108 |
# File 'lib/lookout-rack-1.0/session.rb', line 105 def (, uri = nil) @cookies.merge! , uri self end |
#delete(uri, params = {}, env = {}) ⇒ self
Dispatches a DELETE request.
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).
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.
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.
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.
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.
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 |
#request ⇒ Rack::Request
Returns The Rack request that was most recently sent during this session.
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 |
#response ⇒ Rack::MockResponse
Returns The Rack response that was most recently received during this session.
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 |