Commit 6f80f09e authored by Sergey Gernyak's avatar Sergey Gernyak

AccountSessionManager implementation

parent 582a28cc
...@@ -11,6 +11,7 @@ module BetfairApiNgRails ...@@ -11,6 +11,7 @@ module BetfairApiNgRails
autoload :Account autoload :Account
autoload :AccountSession autoload :AccountSession
autoload :AccountManager autoload :AccountManager
autoload :AccountSessionManager
module Api module Api
extend ActiveSupport::Autoload extend ActiveSupport::Autoload
......
require 'singleton'
module BetfairApiNgRails
class AccountSessionManager
include Singleton
attr_reader :sessions
def initialize
clear
end
def store(session)
sessions[session.username] = session
end
def get(username)
sessions.fetch(username, AccountSession.new).ssoid
end
def expire(username)
sessions.delete username
end
def clear
@sessions = {}
end
end
end
require 'spec_helper'
describe BetfairApiNgRails::AccountSessionManager do
let(:session) { BetfairApiNgRails::AccountSession.new('user001', 'nwfh7348ofho3478fh') }
subject(:manager) { described_class.instance }
describe '#store' do
before do
manager.clear
end
it 'stores session in global hash' do
expect{
manager.store session
}.to change(manager.sessions, :count).by 1
end
end
describe '#get' do
context 'when information for user exists' do
before do
manager.store session
end
it 'returns it' do
expect(manager.get('user001')).to_not be_nil
end
end
context 'when information for user not exists' do
it 'returns nil' do
expect(manager.get('003')).to be_nil
end
end
end
describe '#expire' do
before do
manager.store session
end
it 'removes session of user' do
expect{
manager.expire 'user001'
}.to change(manager.sessions, :count).by -1
end
end
describe '#clear' do
before do
manager.store session
end
it 'cleares sessions register' do
manager.clear
expect(manager.sessions.count).to eq 0
end
end
end
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment