Sha256: 7e6523c8b2581428c8f9a23359da2c642d07fd06df491e0f507f9f55e6cf3c7e

Contents?: true

Size: 989 Bytes

Versions: 13

Compression:

Stored size: 989 Bytes

Contents

# frozen_string_literal: true
require "json"

module Split
  module Persistence
    class CookieAdapter

      def initialize(context)
        @cookies = context.send(:cookies)
        @expires = Time.now + cookie_length_config
      end

      def [](key)
        hash[key]
      end

      def []=(key, value)
        set_cookie(hash.merge(key => value))
      end

      def delete(key)
        set_cookie(hash.tap { |h| h.delete(key) })
      end

      def keys
        hash.keys
      end

      private

      def set_cookie(value)
        @cookies[:split] = {
          :value => JSON.generate(value),
          :expires => @expires
        }
      end

      def hash
        if @cookies[:split]
          begin
            JSON.parse(@cookies[:split])
          rescue JSON::ParserError
            {}
          end
        else
          {}
        end
      end

      def cookie_length_config
        Split.configuration.persistence_cookie_length
      end

    end
  end
end

Version data entries

13 entries across 13 versions & 1 rubygems

Version Path
split-3.0.0 lib/split/persistence/cookie_adapter.rb
split-2.2.0 lib/split/persistence/cookie_adapter.rb
split-2.1.0 lib/split/persistence/cookie_adapter.rb
split-2.0.0 lib/split/persistence/cookie_adapter.rb
split-1.7.0 lib/split/persistence/cookie_adapter.rb
split-1.6.0 lib/split/persistence/cookie_adapter.rb
split-1.5.0 lib/split/persistence/cookie_adapter.rb
split-1.4.5 lib/split/persistence/cookie_adapter.rb
split-1.4.4 lib/split/persistence/cookie_adapter.rb
split-1.4.3 lib/split/persistence/cookie_adapter.rb
split-1.4.2 lib/split/persistence/cookie_adapter.rb
split-1.4.1 lib/split/persistence/cookie_adapter.rb
split-1.4.0 lib/split/persistence/cookie_adapter.rb