Class: IntercomRails::ScriptTag
- Inherits:
-
Object
- Object
- IntercomRails::ScriptTag
- 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
-
#company_details ⇒ Object
readonly
Returns the value of attribute company_details.
-
#controller ⇒ Object
Returns the value of attribute controller.
-
#encrypted_mode ⇒ Object
Returns the value of attribute encrypted_mode.
-
#encrypted_mode_enabled ⇒ Object
Returns the value of attribute encrypted_mode_enabled.
-
#jwt_enabled ⇒ Object
Returns the value of attribute jwt_enabled.
-
#jwt_expiry ⇒ Object
Returns the value of attribute jwt_expiry.
-
#nonce ⇒ Object
Returns the value of attribute nonce.
-
#secret ⇒ Object
Returns the value of attribute secret.
-
#session_duration ⇒ Object
readonly
Returns the value of attribute session_duration.
-
#show_everywhere ⇒ Object
readonly
Returns the value of attribute show_everywhere.
-
#user_details ⇒ Object
readonly
Returns the value of attribute user_details.
-
#widget_options ⇒ Object
Returns the value of attribute widget_options.
Instance Method Summary collapse
- #csp_sha256 ⇒ Object
- #encrypted_settings ⇒ Object
- #find_lead_attributes ⇒ Object
-
#initialize(options = {}) ⇒ ScriptTag
constructor
A new instance of ScriptTag.
- #intercom_settings ⇒ Object
- #plaintext_settings ⇒ Object
- #to_s ⇒ Object
- #valid? ⇒ Boolean
- #valid_nonce? ⇒ Boolean
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( = {}) self.secret = [:secret] || Config.api_secret self. = .merge([:widget] || {}) self.controller = [:controller] @show_everywhere = [:show_everywhere] @session_duration = session_duration_from_config self.jwt_enabled = [:jwt_enabled] || Config.jwt.enabled self.jwt_expiry = [:jwt_expiry] || Config.jwt.expiry initial_user_details = if [:find_current_user_details] find_current_user_details else [:user_details] || {} end lead_attributes = find_lead_attributes self.user_details = initial_user_details.merge(lead_attributes) self.encrypted_mode_enabled = [:encrypted_mode] || Config.encrypted_mode self.encrypted_mode = IntercomRails::EncryptedMode.new(secret, [:initialization_vector], {:enabled => encrypted_mode_enabled}) self.company_details = if [:find_current_company_details] find_current_company_details elsif [:user_details] [:user_details].delete(:company) end self.nonce = [:nonce] end |
Instance Attribute Details
#company_details ⇒ Object
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 |
#controller ⇒ Object
Returns the value of attribute controller.
21 22 23 |
# File 'lib/intercom-rails/script_tag.rb', line 21 def controller @controller end |
#encrypted_mode ⇒ Object
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_enabled ⇒ Object
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_enabled ⇒ Object
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_expiry ⇒ Object
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 |
#nonce ⇒ Object
Returns the value of attribute nonce.
21 22 23 |
# File 'lib/intercom-rails/script_tag.rb', line 21 def nonce @nonce end |
#secret ⇒ Object
Returns the value of attribute secret.
21 22 23 |
# File 'lib/intercom-rails/script_tag.rb', line 21 def secret @secret end |
#session_duration ⇒ Object (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_everywhere ⇒ Object (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_details ⇒ Object
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_options ⇒ Object
Returns the value of attribute widget_options.
21 22 23 |
# File 'lib/intercom-rails/script_tag.rb', line 21 def @widget_options end |
Instance Method Details
#csp_sha256 ⇒ Object
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_settings ⇒ Object
112 113 114 |
# File 'lib/intercom-rails/script_tag.rb', line 112 def encrypted_settings encrypted_mode.encrypt(intercom_settings) end |
#find_lead_attributes ⇒ Object
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_settings ⇒ Object
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] = if .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_settings ⇒ Object
108 109 110 |
# File 'lib/intercom-rails/script_tag.rb', line 108 def plaintext_settings encrypted_mode.plaintext_part(intercom_settings) end |
#to_s ⇒ Object
86 87 88 89 90 |
# File 'lib/intercom-rails/script_tag.rb', line 86 def to_s = { id: 'IntercomSettingsScriptTag' } ['nonce'] = nonce if valid_nonce? javascript_tag(intercom_javascript, ) + "\n" end |
#valid? ⇒ 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
66 67 68 |
# File 'lib/intercom-rails/script_tag.rb', line 66 def valid_nonce? nonce && NONCE_RE.match?(nonce) end |