Class: IntercomRails::ScriptTag

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

Constant Summary collapse

NONCE_RE =

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+/] + ‘=’

%r{^(?:[A-Za-z0-9+/]{4})*(?:[A-Za-z0-9+/]{2}==|[A-Za-z0-9+/]{3}=)?$}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ ScriptTag

Returns a new instance of ScriptTag.



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/intercom-rails/script_tag.rb', line 23

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.jwt_enabled = options[:jwt_enabled] || Config.jwt.enabled
  self.jwt_expiry = options[:jwt_expiry] || Config.jwt.expiry

  initial_user_details = if options[:find_current_user_details]
    find_current_user_details
  else
    options[:user_details] || {}
  end

  lead_attributes = find_lead_attributes

  self.user_details = initial_user_details.merge(lead_attributes)

  self.encrypted_mode_enabled = options[:encrypted_mode] || Config.encrypted_mode
  self.encrypted_mode = IntercomRails::EncryptedMode.new(secret, options[:initialization_vector], {:enabled => encrypted_mode_enabled})

  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.



20
21
22
# File 'lib/intercom-rails/script_tag.rb', line 20

def company_details
  @company_details
end

#controllerObject

Returns the value of attribute controller.



21
22
23
# File 'lib/intercom-rails/script_tag.rb', line 21

def controller
  @controller
end

#encrypted_modeObject

Returns the value of attribute encrypted_mode.



21
22
23
# File 'lib/intercom-rails/script_tag.rb', line 21

def encrypted_mode
  @encrypted_mode
end

#encrypted_mode_enabledObject

Returns the value of attribute encrypted_mode_enabled.



21
22
23
# File 'lib/intercom-rails/script_tag.rb', line 21

def encrypted_mode_enabled
  @encrypted_mode_enabled
end

#jwt_enabledObject

Returns the value of attribute jwt_enabled.



21
22
23
# File 'lib/intercom-rails/script_tag.rb', line 21

def jwt_enabled
  @jwt_enabled
end

#jwt_expiryObject

Returns the value of attribute jwt_expiry.



21
22
23
# File 'lib/intercom-rails/script_tag.rb', line 21

def jwt_expiry
  @jwt_expiry
end

#nonceObject

Returns the value of attribute nonce.



21
22
23
# File 'lib/intercom-rails/script_tag.rb', line 21

def nonce
  @nonce
end

#secretObject

Returns the value of attribute secret.



21
22
23
# File 'lib/intercom-rails/script_tag.rb', line 21

def secret
  @secret
end

#session_durationObject (readonly)

Returns the value of attribute session_duration.



20
21
22
# File 'lib/intercom-rails/script_tag.rb', line 20

def session_duration
  @session_duration
end

#show_everywhereObject (readonly)

Returns the value of attribute show_everywhere.



20
21
22
# File 'lib/intercom-rails/script_tag.rb', line 20

def show_everywhere
  @show_everywhere
end

#user_detailsObject

Returns the value of attribute user_details.



20
21
22
# File 'lib/intercom-rails/script_tag.rb', line 20

def user_details
  @user_details
end

#widget_optionsObject

Returns the value of attribute widget_options.



21
22
23
# File 'lib/intercom-rails/script_tag.rb', line 21

def widget_options
  @widget_options
end

Instance Method Details

#csp_sha256Object



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

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

#encrypted_settingsObject



112
113
114
# File 'lib/intercom-rails/script_tag.rb', line 112

def encrypted_settings
  encrypted_mode.encrypt(intercom_settings)
end

#find_lead_attributesObject



98
99
100
101
102
103
104
105
106
# File 'lib/intercom-rails/script_tag.rb', line 98

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



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

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[:api_base] = Config.api_base if Config.api_base
  hsh[:installation_type] = 'rails'
  hsh
end

#plaintext_settingsObject



108
109
110
# File 'lib/intercom-rails/script_tag.rb', line 108

def plaintext_settings
  encrypted_mode.plaintext_part(intercom_settings)
end

#to_sObject



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

def to_s
  html_options = { id: 'IntercomSettingsScriptTag' }
  html_options['nonce'] = nonce if valid_nonce?
  javascript_tag(intercom_javascript, html_options) + "\n"
end

#valid?Boolean

Returns:

  • (Boolean)


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

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)


66
67
68
# File 'lib/intercom-rails/script_tag.rb', line 66

def valid_nonce?
  nonce && NONCE_RE.match?(nonce)
end