Class: Biscuit::Consent

Inherits:
Object
  • Object
show all
Defined in:
lib/biscuit/consent.rb

Constant Summary collapse

CURRENT_VERSION =
1

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(cookies) ⇒ Consent

cookies: ActionDispatch::Cookies::CookieJar



6
7
8
9
# File 'lib/biscuit/consent.rb', line 6

def initialize(cookies)
  @cookies = cookies
  @data    = self.class.parse(cookies[Biscuit.configuration.cookie_name])
end

Class Method Details

.build_value(categories) ⇒ Object

Build the cookie value hash. Always forces necessary: true.



44
45
46
47
48
49
# File 'lib/biscuit/consent.rb', line 44

def self.build_value(categories)
  cats = categories.transform_keys(&:to_s)
                   .transform_values { |v| v == true || v == "true" }
  cats["necessary"] = true
  { "v" => CURRENT_VERSION, "consented_at" => Time.now.utc.iso8601, "categories" => cats }
end

.clear(cookies) ⇒ Object

Clear the consent cookie.



39
40
41
# File 'lib/biscuit/consent.rb', line 39

def self.clear(cookies)
  cookies.delete(Biscuit.configuration.cookie_name)
end

.parse(raw) ⇒ Object

Parse raw cookie string. Returns nil if blank, invalid JSON, or wrong version.



52
53
54
55
56
57
58
59
# File 'lib/biscuit/consent.rb', line 52

def self.parse(raw)
  return nil if raw.blank?
  data = JSON.parse(raw)
  return nil unless data.is_a?(Hash) && data["v"] == CURRENT_VERSION && data["categories"].is_a?(Hash)
  data
rescue JSON::ParserError
  nil
end

.write(cookies, categories) ⇒ Object

Write consent cookie to the jar.



25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/biscuit/consent.rb', line 25

def self.write(cookies, categories)
  value  = build_value(categories)
  config = Biscuit.configuration
  cookies[config.cookie_name] = {
    value:     value.to_json,
    expires:   config.cookie_expires_days.days.from_now,
    path:      config.cookie_path,
    domain:    config.cookie_domain,
    same_site: config.cookie_same_site,
    httponly:  false   # Must be readable by JS for client-side consent checks
  }
end

Instance Method Details

#allowed?(category) ⇒ Boolean

True if the user has consented to this category. :necessary always returns true regardless of cookie state.

Returns:

  • (Boolean)


18
19
20
21
22
# File 'lib/biscuit/consent.rb', line 18

def allowed?(category)
  return true if category.to_sym == :necessary
  return false unless given?
  @data.dig("categories", category.to_s) == true
end

#given?Boolean

True if a valid consent decision has been recorded.

Returns:

  • (Boolean)


12
13
14
# File 'lib/biscuit/consent.rb', line 12

def given?
  !@data.nil?
end