Class: IntercomRails::ScriptTag

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ ScriptTag


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

def initialize(options = {})
  self.secret = options[:secret] || Config.api_secret
  self.widget_options = widget_options_from_config.merge(options[:widget] || {})
  self.controller = options[:controller]
  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
end

Instance Attribute Details

#company_detailsObject

Returns the value of attribute company_details


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

def company_details
  @company_details
end

#controllerObject

Returns the value of attribute controller


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

def controller
  @controller
end

#secretObject

Returns the value of attribute secret


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

def secret
  @secret
end

#user_detailsObject

Returns the value of attribute user_details


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

def user_details
  @user_details
end

#widget_optionsObject

Returns the value of attribute widget_options


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

def widget_options
  @widget_options
end

Class Method Details

.generate(*args) ⇒ Object


9
10
11
# File 'lib/intercom-rails/script_tag.rb', line 9

def self.generate(*args)
  new(*args).output
end

Instance Method Details

#intercom_settingsObject


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

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

#outputObject


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

def output
  intercom_settings_json = ActiveSupport::JSON.encode(intercom_settings).gsub('<', '\u003C')

  str = <<-INTERCOM_SCRIPT
<script id="IntercomSettingsScriptTag">
  window.intercomSettings = #{intercom_settings_json};
</script>
<script>(function(){var w=window;var ic=w.Intercom;if(typeof ic==="function"){ic('reattach_activator');ic('update',intercomSettings);}else{var d=document;var i=function(){i.c(arguments)};i.q=[];i.c=function(args){i.q.push(args)};w.Intercom=i;function l(){var s=d.createElement('script');s.type='text/javascript';s.async=true;s.src='#{Config.library_url || "https://widget.intercom.io/widget/#{app_id}"}';var x=d.getElementsByTagName('script')[0];x.parentNode.insertBefore(s,x);}if(w.attachEvent){w.attachEvent('onload',l);}else{w.addEventListener('load',l,false);}};})()</script>
  INTERCOM_SCRIPT

  str.respond_to?(:html_safe) ? str.html_safe : str
end

#valid?Boolean


28
29
30
# File 'lib/intercom-rails/script_tag.rb', line 28

def valid?
  user_details[:app_id].present? && (user_details[:user_id] || user_details[:email]).present?
end