Class: Kameleoon::Network::Cookie::CookieManager

Inherits:
Object
  • Object
show all
Defined in:
lib/kameleoon/network/cookie/cookie_manager.rb

Instance Method Summary collapse

Constructor Details

#initialize(data_manager, top_level_domain) ⇒ CookieManager

Returns a new instance of CookieManager.



14
15
16
17
18
19
# File 'lib/kameleoon/network/cookie/cookie_manager.rb', line 14

def initialize(data_manager, top_level_domain)
  Logging::KameleoonLogger.debug("CALL: CookieManager.new(top_level_domain: '%s')", top_level_domain)
  @data_manager = data_manager
  @top_level_domain = top_level_domain
  Logging::KameleoonLogger.debug("RETURN: CookieManager.new(top_level_domain: '%s')", top_level_domain)
end

Instance Method Details

#get_or_add(cookies, default_visitor_code = nil) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/kameleoon/network/cookie/cookie_manager.rb', line 21

def get_or_add(cookies, default_visitor_code = nil)
  return if cookies.nil?

  Logging::KameleoonLogger.debug(
    "CALL: CookieManager.get_or_add(cookies: %s, default_visitor_code: '%s')",
    cookies, default_visitor_code
  )

  visitor_code = get_visitor_code_from_cookies(cookies)
  unless visitor_code.nil?
    Utils::VisitorCode.validate(visitor_code)
    Logging::KameleoonLogger.debug("Read visitor code '%s' from cookies %s", visitor_code, cookies)
    # Remove adding cookies when we will be sure that it doesn't break anything
    add(visitor_code, cookies) unless @data_manager.visitor_code_managed?
    Logging::KameleoonLogger.debug(
      "RETURN: CookieManager.get_or_add(cookies: %s, default_visitor_code: '%s') -> (visitor_code: '%s')",
      cookies, default_visitor_code, visitor_code
    )
    return visitor_code
  end

  if default_visitor_code.nil?
    visitor_code = Utils::VisitorCode.generate
    Logging::KameleoonLogger.debug("Generated new visitor code '%s'", visitor_code)
    add(visitor_code, cookies) unless @data_manager.visitor_code_managed?
    Logging::KameleoonLogger.debug(
      "RETURN: CookieManager.get_or_add(cookies: %s, default_visitor_code: '%s') -> (visitor_code: '%s')",
      cookies, default_visitor_code, visitor_code
    )
    return visitor_code
  end

  visitor_code = default_visitor_code
  Utils::VisitorCode.validate(visitor_code)
  Logging::KameleoonLogger.debug("Used default visitor code '{%s}'", default_visitor_code)
  add(visitor_code, cookies)
  Logging::KameleoonLogger.debug(
    "RETURN: CookieManager.get_or_add(cookies: %s, default_visitor_code: '%s') -> (visitor_code: '%s')",
    cookies, default_visitor_code, visitor_code
  )
  visitor_code
end

#update(visitor_code, consent, cookies) ⇒ Object



64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/kameleoon/network/cookie/cookie_manager.rb', line 64

def update(visitor_code, consent, cookies)
  return if cookies.nil?

  Logging::KameleoonLogger.debug("CALL: CookieManager.update(visitor_code: '%s', consent: %s, cookies: %s)",
                                 visitor_code, consent, cookies)

  if consent
    add(visitor_code, cookies)
  else
    remove(cookies)
  end
  Logging::KameleoonLogger.debug("RETURN: CookieManager.update(visitor_code: '%s', consent: %s, cookies: %s)",
                                 visitor_code, consent, cookies)
end