Commit 582a28cc authored by Sergey Gernyak's avatar Sergey Gernyak

AccountManager implementation

parent 2489c32b
......@@ -10,6 +10,7 @@ module BetfairApiNgRails
autoload :Account
autoload :AccountSession
autoload :AccountManager
module Api
extend ActiveSupport::Autoload
......
require 'singleton'
module BetfairApiNgRails
class AccountManager
include Singleton
attr_reader :accounts
attr_reader :default_account
def initialize
clear
end
def store(account)
accounts[account.username] = account
end
def get(username)
accounts.fetch username, fetch_default_account
end
def default(username)
@default_account = username
end
def clear
@accounts = {}
end
private
def fetch_default_account
accounts.fetch default_account, nil
end
end
end
require 'spec_helper'
describe BetfairApiNgRails::AccountManager do
let(:account) { BetfairApiNgRails::Account.new('user001', 'password', 'cf23f23') }
subject(:manager) { described_class.instance }
it { is_expected.to respond_to :accounts }
describe '#store' do
before do
manager.clear
end
it 'stores account in global hash' do
expect{
manager.store account
}.to change(manager.accounts, :count).by 1
end
end
describe '#get' do
context 'gets account by username' do
context 'when it exists' do
before do
manager.store account
end
it 'returns this one' do
expect(manager.get('user001')).to eq account
end
end
context 'when it not exists' do
context 'and default exists' do
before do
manager.store account
manager.default 'user001'
end
it 'returns it' do
expect(manager.get('user002')).to eq account
end
end
context 'and no defaults' do
before do
manager.clear
end
it 'returns nil' do
expect(manager.get('user002')).to be_nil
end
end
end
end
end
describe '#clear' do
before do
manager.store account
end
it 'cleares account register' do
manager.clear
expect(manager.accounts.count).to eq 0
end
end
describe '#default' do
it 'sets default account' do
expect{
manager.default 'user001'
}.to_not raise_error
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