Class: Verdict::Storage::CookieStorage

Inherits:
BaseStorage show all
Defined in:
lib/verdict/storage/cookie_storage.rb

Overview

CookieStorage, unlike other Verdict storage classes, is distributed and stored on the client. Because cookies are opaque to users, we obsfucate information about the test such as the human readable names for the experiment or assignment group. This means this class assumes that ‘name` will be an obsfucated value, or one that we comfortable being “public”.

Constant Summary collapse

15778476
KEY =
:assignment

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from BaseStorage

#cleanup

Constructor Details

#initialize(cookie_lifespan: DEFAULT_COOKIE_LIFESPAN_SECONDS) ⇒ CookieStorage

Returns a new instance of CookieStorage.



16
17
18
19
# File 'lib/verdict/storage/cookie_storage.rb', line 16

def initialize(cookie_lifespan: DEFAULT_COOKIE_LIFESPAN_SECONDS)
  @cookies = nil
  @cookie_lifespan = cookie_lifespan
end

Instance Attribute Details

Returns the value of attribute cookie_lifespan.



14
15
16
# File 'lib/verdict/storage/cookie_storage.rb', line 14

def cookie_lifespan
  @cookie_lifespan
end

#cookiesObject

Returns the value of attribute cookies.



13
14
15
# File 'lib/verdict/storage/cookie_storage.rb', line 13

def cookies
  @cookies
end

Instance Method Details

#remove_assignment(experiment, _subject) ⇒ Object



44
45
46
# File 'lib/verdict/storage/cookie_storage.rb', line 44

def remove_assignment(experiment, _subject)
  remove(experiment.handle.to_s, KEY)
end

#retrieve_assignment(experiment, subject) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/verdict/storage/cookie_storage.rb', line 30

def retrieve_assignment(experiment, subject)
  if (value = get(experiment.handle.to_s, KEY))
    data = parse_cookie_value(value)
    group = find_group_by_digest(experiment, data['group'])

    if group.nil?
      experiment.remove_subject_assignment(subject)
      return nil
    end

    experiment.subject_assignment(subject, group, Time.xmlschema(data['created_at']))
  end
end

#retrieve_start_timestamp(_experiment) ⇒ Object



48
49
50
# File 'lib/verdict/storage/cookie_storage.rb', line 48

def retrieve_start_timestamp(_experiment)
  nil
end

#store_assignment(assignment) ⇒ Object



21
22
23
24
25
26
27
28
# File 'lib/verdict/storage/cookie_storage.rb', line 21

def store_assignment(assignment)
  data = {
    group: digest_of(assignment.group.to_s),
    created_at: assignment.created_at.strftime('%FT%TZ')
  }

  set(assignment.experiment.handle.to_s, KEY, JSON.dump(data))
end

#store_start_timestamp(_experiment, _timestamp) ⇒ Object

Raises:

  • (NotImplementedError)


52
53
54
# File 'lib/verdict/storage/cookie_storage.rb', line 52

def store_start_timestamp(_experiment, _timestamp)
  raise NotImplementedError
end