module Bot class Context attr_accessor :redis_client, :user def initialize(user, redis_client) @redis_client = redis_client @user = user end def method_missing(m, *args, &block) if m =~ /.+=/ self.add(m.to_s.gsub('=', ''), *args) else self.get(m) end end def get(field) data = $redis.hget(context_key, field) JSON.parse(data) if data.present? rescue JSON::ParserError data end def set(context) self.clear $redis.hmset(context_key, context.map { |k, v| [k, v] }.flatten) end def add(field, content) $redis.hset context_key, field, content end def clear $redis.del context_key end def context_key "bot:context:#{user.id}" end end end