class EenyMeeny::Cookie

Constants

EXPERIMENT_PREFIX
SMOKE_TEST_PREFIX

Attributes

expires[R]
httponly[R]
name[R]
path[R]
same_site[R]
value[RW]

Public Class Methods

create_for_experiment(experiment, config = {}) click to toggle source
# File lib/eeny-meeny/models/cookie.rb, line 25
def self.create_for_experiment(experiment, config = {})
  options = {
      name: cookie_name(experiment),
      value: experiment.pick_variation.id.to_s
  }
  options[:expires] = experiment.end_at if experiment.end_at
  if EenyMeeny.config.secure
    options[:value] = EenyMeeny.config.encryptor.encrypt(options[:value])
  end
  new(**options.merge(config))
end
create_for_experiment_variation(experiment, variation_id, config = {}) click to toggle source
# File lib/eeny-meeny/models/cookie.rb, line 11
def self.create_for_experiment_variation(experiment, variation_id, config = {})
  variation = experiment.find_variation(variation_id)
  raise "Variation '#{variation_id}' not found for Experiment '#{experiment.id}'" if variation.nil?
  options = {
      name: cookie_name(experiment),
      value: variation.id.to_s
  }
  options[:expires] = experiment.end_at if experiment.end_at
  if EenyMeeny.config.secure
    options[:value] = EenyMeeny.config.encryptor.encrypt(options[:value])
  end
  new(**options.merge(config))
end
create_for_smoke_test(smoke_test_id, version: 1, **config) click to toggle source
# File lib/eeny-meeny/models/cookie.rb, line 37
def self.create_for_smoke_test(smoke_test_id, version: 1, **config)
  options = {
      name: smoke_test_name(smoke_test_id, version: version),
      value: smoke_test_id.to_s
  }
  if EenyMeeny.config.secure
    options[:value] = EenyMeeny.config.encryptor.encrypt(options[:value])
  end
  new(**options.merge(config))
end
new(name: '', value: '', expires: 1.month.from_now, http_only: true, same_site: nil, path: nil) click to toggle source
# File lib/eeny-meeny/models/cookie.rb, line 64
def initialize(name: '', value: '', expires: 1.month.from_now, http_only: true, same_site: nil, path: nil)
  @name      = name
  @expires   = expires
  @httponly  = http_only
  @value     = value
  @same_site = same_site
  @path      = path
end
read(cookie_string) click to toggle source
# File lib/eeny-meeny/models/cookie.rb, line 58
def self.read(cookie_string)
  return if cookie_string.nil? || cookie_string.empty?
  return cookie_string unless EenyMeeny.config.secure # Cookie encryption disabled.
  EenyMeeny.config.encryptor.decrypt(cookie_string)
end
smoke_test_name(smoke_test_id, version: 1) click to toggle source
# File lib/eeny-meeny/models/cookie.rb, line 48
def self.smoke_test_name(smoke_test_id, version: 1)
  return if smoke_test_id.nil?
  SMOKE_TEST_PREFIX+smoke_test_id.to_s+'_v'+version.to_s
end

Public Instance Methods

to_h() click to toggle source
# File lib/eeny-meeny/models/cookie.rb, line 73
def to_h
  hash = {
      expires: @expires,
      httponly: @httponly,
      value: @value
  }
  hash[:same_site] = @same_site unless @same_site.nil?
  hash[:path] = @path unless @path.nil?
  hash
end
to_s() click to toggle source
# File lib/eeny-meeny/models/cookie.rb, line 84
def to_s
  header = {}
  Rack::Utils.set_cookie_header!(header, name, to_h)
  header['Set-Cookie']
end