Class: RackSessionManipulation::Middleware

Inherits:
Object
  • Object
show all
Defined in:
lib/rack_session_manipulation/middleware.rb

Overview

Rack middleware that handles the accessing and modification of session state.

Instance Method Summary collapse

Constructor Details

#initialize(app) ⇒ Middleware

Setup the middleware with the primary application passed in, anything we can’t handle will be passed to this application.

Parameters:

  • app (Object#call)

    A rack application that implements the #call method.



27
28
29
30
31
32
33
34
# File 'lib/rack_session_manipulation/middleware.rb', line 27

def initialize(app)
  @app = app
  @routes = {
    'DELETE'  => :reset,
    'GET'     => :retrieve,
    'PUT'     => :update
  }
end

Instance Method Details

#call(env) ⇒ Array<Fixnum, Hash, String>

Primary entry point of this middleware. Every request that makes it this far into the stack will be parsed and when it matches something this middleware is designed to handle it will stop the chain and process it appropriately.

Parameters:

  • env (Hash)

    An environment hash containing everything about the client’s request.

Returns:

  • (Array<Fixnum, Hash, String>)

    A generated response to the client’s request.



15
16
17
18
19
20
# File 'lib/rack_session_manipulation/middleware.rb', line 15

def call(env)
  request = Rack::Request.new(env)

  action = get_action(request)
  action.nil? ? @app.call(env) : send(action, request)
end

#extract_method(request) ⇒ String (protected)

Look up what HTTP method was used for this request. In the event the client doesn’t support all HTTP methods, the standard ‘_method’ parameter fall back is made available to override the method actually used.

Normally the ‘_method’ fall back is only accepted over the POST method, however, Capybara drivers are only required to implement GET method and so this does not require any particular method to support the override.

When a GET method was used to provide data, a subtle issue can crop up. URLs can’t exceed 1024 characters. Generally this results in them being truncated which in turn will cause a JSON parse error and no changes being made to the session.

Parameters:

  • request (Rack::Request)

    The request to analyze

Returns:

  • (String)

    The decided on HTTP method



53
54
55
56
# File 'lib/rack_session_manipulation/middleware.rb', line 53

def extract_method(request)
  return request.params['_method'].upcase if request.params['_method']
  request.request_method
end

#get_action(request) ⇒ Symbol, Nil (protected)

Considers the request and if it matches something this middleware handles return the symbol matching the name of the method that should handle the request.

Parameters:

  • request (Rack::Request)

    The request to assess

Returns:

  • (Symbol, Nil)

    Name of method to use or nil if this middleware should pass the request on to the app.



65
66
67
68
# File 'lib/rack_session_manipulation/middleware.rb', line 65

def get_action(request)
  return unless request.path == RackSessionManipulation.config.path
  @routes[extract_method(request)]
end

#headers(length) ⇒ Hash (protected)

A helper mechanism to consistently generate common headers client will expect.

Parameters:

  • length (Fixnum)

    Byte size of the message body.

Returns:

  • (Hash)

    The common headers



75
76
77
78
79
80
# File 'lib/rack_session_manipulation/middleware.rb', line 75

def headers(length)
  {
    'Content-Length'  => length,
    'Content-Type'    => 'application/json; charset=utf-8'
  }
end

#reset(request) ⇒ Array<Fixnum, Hash, String> (protected)

Handle requests to entirely reset the session state.

Parameters:

  • request (Rack::Request)

Returns:

  • (Array<Fixnum, Hash, String>)


86
87
88
89
# File 'lib/rack_session_manipulation/middleware.rb', line 86

def reset(request)
  request.env['rack.session'].clear
  [204, headers(0), '']
end

#retrieve(request) ⇒ Array<Fixnum, Hash, String> (protected)

Retrieve the entire contents of the session and properly encode it before returning.

Parameters:

  • request (Rack::Request)

Returns:

  • (Array<Fixnum, Hash, String>)


96
97
98
99
100
101
# File 'lib/rack_session_manipulation/middleware.rb', line 96

def retrieve(request)
  session_hash = request.env['rack.session'].to_hash
  content = RackSessionManipulation.encode(session_hash)

  [200, headers(content.length), content]
end

#update(request) ⇒ Array<Fixnum, Hash, String> (protected)

Update the current state of the session with the provided data. This works effectively like a hash merge on the current session only setting and overriding keys in the session data provided.

Parameters:

  • request (Rack::Request)

Returns:

  • (Array<Fixnum, Hash, String>)


109
110
111
112
113
114
115
116
117
118
119
# File 'lib/rack_session_manipulation/middleware.rb', line 109

def update(request)
  session_data = request.params['session_data']
  RackSessionManipulation.decode(session_data).each do |k, v|
    request.env['rack.session'][k] = v
  end

  loc_hdr = { 'Location' => RackSessionManipulation.config.path }
  [303, headers(0).merge(loc_hdr), '']
rescue JSON::ParserError
  [400, headers(0), '']
end