Class: IntercomRails::ScriptTag
- Inherits:
-
Object
- Object
- IntercomRails::ScriptTag
- Includes:
- ActionView::Helpers::JavaScriptHelper
- Defined in:
- lib/intercom-rails/script_tag.rb
Instance Attribute Summary collapse
-
#company_details ⇒ Object
readonly
Returns the value of attribute company_details.
-
#controller ⇒ Object
Returns the value of attribute controller.
-
#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
-
#initialize(options = {}) ⇒ ScriptTag
constructor
A new instance of ScriptTag.
- #intercom_settings ⇒ Object
- #to_s ⇒ Object
- #valid? ⇒ Boolean
- #valid_nonce? ⇒ Boolean
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( = {}) self.secret = [:secret] || Config.api_secret self. = .merge([:widget] || {}) self.controller = [:controller] @show_everywhere = [:show_everywhere] @session_duration = session_duration_from_config self.user_details = [:find_current_user_details] ? find_current_user_details : [:user_details] self.company_details = if [:find_current_company_details] find_current_company_details elsif [:user_details] [:user_details].delete(:company) if [:user_details] end self.nonce = [:nonce] end |
Instance Attribute Details
#company_details ⇒ Object
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 |
#controller ⇒ Object
Returns the value of attribute controller.
13 14 15 |
# File 'lib/intercom-rails/script_tag.rb', line 13 def controller @controller end |
#nonce ⇒ Object
Returns the value of attribute nonce.
13 14 15 |
# File 'lib/intercom-rails/script_tag.rb', line 13 def nonce @nonce end |
#secret ⇒ Object
Returns the value of attribute secret.
13 14 15 |
# File 'lib/intercom-rails/script_tag.rb', line 13 def secret @secret end |
#session_duration ⇒ Object (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_everywhere ⇒ Object (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_details ⇒ Object
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_options ⇒ Object
Returns the value of attribute widget_options.
13 14 15 |
# File 'lib/intercom-rails/script_tag.rb', line 13 def @widget_options end |
Instance Method Details
#csp_sha256 ⇒ Object
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_settings ⇒ Object
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] = if .present? hsh[:company] = company_details if company_details.present? hsh end |
#to_s ⇒ Object
66 67 68 69 70 71 72 73 |
# File 'lib/intercom-rails/script_tag.rb', line 66 def to_s = 'id="IntercomSettingsScriptTag"' if nonce && valid_nonce? = + " nonce=\"#{nonce}\"" end str = "<script #{}>#{intercom_javascript}</script>\n" str.respond_to?(:html_safe) ? str.html_safe : str end |
#valid? ⇒ 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
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 |