Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'michalmuskala-allow-session-configuration'
- Loading branch information
Showing
9 changed files
with
388 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
require 'lotus/utils/string' | ||
|
||
module Lotus | ||
module Config | ||
# Sessions configuration | ||
# | ||
# @since x.x.x | ||
# @api private | ||
class Sessions | ||
|
||
RACK_NAMESPACE = 'Rack::Session::%{class_name}'.freeze | ||
|
||
def initialize(identifier = nil, options = {}, config = nil) | ||
@identifier = identifier | ||
@options = options | ||
@config = config | ||
end | ||
|
||
def enabled? | ||
!!@identifier | ||
end | ||
|
||
def options | ||
default_options.merge(@options) | ||
end | ||
|
||
def middleware_class | ||
case @identifier | ||
when Symbol | ||
class_name = Utils::String.new(@identifier).classify | ||
RACK_NAMESPACE % { class_name: class_name } | ||
else | ||
@identifier | ||
end | ||
end | ||
|
||
private | ||
|
||
def default_options | ||
if @config | ||
{ domain: @config.host, secure: @config.scheme == 'https' } | ||
else | ||
{} | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
require 'test_helper' | ||
|
||
describe Lotus::Config::Sessions do | ||
describe '#enabled?' do | ||
it 'is false unless identifier is provided' do | ||
sessions = Lotus::Config::Sessions.new | ||
sessions.wont_be :enabled? | ||
end | ||
|
||
it 'is true when identifier is provided' do | ||
sessions = Lotus::Config::Sessions.new('Cookie') | ||
sessions.must_be :enabled? | ||
end | ||
end | ||
|
||
describe '#middleware_class' do | ||
describe 'provided with class as identifier' do | ||
before do | ||
SessionMiddleware = Class.new | ||
end | ||
|
||
after do | ||
Object.send(:remove_const, :SessionMiddleware) | ||
end | ||
|
||
it 'returns class' do | ||
sessions = Lotus::Config::Sessions.new(SessionMiddleware) | ||
sessions.middleware_class.must_equal SessionMiddleware | ||
end | ||
end | ||
|
||
describe 'provided with string as identifier' do | ||
it 'returns string' do | ||
sessions = Lotus::Config::Sessions.new('SessionMiddleware') | ||
sessions.middleware_class.must_equal 'SessionMiddleware' | ||
end | ||
end | ||
|
||
describe 'provided with symbol as identifier' do | ||
it 'returns symbol as class name under Rack::Session namespace' do | ||
sessions = Lotus::Config::Sessions.new(:some_storage) | ||
sessions.middleware_class.must_equal 'Rack::Session::SomeStorage' | ||
end | ||
end | ||
end | ||
|
||
describe '#options' do | ||
it 'returns passed options' do | ||
options = { domain: 'example.com' } | ||
sessions = Lotus::Config::Sessions.new('Cookie', options) | ||
sessions.options.must_equal options | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
module SessionsApp | ||
class Application < Lotus::Application | ||
configure do | ||
# Activate sessions | ||
sessions :cookie, secret: '1234567890' | ||
|
||
routes do | ||
post '/set_session' , to: 'sessions#new' | ||
get '/get_session' , to: 'sessions#show' | ||
delete '/clear_session', to: 'sessions#destroy' | ||
end | ||
end | ||
|
||
load! | ||
end | ||
|
||
|
||
module Controllers::Sessions | ||
include SessionsApp::Controller | ||
|
||
action 'New' do | ||
def call(params) | ||
session[:name] = params[:name] | ||
self.body = "Session created for: #{session[:name]}" | ||
end | ||
end | ||
|
||
action 'Show' do | ||
def call(params) | ||
|
||
self.body = session[:name] | ||
end | ||
end | ||
|
||
action 'Destroy' do | ||
def call(params) | ||
name = session[:name] | ||
session.clear | ||
self.body = "Session cleared for: #{name}" | ||
end | ||
end | ||
end | ||
|
||
end |
Oops, something went wrong.