Module: ClankerSupport

Defined in:
lib/clankersupport/railtie.rb,
lib/clankersupport.rb,
lib/clankersupport/version.rb

Overview

Rails integration. This file is only required from lib/clankersupport.rb when Rails::Railtie is defined, and every constant reference below is guarded, so the gem loads fine without Rails.

Defined Under Namespace

Modules: Helper Classes: Railtie

Constant Summary collapse

DEFAULT_API_URL =
"https://api.clankersupport.com"
MODES =

data-mode values the widget understands. bubble (the default) is the floating launcher; inline mounts the panel in place.

["bubble", "inline"].freeze
THEMES =

data-theme values the widget understands. Absent means light.

["light", "dark", "auto"].freeze
VERSION =
"1.0.0"

Class Method Summary collapse

Class Method Details

.script_tag(project_key, api_url: DEFAULT_API_URL, brand_color: nil, mode: nil, theme: nil, escalation_threshold: nil) ⇒ String

Build the Clanker Support widget <script> tag.

Parameters:

  • project_key (String)

    the project's public widget key (dashboard → Project → Embed). Safe to expose — it's the same key the script embed uses.

  • api_url (String) (defaults to: DEFAULT_API_URL)

    API origin. Point at your own deployment when self-hosting; trailing slashes are stripped.

  • brand_color (String, nil) (defaults to: nil)

    accent color for the launcher/header, e.g. "#4f46e5". Omitted → the widget default.

  • mode (String, nil) (defaults to: nil)

    "bubble" (floating launcher, default) or "inline".

  • theme (String, nil) (defaults to: nil)

    "light" (default), "dark", or "auto" (follow OS).

  • escalation_threshold (Integer, nil) (defaults to: nil)

    visitor messages before "Talk to a human" appears. Omitted → the project's configured default.

Returns:

  • (String)

Raises:

  • (ArgumentError)

    on an empty project key, an unknown mode/theme, or a non-positive escalation threshold — misconfiguration should fail at render time in development, not silently ship a broken widget.



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/clankersupport.rb', line 48

def self.script_tag(
	project_key,
	api_url: DEFAULT_API_URL,
	brand_color: nil,
	mode: nil,
	theme: nil,
	escalation_threshold: nil
)
	key = project_key.to_s
	raise ArgumentError, "project_key is required" if key.strip.empty?
	if !mode.nil? && !MODES.include?(mode)
		raise ArgumentError, "mode must be one of #{MODES.inspect}, got #{mode.inspect}"
	end
	if !theme.nil? && !THEMES.include?(theme)
		raise ArgumentError, "theme must be one of #{THEMES.inspect}, got #{theme.inspect}"
	end
	if !escalation_threshold.nil?
		unless escalation_threshold.is_a?(Integer) && escalation_threshold >= 1
			raise ArgumentError,
				"escalation_threshold must be a positive Integer, got #{escalation_threshold.inspect}"
		end
	end

	base = api_url.to_s.sub(%r{/+\z}, "")
	parts = [
		%(<script src="#{escape_attr(base)}/widget.js"),
		%(data-project="#{escape_attr(key)}"),
		%(data-api="#{escape_attr(base)}"),
	]
	parts << %(data-brand="#{escape_attr(brand_color)}") unless brand_color.nil?
	parts << %(data-mode="#{escape_attr(mode)}") unless mode.nil?
	parts << %(data-theme="#{escape_attr(theme)}") unless theme.nil?
	unless escalation_threshold.nil?
		parts << %(data-escalation-threshold="#{escalation_threshold}")
	end
	parts << "async></script>"
	parts.join(" ")
end