Sha256: 6ccd5b950ccd28b97b6a20a451d93a232ab9b45e8c4659efa96521b02ef01f26

Contents?: true

Size: 959 Bytes

Versions: 3

Compression:

Stored size: 959 Bytes

Contents

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

3 entries across 3 versions & 1 rubygems

Version Path
split-1.3.2 lib/split/persistence/cookie_adapter.rb
split-1.3.1 lib/split/persistence/cookie_adapter.rb
split-1.3.0 lib/split/persistence/cookie_adapter.rb