Class: Biscuit::Consent
- Inherits:
-
Object
- Object
- Biscuit::Consent
- Defined in:
- lib/biscuit/consent.rb
Constant Summary collapse
- CURRENT_VERSION =
1
Class Method Summary collapse
-
.build_value(categories) ⇒ Object
Build the cookie value hash.
-
.clear(cookies) ⇒ Object
Clear the consent cookie.
-
.parse(raw) ⇒ Object
Parse raw cookie string.
-
.write(cookies, categories) ⇒ Object
Write consent cookie to the jar.
Instance Method Summary collapse
-
#allowed?(category) ⇒ Boolean
True if the user has consented to this category.
-
#given? ⇒ Boolean
True if a valid consent decision has been recorded.
-
#initialize(cookies) ⇒ Consent
constructor
cookies: ActionDispatch::Cookies::CookieJar.
Constructor Details
#initialize(cookies) ⇒ Consent
cookies: ActionDispatch::Cookies::CookieJar
6 7 8 9 |
# File 'lib/biscuit/consent.rb', line 6 def initialize() @cookies = @data = self.class.parse([Biscuit.configuration.]) 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() .delete(Biscuit.configuration.) 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(, categories) value = build_value(categories) config = Biscuit.configuration [config.] = { value: value.to_json, expires: config..days.from_now, path: config., domain: config., same_site: config., 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.
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.
12 13 14 |
# File 'lib/biscuit/consent.rb', line 12 def given? !@data.nil? end |