class IntercomRails::AutoInclude::Filter

Constants

BLACKLISTED_CONTROLLER_NAMES
CLOSING_BODY_TAG

Attributes

controller[R]

Public Class Methods

filter(controller) click to toggle source
# File lib/intercom-rails/auto_include_filter.rb, line 16
def self.filter(controller)
  return if BLACKLISTED_CONTROLLER_NAMES.include?(controller.class.name)
  auto_include_filter = new(controller)
  return unless auto_include_filter.include_javascript?

  auto_include_filter.include_javascript!

  # User defined method to whitelist the script sha-256 when using CSP
  if defined?(CoreExtensions::IntercomRails::AutoInclude.csp_sha256_hook) == 'method'
    CoreExtensions::IntercomRails::AutoInclude.csp_sha256_hook(controller, auto_include_filter.csp_sha256)
  end
end
new(kontroller) click to toggle source
# File lib/intercom-rails/auto_include_filter.rb, line 31
def initialize(kontroller)
  @controller = kontroller
end

Public Instance Methods

csp_sha256() click to toggle source
# File lib/intercom-rails/auto_include_filter.rb, line 47
def csp_sha256
  intercom_script_tag.csp_sha256
end
include_javascript!() click to toggle source
# File lib/intercom-rails/auto_include_filter.rb, line 35
def include_javascript!
  response.body = response.body.insert(response.body.rindex(CLOSING_BODY_TAG), intercom_script_tag.to_s)
end
include_javascript?() click to toggle source
# File lib/intercom-rails/auto_include_filter.rb, line 39
def include_javascript?
  enabled_for_environment? &&
  !intercom_script_tag_called_manually? &&
  html_content_type? &&
  response_has_closing_body_tag? &&
  intercom_script_tag.valid?
end

Private Instance Methods

enabled_for_environment?() click to toggle source
# File lib/intercom-rails/auto_include_filter.rb, line 92
def enabled_for_environment?
  enabled_environments = IntercomRails.config.enabled_environments
  return true if enabled_environments.nil?
  enabled_environments.map(&:to_s).include?(Rails.env)
end
html_content_type?() click to toggle source
# File lib/intercom-rails/auto_include_filter.rb, line 56
def html_content_type?
  if response.respond_to?(:media_type)
    response.media_type == 'text/html'
  else
    response.content_type == 'text/html'
  end
end
intercom_script_tag() click to toggle source
# File lib/intercom-rails/auto_include_filter.rb, line 72
def intercom_script_tag
  options = {
    :find_current_user_details => true,
    :find_current_company_details => true,
    :controller => controller,
    :show_everywhere => show_everywhere?
  }
  # User defined method for applying a nonce to the inserted js tag when
  # using CSP
  if defined?(CoreExtensions::IntercomRails::AutoInclude.csp_nonce_hook) == 'method'
    nonce = CoreExtensions::IntercomRails::AutoInclude.csp_nonce_hook(controller)
    options.merge!(:nonce => nonce)
  end
  @script_tag ||= ScriptTag.new(options)
end
intercom_script_tag_called_manually?() click to toggle source
# File lib/intercom-rails/auto_include_filter.rb, line 68
def intercom_script_tag_called_manually?
  controller.instance_variable_get(SCRIPT_TAG_HELPER_CALLED_INSTANCE_VARIABLE)
end
response() click to toggle source
# File lib/intercom-rails/auto_include_filter.rb, line 52
def response
  controller.response
end
response_has_closing_body_tag?() click to toggle source
# File lib/intercom-rails/auto_include_filter.rb, line 64
def response_has_closing_body_tag?
  response.body.include? CLOSING_BODY_TAG
end
show_everywhere?() click to toggle source
# File lib/intercom-rails/auto_include_filter.rb, line 88
def show_everywhere?
  IntercomRails.config.include_for_logged_out_users
end