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
29
30
31
32
33
34
35
# 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.encrypted_mode_enabled = options[:encrypted_mode] || Config.encrypted_mode
  self.encrypted_mode = IntercomRails::EncryptedMode.new(secret, options[:initialization_vector], {:enabled => encrypted_mode_enabled})

  # Request specific custom data for non-signed up users base on lead_attributes
  self.user_details = self.user_details.merge(find_lead_attributes)

  self.company_details = if options[:find_current_company_details]
    find_current_company_details
  elsif options[:user_details]
    options[:user_details].delete(:company)
  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

#encrypted_modeObject

Returns the value of attribute encrypted_mode.



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

def encrypted_mode
  @encrypted_mode
end

#encrypted_mode_enabledObject

Returns the value of attribute encrypted_mode_enabled.



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

def encrypted_mode_enabled
  @encrypted_mode_enabled
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



84
85
86
87
88
# File 'lib/intercom-rails/script_tag.rb', line 84

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

#encrypted_settingsObject



104
105
106
# File 'lib/intercom-rails/script_tag.rb', line 104

def encrypted_settings
  encrypted_mode.encrypt(intercom_settings)
end

#find_lead_attributesObject



90
91
92
93
94
95
96
97
98
# File 'lib/intercom-rails/script_tag.rb', line 90

def find_lead_attributes
  lead_attributes = IntercomRails.config.user.lead_attributes
  return {} unless controller.present? && lead_attributes && lead_attributes.size > 0

  # Get custom data. This also allows to retrieve custom data
  # set via helper function intercom_custom_data
  custom_data = controller.intercom_custom_data.user.with_indifferent_access
  custom_data.select {|k, v| lead_attributes.map(&:to_s).include?(k)}
end

#intercom_settingsObject



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

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[:hide_default_launcher] = Config.hide_default_launcher if Config.hide_default_launcher
  hsh
end

#plaintext_settingsObject



100
101
102
# File 'lib/intercom-rails/script_tag.rb', line 100

def plaintext_settings
  encrypted_mode.plaintext_part(intercom_settings)
end

#to_sObject



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

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)


37
38
39
40
41
42
43
44
45
46
47
# File 'lib/intercom-rails/script_tag.rb', line 37

def valid?
  return false if user_details[:excluded_user] == true
  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)


49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/intercom-rails/script_tag.rb', line 49

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