Class: Consently::Providers::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/consently/providers/base.rb

Overview

A tag vendor: what scripts it needs and which consent category it falls under. Subclasses describe the snippet as data (see Consently::Script) so the view can render it live or blocked from the same description.

Constant Summary collapse

SAFE_OPTION =

Ids are interpolated straight into JavaScript, so they are held to a shape that cannot carry a quote or a bracket out of the string.

/\A[\w\-.:\/]+\z/

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(**options) ⇒ Base

Returns a new instance of Base.



60
61
62
63
64
65
# File 'lib/consently/providers/base.rb', line 60

def initialize(**options)
  @options = options
  @category = (options[:category] || self.class.default_category).to_sym
  @key = (options[:as] || self.class.provider_key).to_sym
  validate!
end

Class Attribute Details

.default_categoryObject

Set by each subclass; where a vendor is unambiguous (Plausible does not touch cookies) it can say :necessary and load right away.



36
37
38
# File 'lib/consently/providers/base.rb', line 36

def default_category
  @default_category
end

.google=(value) ⇒ Object (writeonly)

Whether this vendor is one of Google's, which is what advanced consent mode is about: those tags may load before consent because they respect the denied defaults themselves.



28
29
30
# File 'lib/consently/providers/base.rb', line 28

def google=(value)
  @google = value
end

Instance Attribute Details

#categoryObject (readonly)

Returns the value of attribute category.



58
59
60
# File 'lib/consently/providers/base.rb', line 58

def category
  @category
end

#keyObject (readonly)

Returns the value of attribute key.



58
59
60
# File 'lib/consently/providers/base.rb', line 58

def key
  @key
end

#optionsObject (readonly)

Returns the value of attribute options.



58
59
60
# File 'lib/consently/providers/base.rb', line 58

def options
  @options
end

Class Method Details

.build(key, **options) ⇒ Object



20
21
22
23
# File 'lib/consently/providers/base.rb', line 20

def build(key, **options)
  klass = registry[key.to_sym] or raise UnknownProvider, key
  klass.new(**options)
end

What this vendor stores in the visitor's browser, for the policy page. days: nil means a session cookie.



49
50
51
# File 'lib/consently/providers/base.rb', line 49

def cookie(name, days: nil)
  cookie_manifest << { name: name, days: days }
end


53
54
55
# File 'lib/consently/providers/base.rb', line 53

def cookie_manifest
  @cookie_manifest ||= []
end

.google?Boolean

Returns:

  • (Boolean)


30
31
32
# File 'lib/consently/providers/base.rb', line 30

def google?
  !!@google
end

.provider_keyObject



38
39
40
# File 'lib/consently/providers/base.rb', line 38

def provider_key
  @provider_key
end

.provider_key=(key) ⇒ Object



42
43
44
45
# File 'lib/consently/providers/base.rb', line 42

def provider_key=(key)
  @provider_key = key.to_sym
  register(@provider_key, self)
end

.register(key, klass) ⇒ Object



16
17
18
# File 'lib/consently/providers/base.rb', line 16

def register(key, klass)
  registry[key.to_sym] = klass
end

.registryObject



12
13
14
# File 'lib/consently/providers/base.rb', line 12

def registry
  @@registry ||= {}
end

Instance Method Details

#cookiesObject

ArrayConsently::Cookie - what this tag leaves in the browser.



77
78
79
# File 'lib/consently/providers/base.rb', line 77

def cookies
  self.class.cookie_manifest.map { |attributes| Cookie.new(**attributes, provider: key) }
end

#google?Boolean

Returns:

  • (Boolean)


72
73
74
# File 'lib/consently/providers/base.rb', line 72

def google?
  self.class.google?
end

#noscriptObject

Rendered inside , for vendors that still ship a



83
84
85
# File 'lib/consently/providers/base.rb', line 83

def noscript
  nil
end

#scriptsObject



68
69
70
# File 'lib/consently/providers/base.rb', line 68

def scripts
  []
end

#to_sObject



87
88
89
# File 'lib/consently/providers/base.rb', line 87

def to_s
  "#{key} (#{category})"
end