1
0
зеркало из https://github.com/glebtv/telegram-bot.git synced 2026-09-07 11:25:52 +03:00

Sessions for UpdatesController

Этот коммит содержится в:
Max Melentiev
2016-02-28 21:51:59 +06:00
родитель 298947f970
Коммит bff412360e
5 изменённых файлов: 153 добавлений и 5 удалений

Просмотреть файл

@@ -0,0 +1,72 @@
require 'rack/session/abstract/id'
require 'active_support/cache'
module Telegram
module Bot
class UpdatesController
# Add functionality to store data between requests.
module Session
extend ActiveSupport::Concern
def process_action(*)
super
ensure
session.commit
end
protected
def session
@_session ||= SessionHash.new(self.class.session_store, session_key)
end
def session_key
"#{bot.username}:#{from ? "from:#{from['id']}" : "chat:#{chat['id']}"}"
end
# Rack::Session::Abstract::SessionHash is taken to provide lazy loading.
# All methods that access store are overriden to support
# ActiveSupport::Cache::Store stores.
class SessionHash < Rack::Session::Abstract::SessionHash
attr_reader :id
def initialize(store, id)
@store = store
@id = id
end
def destroy
clear
@store.delete(id)
end
def exists?
return @exists if defined?(@exists)
@data = {}
@exists = @store.exist? id
end
def load!
session = @store.read(id)
@data = session ? stringify_keys(session) : {}
@loaded = true
end
def commit
return unless loaded?
data = to_hash.delete_if { |_, v| v.nil? }
@store.write(id, data)
end
end
module ConfigMethods
delegate :session_store, to: :config
def session_store=(store)
config.session_store = ActiveSupport::Cache.lookup_store(store)
end
end
end
end
end
end