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.



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

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.



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

def default_category
  @default_category
end

Instance Attribute Details

#categoryObject (readonly)

Returns the value of attribute category.



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

def category
  @category
end

#keyObject (readonly)

Returns the value of attribute key.



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

def key
  @key
end

#optionsObject (readonly)

Returns the value of attribute options.



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

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.



40
41
42
# File 'lib/consently/providers/base.rb', line 40

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


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

def cookie_manifest
  @cookie_manifest ||= []
end

.provider_keyObject



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

def provider_key
  @provider_key
end

.provider_key=(key) ⇒ Object



33
34
35
36
# File 'lib/consently/providers/base.rb', line 33

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.



64
65
66
# File 'lib/consently/providers/base.rb', line 64

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

#noscriptObject

Rendered inside , for vendors that still ship a



70
71
72
# File 'lib/consently/providers/base.rb', line 70

def noscript
  nil
end

#scriptsObject



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

def scripts
  []
end

#to_sObject



74
75
76
# File 'lib/consently/providers/base.rb', line 74

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