Class: IntercomRails::ScriptTag

Inherits:
Object
  • Object
show all
Includes:
ActionView::Helpers::JavaScriptHelper
Defined in:
lib/intercom-rails/script_tag.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ ScriptTag

Returns a new instance of ScriptTag.



15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/intercom-rails/script_tag.rb', line 15

def initialize(options = {})
  self.secret = options[:secret] || Config.api_secret
  self.widget_options = widget_options_from_config.merge(options[:widget] || {})
  self.controller = options[:controller]
  @show_everywhere = options[:show_everywhere]
  @session_duration = session_duration_from_config
  self.user_details = options[:find_current_user_details] ? find_current_user_details : options[:user_details]
  self.company_details = if options[:find_current_company_details]
    find_current_company_details
  elsif options[:user_details]
    options[:user_details].delete(:company) if options[:user_details]
  end
  self.nonce = options[:nonce]
end

Instance Attribute Details

#company_detailsObject

Returns the value of attribute company_details.



12
13
14
# File 'lib/intercom-rails/script_tag.rb', line 12

def company_details
  @company_details
end

#controllerObject

Returns the value of attribute controller.



13
14
15
# File 'lib/intercom-rails/script_tag.rb', line 13

def controller
  @controller
end

#nonceObject

Returns the value of attribute nonce.



13
14
15
# File 'lib/intercom-rails/script_tag.rb', line 13

def nonce
  @nonce
end

#secretObject

Returns the value of attribute secret.



13
14
15
# File 'lib/intercom-rails/script_tag.rb', line 13

def secret
  @secret
end

#session_durationObject (readonly)

Returns the value of attribute session_duration.



12
13
14
# File 'lib/intercom-rails/script_tag.rb', line 12

def session_duration
  @session_duration
end

#show_everywhereObject (readonly)

Returns the value of attribute show_everywhere.



12
13
14
# File 'lib/intercom-rails/script_tag.rb', line 12

def show_everywhere
  @show_everywhere
end

#user_detailsObject

Returns the value of attribute user_details.



12
13
14
# File 'lib/intercom-rails/script_tag.rb', line 12

def user_details
  @user_details
end

#widget_optionsObject

Returns the value of attribute widget_options.



13
14
15
# File 'lib/intercom-rails/script_tag.rb', line 13

def widget_options
  @widget_options
end

Instance Method Details

#csp_sha256Object



75
76
77
78
79
# File 'lib/intercom-rails/script_tag.rb', line 75

def csp_sha256
  base64_sha256 = Base64.encode64(Digest::SHA256.digest(intercom_javascript))
  csp_hash = "'sha256-#{base64_sha256}'".delete("\n")
  csp_hash
end

#intercom_settingsObject



58
59
60
61
62
63
64
# File 'lib/intercom-rails/script_tag.rb', line 58

def intercom_settings
  hsh = user_details
  hsh[:session_duration] = @session_duration if @session_duration.present?
  hsh[:widget] = widget_options if widget_options.present?
  hsh[:company] = company_details if company_details.present?
  hsh
end

#to_sObject



66
67
68
69
70
71
72
73
# File 'lib/intercom-rails/script_tag.rb', line 66

def to_s
  js_options = 'id="IntercomSettingsScriptTag"'
  if nonce && valid_nonce?
    js_options = js_options + " nonce=\"#{nonce}\""
  end
  str = "<script #{js_options}>#{intercom_javascript}</script>\n"
  str.respond_to?(:html_safe) ? str.html_safe : str
end

#valid?Boolean

Returns:

  • (Boolean)


30
31
32
33
34
35
36
37
38
39
# File 'lib/intercom-rails/script_tag.rb', line 30

def valid?
  valid = user_details[:app_id].present?
  unless @show_everywhere
    valid = valid && (user_details[:user_id] || user_details[:email]).present?
  end
  if nonce
    valid = valid && valid_nonce?
  end
  valid
end

#valid_nonce?Boolean

Returns:

  • (Boolean)


41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/intercom-rails/script_tag.rb', line 41

def valid_nonce?
  valid = false
  if nonce
    # Base64 regexp:
    # - blocks of 4 [A-Za-z0-9+/]
    # followed either by:
    # - blocks of 2 [A-Za-z0-9+/] + '=='
    # - blocks of 3 [A-Za-z0-9+/] + '='
    base64_regexp = Regexp.new('^(?:[A-Za-z0-9+/]{4})*(?:[A-Za-z0-9+/]{2}==|[A-Za-z0-9+/]{3}=)?$')
    m = base64_regexp.match(nonce)
    if nonce == m.to_s
      valid = true
    end
  end
  valid
end