Sha256: d1eb6f4fcc2dfa63c7659540c7041b6810e52089331750de0bb99a35495dd468

Contents?: true

Size: 1.4 KB

Versions: 4

Compression:

Stored size: 1.4 KB

Contents

# coding: utf-8
# frozen_string_literal: true

module Stealth
  class Session

    SLUG_SEPARATOR = '->'

    attr_reader :session, :flow, :state, :user_id

    def initialize(user_id:)
      @user_id = user_id

      unless defined?($redis)
        raise(Stealth::Errors::RedisNotConfigured, "Please make sure REDIS_URL is configured before using sessions.")
      end

      get
      self
    end

    def self.flow_and_state_from_session_slug(slug:)
      {
        flow: slug&.split(SLUG_SEPARATOR)&.first,
        state: slug&.split(SLUG_SEPARATOR)&.last
      }
    end

    def flow
      @flow = begin
        flow_klass = [flow_string, 'flow'].join('_').classify.constantize
        flow = flow_klass.new.init_state(state_string)
        flow
      end
    end

    def state
      flow.current_state
    end

    def flow_string
      session&.split(SLUG_SEPARATOR)&.first
    end

    def state_string
      session&.split(SLUG_SEPARATOR)&.last
    end

    def get
      @session ||= $redis.get(user_id)
    end

    def set(flow:, state:)
      @session = canonical_session_slug(flow: flow, state: state)
      flow
      $redis.set(user_id, canonical_session_slug(flow: flow, state: state))
    end

    def present?
      session.present?
    end

    def blank?
      !present?
    end

    private

      def canonical_session_slug(flow:, state:)
        [flow, state].join(SLUG_SEPARATOR)
      end

  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
stealth-0.9.4 lib/stealth/session.rb
stealth-0.9.3 lib/stealth/session.rb
stealth-0.9.2 lib/stealth/session.rb
stealth-0.9.1 lib/stealth/session.rb