Class: RackSessionManipulation::Middleware
- Inherits:
-
Object
- Object
- RackSessionManipulation::Middleware
- Defined in:
- lib/rack_session_manipulation/middleware.rb
Overview
Rack middleware that handles the accessing and modification of session state.
Instance Attribute Summary collapse
-
#app ⇒ void
readonly
Returns the value of attribute app.
-
#config ⇒ void
readonly
Returns the value of attribute config.
-
#routes ⇒ void
readonly
Returns the value of attribute routes.
Instance Method Summary collapse
-
#call(env) ⇒ Array<Fixnum, Hash, String>
Primary entry point of this middleware.
-
#extract_method(request) ⇒ String
Look up what HTTP method was used for this request.
-
#get_action(request) ⇒ Symbol, Nil
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.
-
#headers(length) ⇒ Hash
A helper mechanism to consistently generate common headers client will expect.
-
#initialize(app, opts = {}) ⇒ Middleware
constructor
Setup the middleware with the primary application passed in, anything we can’t handle will be passed to this application.
-
#reset(request) ⇒ Array<Fixnum, Hash, String>
Handle requests to entirely reset the session state.
-
#retrieve(request) ⇒ Array<Fixnum, Hash, String>
Retrieve the entire contents of the session and properly encode it before returning.
-
#update(request) ⇒ Array<Fixnum, Hash, String>
Update the current state of the session with the provided data.
Constructor Details
#initialize(app, opts = {}) ⇒ Middleware
Setup the middleware with the primary application passed in, anything we can’t handle will be passed to this application.
31 32 33 34 35 36 37 38 39 |
# File 'lib/rack_session_manipulation/middleware.rb', line 31 def initialize(app, opts = {}) @app = app @config = Config.new(opts) @routes = { 'DELETE' => :reset, 'GET' => :retrieve, 'PUT' => :update } end |
Instance Attribute Details
#app ⇒ void (readonly)
Returns the value of attribute app.
6 7 8 |
# File 'lib/rack_session_manipulation/middleware.rb', line 6 def app @app end |
#config ⇒ void (readonly)
Returns the value of attribute config.
6 7 8 |
# File 'lib/rack_session_manipulation/middleware.rb', line 6 def config @config end |
#routes ⇒ void (readonly)
Returns the value of attribute routes.
6 7 8 |
# File 'lib/rack_session_manipulation/middleware.rb', line 6 def routes @routes 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.
17 18 19 20 21 22 |
# File 'lib/rack_session_manipulation/middleware.rb', line 17 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
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.
56 57 58 59 |
# File 'lib/rack_session_manipulation/middleware.rb', line 56 def extract_method(request) return request.params['_method'].upcase if request.params['_method'] request.request_method end |
#get_action(request) ⇒ Symbol, Nil
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.
68 69 70 71 |
# File 'lib/rack_session_manipulation/middleware.rb', line 68 def get_action(request) return unless request.path == config.path routes[extract_method(request)] end |
#headers(length) ⇒ Hash
A helper mechanism to consistently generate common headers client will expect.
78 79 80 81 82 83 |
# File 'lib/rack_session_manipulation/middleware.rb', line 78 def headers(length) { 'Content-Length' => length, 'Content-Type' => 'application/json; charset=utf-8' } end |
#reset(request) ⇒ Array<Fixnum, Hash, String>
Handle requests to entirely reset the session state.
89 90 91 92 |
# File 'lib/rack_session_manipulation/middleware.rb', line 89 def reset(request) request.env['rack.session'].clear [204, headers(0), ''] end |
#retrieve(request) ⇒ Array<Fixnum, Hash, String>
Retrieve the entire contents of the session and properly encode it before returning.
99 100 101 102 103 104 |
# File 'lib/rack_session_manipulation/middleware.rb', line 99 def retrieve(request) session_hash = request.env['rack.session'].to_hash content = config.encoder.encode(session_hash) [200, headers(content.length), content] end |
#update(request) ⇒ Array<Fixnum, Hash, String>
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.
112 113 114 115 116 117 118 119 120 121 122 |
# File 'lib/rack_session_manipulation/middleware.rb', line 112 def update(request) session_data = request.params['session_data'] config.encoder.decode(session_data).each do |k, v| request.env['rack.session'][k] = v end loc_hdr = { 'Location' => config.path } [303, headers(0).merge(loc_hdr), ''] rescue JSON::ParserError [400, headers(0), ''] end |