intercom-rails 0.0.8 → 0.0.9
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/lib/intercom-rails.rb +3 -0
- data/lib/intercom-rails/auto_include_filter.rb +3 -41
- data/lib/intercom-rails/config.rb +10 -0
- data/lib/intercom-rails/current_user.rb +53 -0
- data/lib/intercom-rails/exceptions.rb +9 -0
- data/lib/intercom-rails/import.rb +0 -2
- data/lib/intercom-rails/script_tag.rb +111 -0
- data/lib/intercom-rails/script_tag_helper.rb +14 -49
- data/lib/intercom-rails/version.rb +1 -1
- data/lib/rails/generators/intercom/config/intercom.rb.erb +11 -0
- data/test/action_controller_test_setup.rb +0 -7
- data/test/intercom-rails/auto_include_filter_test.rb +8 -0
- data/test/intercom-rails/current_user_test.rb +54 -0
- data/test/intercom-rails/script_tag_helper_test.rb +9 -20
- data/test/intercom-rails/script_tag_test.rb +62 -0
- data/test/test_setup.rb +7 -0
- metadata +11 -4
data/lib/intercom-rails.rb
CHANGED
@@ -2,7 +2,6 @@ module IntercomRails
|
|
2
2
|
|
3
3
|
class AutoIncludeFilter
|
4
4
|
|
5
|
-
include ScriptTagHelper
|
6
5
|
CLOSING_BODY_TAG = %r{</body>}
|
7
6
|
|
8
7
|
def self.filter(controller)
|
@@ -19,15 +18,14 @@ module IntercomRails
|
|
19
18
|
end
|
20
19
|
|
21
20
|
def include_javascript!
|
22
|
-
response.body = response.body.gsub(CLOSING_BODY_TAG, intercom_script_tag + '\\0')
|
21
|
+
response.body = response.body.gsub(CLOSING_BODY_TAG, intercom_script_tag.output + '\\0')
|
23
22
|
end
|
24
23
|
|
25
24
|
def include_javascript?
|
26
25
|
!intercom_script_tag_called_manually? &&
|
27
26
|
html_content_type? &&
|
28
27
|
response_has_closing_body_tag? &&
|
29
|
-
|
30
|
-
intercom_user_object.present?
|
28
|
+
intercom_script_tag.valid?
|
31
29
|
end
|
32
30
|
|
33
31
|
private
|
@@ -47,44 +45,8 @@ module IntercomRails
|
|
47
45
|
controller.instance_variable_get(SCRIPT_TAG_HELPER_CALLED_INSTANCE_VARIABLE)
|
48
46
|
end
|
49
47
|
|
50
|
-
POTENTIAL_INTERCOM_USER_OBJECTS = [
|
51
|
-
Proc.new { instance_eval &IntercomRails.config.current_user if IntercomRails.config.current_user.present? },
|
52
|
-
Proc.new { current_user },
|
53
|
-
Proc.new { @user }
|
54
|
-
]
|
55
|
-
|
56
|
-
def intercom_user_object
|
57
|
-
POTENTIAL_INTERCOM_USER_OBJECTS.each do |potential_user|
|
58
|
-
begin
|
59
|
-
user = controller.instance_eval &potential_user
|
60
|
-
return user if user.present? &&
|
61
|
-
(user.email.present? || user.id.present?)
|
62
|
-
rescue NameError
|
63
|
-
next
|
64
|
-
end
|
65
|
-
end
|
66
|
-
|
67
|
-
nil
|
68
|
-
end
|
69
|
-
|
70
|
-
def intercom_app_id
|
71
|
-
return ENV['INTERCOM_APP_ID'] if ENV['INTERCOM_APP_ID'].present?
|
72
|
-
return IntercomRails.config.app_id if IntercomRails.config.app_id.present?
|
73
|
-
return 'abcd1234' if defined?(Rails) && Rails.env.development?
|
74
|
-
|
75
|
-
nil
|
76
|
-
end
|
77
|
-
|
78
48
|
def intercom_script_tag
|
79
|
-
|
80
|
-
user = intercom_user_object
|
81
|
-
|
82
|
-
user_details[:user_id] = user.id if user.respond_to?(:id) && user.id.present?
|
83
|
-
[:email, :name, :created_at].each do |attribute|
|
84
|
-
user_details[attribute] = user.send(attribute) if user.respond_to?(attribute) && user.send(attribute).present?
|
85
|
-
end
|
86
|
-
|
87
|
-
super(user_details)
|
49
|
+
@script_tag ||= ScriptTag.new(:find_current_user_details => true, :controller => controller)
|
88
50
|
end
|
89
51
|
|
90
52
|
end
|
@@ -49,6 +49,16 @@ module IntercomRails
|
|
49
49
|
@user_model
|
50
50
|
end
|
51
51
|
|
52
|
+
# Widget options
|
53
|
+
def self.inbox=(value)
|
54
|
+
raise ArgumentError, "inbox must be one of :default or :custom" unless [:default, :custom].include?(value)
|
55
|
+
@inbox = value
|
56
|
+
end
|
57
|
+
|
58
|
+
def self.inbox
|
59
|
+
@inbox
|
60
|
+
end
|
61
|
+
|
52
62
|
end
|
53
63
|
|
54
64
|
end
|
@@ -0,0 +1,53 @@
|
|
1
|
+
module IntercomRails
|
2
|
+
|
3
|
+
class CurrentUser
|
4
|
+
|
5
|
+
POTENTIAL_USER_OBJECTS = [
|
6
|
+
Proc.new { instance_eval &IntercomRails.config.current_user if IntercomRails.config.current_user.present? },
|
7
|
+
Proc.new { current_user },
|
8
|
+
Proc.new { @user }
|
9
|
+
]
|
10
|
+
|
11
|
+
def self.locate_and_prepare_for_intercom(*args)
|
12
|
+
new(*args).to_hash
|
13
|
+
end
|
14
|
+
|
15
|
+
attr_reader :search_object, :user
|
16
|
+
|
17
|
+
def initialize(search_object)
|
18
|
+
@search_object = search_object
|
19
|
+
@user = find_user
|
20
|
+
end
|
21
|
+
|
22
|
+
def find_user
|
23
|
+
POTENTIAL_USER_OBJECTS.each do |potential_user|
|
24
|
+
begin
|
25
|
+
user = search_object.instance_eval &potential_user
|
26
|
+
return user if user.present? &&
|
27
|
+
(user.email.present? || user.id.present?)
|
28
|
+
rescue NameError
|
29
|
+
next
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
raise CurrentUserNotFoundError
|
34
|
+
end
|
35
|
+
|
36
|
+
def to_hash
|
37
|
+
hsh = {}
|
38
|
+
hsh[:user_id] = user.id if attribute_present?(:id)
|
39
|
+
[:email, :name, :created_at].each do |attribute|
|
40
|
+
hsh[attribute] = user.send(attribute) if attribute_present?(attribute)
|
41
|
+
end
|
42
|
+
|
43
|
+
hsh
|
44
|
+
end
|
45
|
+
|
46
|
+
private
|
47
|
+
def attribute_present?(attribute)
|
48
|
+
user.respond_to?(attribute) && user.send(attribute).present?
|
49
|
+
end
|
50
|
+
|
51
|
+
end
|
52
|
+
|
53
|
+
end
|
@@ -0,0 +1,111 @@
|
|
1
|
+
require "active_support/json"
|
2
|
+
require "active_support/core_ext/hash/indifferent_access"
|
3
|
+
|
4
|
+
module IntercomRails
|
5
|
+
|
6
|
+
class ScriptTag
|
7
|
+
|
8
|
+
def self.generate(*args)
|
9
|
+
new(*args).output
|
10
|
+
end
|
11
|
+
|
12
|
+
attr_reader :user_details
|
13
|
+
attr_accessor :secret, :widget_options, :controller
|
14
|
+
|
15
|
+
def initialize(options = {})
|
16
|
+
self.secret = options[:secret] || Config.api_secret
|
17
|
+
self.widget_options = options[:widget] || widget_options_from_config
|
18
|
+
self.controller = options[:controller]
|
19
|
+
self.user_details = options[:find_current_user_details] ? find_current_user_details : options[:user_details]
|
20
|
+
end
|
21
|
+
|
22
|
+
def valid?
|
23
|
+
user_details[:app_id].present? && (user_details[:user_id] || user_details[:email]).present?
|
24
|
+
end
|
25
|
+
|
26
|
+
def intercom_settings
|
27
|
+
options = {}
|
28
|
+
options[:widget] = widget_options if widget_options.present?
|
29
|
+
|
30
|
+
user_details.merge(options)
|
31
|
+
end
|
32
|
+
|
33
|
+
def output
|
34
|
+
str = <<-INTERCOM_SCRIPT
|
35
|
+
<script id="IntercomSettingsScriptTag">
|
36
|
+
var intercomSettings = #{ActiveSupport::JSON.encode(intercom_settings)};
|
37
|
+
</script>
|
38
|
+
<script>
|
39
|
+
(function() {
|
40
|
+
function async_load() {
|
41
|
+
var s = document.createElement('script');
|
42
|
+
s.type = 'text/javascript'; s.async = true;
|
43
|
+
s.src = 'https://api.intercom.io/api/js/library.js';
|
44
|
+
var x = document.getElementsByTagName('script')[0];
|
45
|
+
x.parentNode.insertBefore(s, x);
|
46
|
+
}
|
47
|
+
if (window.attachEvent) {
|
48
|
+
window.attachEvent('onload', async_load);
|
49
|
+
} else {
|
50
|
+
window.addEventListener('load', async_load, false);
|
51
|
+
}
|
52
|
+
})();
|
53
|
+
</script>
|
54
|
+
INTERCOM_SCRIPT
|
55
|
+
|
56
|
+
str.respond_to?(:html_safe) ? str.html_safe : str
|
57
|
+
end
|
58
|
+
|
59
|
+
private
|
60
|
+
def user_details=(user_details)
|
61
|
+
@user_details = convert_dates_to_unix_timestamps(user_details || {})
|
62
|
+
@user_details = @user_details.with_indifferent_access.tap do |u|
|
63
|
+
[:email, :name, :user_id].each { |k| u.delete(k) if u[k].nil? }
|
64
|
+
|
65
|
+
u[:user_hash] ||= user_hash if secret.present?
|
66
|
+
u[:app_id] ||= app_id
|
67
|
+
end
|
68
|
+
end
|
69
|
+
|
70
|
+
def find_current_user_details
|
71
|
+
return {} unless controller.present?
|
72
|
+
CurrentUser.locate_and_prepare_for_intercom(controller)
|
73
|
+
rescue CurrentUserNotFoundError
|
74
|
+
{}
|
75
|
+
end
|
76
|
+
|
77
|
+
def user_hash
|
78
|
+
components = [secret, (user_details[:user_id] || user_details[:email])]
|
79
|
+
Digest::SHA1.hexdigest(components.join)
|
80
|
+
end
|
81
|
+
|
82
|
+
def app_id
|
83
|
+
return ENV['INTERCOM_APP_ID'] if ENV['INTERCOM_APP_ID'].present?
|
84
|
+
return IntercomRails.config.app_id if IntercomRails.config.app_id.present?
|
85
|
+
return 'abcd1234' if defined?(Rails) && Rails.env.development?
|
86
|
+
|
87
|
+
nil
|
88
|
+
end
|
89
|
+
|
90
|
+
def widget_options_from_config
|
91
|
+
return nil unless Config.inbox
|
92
|
+
|
93
|
+
activator = case Config.inbox
|
94
|
+
when :default
|
95
|
+
'#IntercomDefaultWidget'
|
96
|
+
when :custom
|
97
|
+
'#Intercom'
|
98
|
+
end
|
99
|
+
|
100
|
+
{:activator => activator}
|
101
|
+
end
|
102
|
+
|
103
|
+
def convert_dates_to_unix_timestamps(object)
|
104
|
+
return Hash[object.map { |k, v| [k, convert_dates_to_unix_timestamps(v)] }] if object.is_a?(Hash)
|
105
|
+
return object.strftime('%s').to_i if object.respond_to?(:strftime)
|
106
|
+
object
|
107
|
+
end
|
108
|
+
|
109
|
+
end
|
110
|
+
|
111
|
+
end
|
@@ -1,74 +1,39 @@
|
|
1
|
-
require "active_support/json"
|
2
|
-
require "active_support/core_ext/hash/indifferent_access"
|
3
|
-
|
4
1
|
module IntercomRails
|
5
|
-
# Helper methods for generating Intercom javascript script tags.
|
6
2
|
SCRIPT_TAG_HELPER_CALLED_INSTANCE_VARIABLE = :@_intercom_script_tag_helper_called
|
7
3
|
|
8
4
|
module ScriptTagHelper
|
5
|
+
# Generate an intercom script tag.
|
6
|
+
#
|
9
7
|
# @param user_details [Hash] a customizable hash of user details
|
10
8
|
# @param options [Hash] an optional hash for secure mode and widget customisation
|
11
|
-
# @option user_details [String] :app_id Your application id
|
9
|
+
# @option user_details [String] :app_id Your application id
|
12
10
|
# @option user_details [String] :user_id unique id of this user within your application
|
13
11
|
# @option user_details [String] :email email address for this user
|
14
12
|
# @option user_details [String] :name the users name, _optional_ but useful for identify people in the Intercom App.
|
15
|
-
# @option user_details [Hash] :custom_data custom attributes you'd like saved for this user on Intercom.
|
16
|
-
# @option options [String] :
|
13
|
+
# @option user_details [Hash] :custom_data custom attributes you'd like saved for this user on Intercom.
|
14
|
+
# @option options [String] :widget a hash containing a css selector for an element which when clicked should show the Intercom widget
|
15
|
+
# @option options [String] :secret Your app secret for secure mode
|
17
16
|
# @return [String] Intercom script tag
|
18
17
|
# @example basic example
|
19
18
|
# <%= intercom_script_tag({ :app_id => "your-app-id",
|
20
19
|
# :user_id => current_user.id,
|
21
20
|
# :email => current_user.email,
|
22
21
|
# :custom_data => { :plan => current_user.plan.name },
|
23
|
-
# :name => current_user.name }
|
24
|
-
# ) %>
|
22
|
+
# :name => current_user.name }) %>
|
25
23
|
# @example with widget activator for launching then widget when an element matching the css selector '#Intercom' is clicked.
|
26
24
|
# <%= intercom_script_tag({ :app_id => "your-app-id",
|
27
25
|
# :user_id => current_user.id,
|
28
26
|
# :email => current_user.email,
|
29
27
|
# :custom_data => { :plan => current_user.plan.name },
|
30
|
-
# :name => current_user.name }
|
31
|
-
# {:activator => "#Intercom"}
|
32
|
-
|
33
|
-
def intercom_script_tag(user_details, options={})
|
34
|
-
if options[:secret]
|
35
|
-
secret_string = "#{options[:secret]}#{user_details[:user_id].blank? ? user_details[:email] : user_details[:user_id]}"
|
36
|
-
user_details[:user_hash] = Digest::SHA1.hexdigest(secret_string)
|
37
|
-
end
|
38
|
-
intercom_settings = user_details.merge({:widget => options[:widget]}).with_indifferent_access
|
39
|
-
intercom_settings_with_dates_as_timestamps = convert_dates_to_unix_timestamps(intercom_settings)
|
40
|
-
intercom_settings_with_dates_as_timestamps.reject! { |key, value| %w(email name user_id).include?(key.to_s) && value.nil? }
|
41
|
-
intercom_script = <<-INTERCOM_SCRIPT
|
42
|
-
<script id="IntercomSettingsScriptTag">
|
43
|
-
var intercomSettings = #{ActiveSupport::JSON.encode(intercom_settings_with_dates_as_timestamps)};
|
44
|
-
</script>
|
45
|
-
<script>
|
46
|
-
(function() {
|
47
|
-
function async_load() {
|
48
|
-
var s = document.createElement('script');
|
49
|
-
s.type = 'text/javascript'; s.async = true;
|
50
|
-
s.src = 'https://api.intercom.io/api/js/library.js';
|
51
|
-
var x = document.getElementsByTagName('script')[0];
|
52
|
-
x.parentNode.insertBefore(s, x);
|
53
|
-
}
|
54
|
-
if (window.attachEvent) {
|
55
|
-
window.attachEvent('onload', async_load);
|
56
|
-
} else {
|
57
|
-
window.addEventListener('load', async_load, false);
|
58
|
-
}
|
59
|
-
})();
|
60
|
-
</script>
|
61
|
-
INTERCOM_SCRIPT
|
62
|
-
|
28
|
+
# :name => current_user.name },
|
29
|
+
# {:widget => {:activator => "#Intercom"}},) %>
|
30
|
+
def intercom_script_tag(user_details = nil, options={})
|
63
31
|
controller.instance_variable_set(IntercomRails::SCRIPT_TAG_HELPER_CALLED_INSTANCE_VARIABLE, true) if defined?(controller)
|
64
|
-
|
65
|
-
|
32
|
+
options[:user_details] = user_details if user_details.present?
|
33
|
+
options[:find_current_user_details] = !options[:user_details]
|
34
|
+
options[:controller] = controller if defined?(controller)
|
66
35
|
|
67
|
-
|
68
|
-
def convert_dates_to_unix_timestamps(object)
|
69
|
-
return Hash[object.map { |k, v| [k, convert_dates_to_unix_timestamps(v)] }] if object.is_a?(Hash)
|
70
|
-
return object.strftime('%s').to_i if object.respond_to?(:strftime)
|
71
|
-
object
|
36
|
+
ScriptTag.generate(options)
|
72
37
|
end
|
73
38
|
end
|
74
39
|
end
|
@@ -33,4 +33,15 @@ IntercomRails.config do |config|
|
|
33
33
|
# The class which defines your user model
|
34
34
|
#
|
35
35
|
# config.user_model = Proc.new { User }
|
36
|
+
|
37
|
+
# == Inbox
|
38
|
+
# This enables the Intercom inbox which allows your users to read their
|
39
|
+
# past conversations with your app, as well as start new ones.
|
40
|
+
# * :default shows a small tab with a question mark icon on it
|
41
|
+
# * :custom attaches the inbox open event to an anchor with an
|
42
|
+
# id of #Intercom.
|
43
|
+
#
|
44
|
+
# config.inbox = :default
|
45
|
+
# config.inbox = :custom
|
46
|
+
|
36
47
|
end
|
@@ -3,13 +3,6 @@ require 'test_setup'
|
|
3
3
|
require 'action_controller'
|
4
4
|
require 'action_controller/test_case'
|
5
5
|
|
6
|
-
def dummy_user(options = {})
|
7
|
-
user = Struct.new(:email, :name).new
|
8
|
-
user.email = options[:email] || 'ben@intercom.io'
|
9
|
-
user.name = options[:name] || 'Ben McRedmond'
|
10
|
-
user
|
11
|
-
end
|
12
|
-
|
13
6
|
TestRoutes = ActionDispatch::Routing::RouteSet.new
|
14
7
|
TestRoutes.draw do
|
15
8
|
get ':controller(/:action)'
|
@@ -83,6 +83,14 @@ class AutoIncludeFilterTest < ActionController::TestCase
|
|
83
83
|
assert_includes @response.body, "Eoghan McCabe"
|
84
84
|
end
|
85
85
|
|
86
|
+
def test_auto_insert_with_api_secret_set
|
87
|
+
IntercomRails.config.api_secret = 'abcd'
|
88
|
+
get :with_current_user_method, :body => "<body>Hello world</body>"
|
89
|
+
assert_includes @response.body, "<script>"
|
90
|
+
assert_includes @response.body, "user_hash"
|
91
|
+
assert_includes @response.body, Digest::SHA1.hexdigest('abcd' + @controller.current_user.email)
|
92
|
+
end
|
93
|
+
|
86
94
|
def test_no_app_id_present
|
87
95
|
ENV.delete('INTERCOM_APP_ID')
|
88
96
|
get :with_current_user_method, :body => "<body>Hello world</body>"
|
@@ -0,0 +1,54 @@
|
|
1
|
+
require 'test_setup'
|
2
|
+
|
3
|
+
class CurrentUserTest < MiniTest::Unit::TestCase
|
4
|
+
|
5
|
+
include IntercomRails
|
6
|
+
|
7
|
+
DUMMY_USER = dummy_user(:email => 'ciaran@intercom.io', :name => 'Ciaran Lee')
|
8
|
+
|
9
|
+
def test_raises_error_when_no_user_found
|
10
|
+
assert_raises(IntercomRails::CurrentUserNotFoundError) {
|
11
|
+
CurrentUser.new(Object.new)
|
12
|
+
}
|
13
|
+
end
|
14
|
+
|
15
|
+
def test_finds_current_user
|
16
|
+
object_with_current_user_method = Object.new
|
17
|
+
object_with_current_user_method.instance_eval do
|
18
|
+
def current_user
|
19
|
+
DUMMY_USER
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
@current_user = CurrentUser.new(object_with_current_user_method)
|
24
|
+
assert_user_found
|
25
|
+
end
|
26
|
+
|
27
|
+
def test_finds_user_instance_variable
|
28
|
+
object_with_instance_variable = Object.new
|
29
|
+
object_with_instance_variable.instance_eval do
|
30
|
+
@user = DUMMY_USER
|
31
|
+
end
|
32
|
+
|
33
|
+
@current_user = CurrentUser.new(object_with_instance_variable)
|
34
|
+
assert_user_found
|
35
|
+
end
|
36
|
+
|
37
|
+
def test_finds_config_user
|
38
|
+
object_from_config = Object.new
|
39
|
+
object_from_config.instance_eval do
|
40
|
+
def something_esoteric
|
41
|
+
DUMMY_USER
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
IntercomRails.config.current_user = Proc.new { something_esoteric }
|
46
|
+
@current_user = CurrentUser.new(object_from_config)
|
47
|
+
assert_user_found
|
48
|
+
end
|
49
|
+
|
50
|
+
def assert_user_found
|
51
|
+
assert_equal DUMMY_USER, @current_user.user
|
52
|
+
end
|
53
|
+
|
54
|
+
end
|
@@ -4,28 +4,17 @@ require 'test_setup'
|
|
4
4
|
class ScriptTagHelperTest < MiniTest::Unit::TestCase
|
5
5
|
|
6
6
|
include IntercomRails::ScriptTagHelper
|
7
|
-
def test_output_is_html_safe?
|
8
|
-
assert_equal true, intercom_script_tag({}).html_safe?
|
9
|
-
end
|
10
|
-
|
11
|
-
def test_converts_times_to_unix_timestamps
|
12
|
-
now = Time.now
|
13
|
-
assert_match(/.created_at.\s*:\s*#{now.to_i}/, intercom_script_tag({:created_at => now}))
|
14
|
-
assert_match(/.something.\s*:\s*#{now.to_i}/, intercom_script_tag({:custom_data => {"something" => now}}))
|
15
|
-
end
|
16
7
|
|
17
|
-
def
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
end
|
23
|
-
end
|
8
|
+
def test_delegates_to_script_tag_generate
|
9
|
+
delegated = false
|
10
|
+
IntercomRails::ScriptTag.stub(:generate) {
|
11
|
+
delegated = true
|
12
|
+
}
|
24
13
|
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
14
|
+
intercom_script_tag({})
|
15
|
+
assert(delegated, "should delegate to ScriptTag#generate")
|
16
|
+
ensure
|
17
|
+
IntercomRails::ScriptTag.rspec_reset
|
29
18
|
end
|
30
19
|
|
31
20
|
def test_sets_instance_variable
|
@@ -0,0 +1,62 @@
|
|
1
|
+
require 'active_support/core_ext/string/output_safety'
|
2
|
+
require 'test_setup'
|
3
|
+
|
4
|
+
class ScriptTagTest < MiniTest::Unit::TestCase
|
5
|
+
|
6
|
+
include IntercomRails
|
7
|
+
|
8
|
+
def test_output_is_html_safe?
|
9
|
+
assert_equal true, ScriptTag.generate({}).html_safe?
|
10
|
+
end
|
11
|
+
|
12
|
+
def test_converts_times_to_unix_timestamps
|
13
|
+
time = Time.new(1993,02,13)
|
14
|
+
top_level_time = ScriptTag.new(:user_details => {:created_at => time})
|
15
|
+
assert_equal time.to_i, top_level_time.intercom_settings[:created_at]
|
16
|
+
|
17
|
+
now = Time.now
|
18
|
+
nested_time = ScriptTag.new(:user_details => {:custom_data => {"something" => now}})
|
19
|
+
assert_equal now.to_i, nested_time.intercom_settings[:custom_data]["something"]
|
20
|
+
end
|
21
|
+
|
22
|
+
def test_strips_out_nil_entries_for_standard_attributes
|
23
|
+
%w(name email user_id).each do |standard_attribute|
|
24
|
+
with_value = ScriptTag.new(:user_details => {standard_attribute => 'value'})
|
25
|
+
assert_equal with_value.intercom_settings[standard_attribute], 'value'
|
26
|
+
|
27
|
+
with_nil_value = ScriptTag.new(:user_details => {standard_attribute.to_sym => 'value'})
|
28
|
+
assert with_nil_value.intercom_settings.has_key?(standard_attribute.to_sym), "should strip :#{standard_attribute} when nil"
|
29
|
+
|
30
|
+
with_nil_value = ScriptTag.new(:user_details => {standard_attribute => 'value'})
|
31
|
+
assert with_nil_value.intercom_settings.has_key?(standard_attribute), "should strip #{standard_attribute} when nil"
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
def test_secure_mode_with_email
|
36
|
+
script_tag = ScriptTag.new(:user_details => {:email => 'ciaran@intercom.io'}, :secret => 'abcdefgh')
|
37
|
+
assert_equal Digest::SHA1.hexdigest('abcdefgh' + 'ciaran@intercom.io'), script_tag.intercom_settings[:user_hash]
|
38
|
+
end
|
39
|
+
|
40
|
+
def test_secure_mode_with_user_id
|
41
|
+
script_tag = ScriptTag.new(:user_details => {:user_id => '1234'}, :secret => 'abcdefgh')
|
42
|
+
assert_equal Digest::SHA1.hexdigest('abcdefgh' + '1234'), script_tag.intercom_settings[:user_hash]
|
43
|
+
end
|
44
|
+
|
45
|
+
def test_secure_mode_with_email_and_user_id
|
46
|
+
script_tag = ScriptTag.new(:user_details => {:user_id => '1234', :email => 'ciaran@intercom.io'}, :secret => 'abcdefgh')
|
47
|
+
assert_equal Digest::SHA1.hexdigest('abcdefgh' + '1234'), script_tag.intercom_settings[:user_hash]
|
48
|
+
end
|
49
|
+
|
50
|
+
def test_secure_mode_with_secret_from_config
|
51
|
+
IntercomRails.config.api_secret = 'abcd'
|
52
|
+
script_tag = ScriptTag.new(:user_details => {:email => 'ben@intercom.io'})
|
53
|
+
assert_equal Digest::SHA1.hexdigest('abcd' + 'ben@intercom.io'), script_tag.intercom_settings[:user_hash]
|
54
|
+
end
|
55
|
+
|
56
|
+
def test_secure_mode_chooses_passed_secret_over_config
|
57
|
+
IntercomRails.config.api_secret = 'abcd'
|
58
|
+
script_tag = ScriptTag.new(:user_details => {:email => 'ben@intercom.io'}, :secret => '1234')
|
59
|
+
assert_equal Digest::SHA1.hexdigest('1234' + 'ben@intercom.io'), script_tag.intercom_settings[:user_hash]
|
60
|
+
end
|
61
|
+
|
62
|
+
end
|
data/test/test_setup.rb
CHANGED
@@ -3,6 +3,13 @@ require 'minitest/autorun'
|
|
3
3
|
require 'rspec/mocks'
|
4
4
|
require 'pry'
|
5
5
|
|
6
|
+
def dummy_user(options = {})
|
7
|
+
user = Struct.new(:email, :name).new
|
8
|
+
user.email = options[:email] || 'ben@intercom.io'
|
9
|
+
user.name = options[:name] || 'Ben McRedmond'
|
10
|
+
user
|
11
|
+
end
|
12
|
+
|
6
13
|
def fake_action_view_class
|
7
14
|
klass = Class.new(ActionView::Base)
|
8
15
|
klass.class_eval do
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: intercom-rails
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.9
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -11,7 +11,7 @@ authors:
|
|
11
11
|
autorequire:
|
12
12
|
bindir: bin
|
13
13
|
cert_chain: []
|
14
|
-
date: 2012-11-
|
14
|
+
date: 2012-11-22 00:00:00.000000000 Z
|
15
15
|
dependencies:
|
16
16
|
- !ruby/object:Gem::Dependency
|
17
17
|
name: activesupport
|
@@ -139,9 +139,12 @@ files:
|
|
139
139
|
- lib/data/cacert.pem
|
140
140
|
- lib/intercom-rails/auto_include_filter.rb
|
141
141
|
- lib/intercom-rails/config.rb
|
142
|
+
- lib/intercom-rails/current_user.rb
|
143
|
+
- lib/intercom-rails/exceptions.rb
|
142
144
|
- lib/intercom-rails/import.rb
|
143
145
|
- lib/intercom-rails/intercom.rake
|
144
146
|
- lib/intercom-rails/railtie.rb
|
147
|
+
- lib/intercom-rails/script_tag.rb
|
145
148
|
- lib/intercom-rails/script_tag_helper.rb
|
146
149
|
- lib/intercom-rails/version.rb
|
147
150
|
- lib/intercom-rails.rb
|
@@ -154,9 +157,11 @@ files:
|
|
154
157
|
- test/import_test_setup.rb
|
155
158
|
- test/intercom-rails/auto_include_filter_test.rb
|
156
159
|
- test/intercom-rails/config_test.rb
|
160
|
+
- test/intercom-rails/current_user_test.rb
|
157
161
|
- test/intercom-rails/import_network_test.rb
|
158
162
|
- test/intercom-rails/import_unit_test.rb
|
159
163
|
- test/intercom-rails/script_tag_helper_test.rb
|
164
|
+
- test/intercom-rails/script_tag_test.rb
|
160
165
|
- test/test_setup.rb
|
161
166
|
homepage: http://www.intercom.io
|
162
167
|
licenses: []
|
@@ -172,7 +177,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
172
177
|
version: '0'
|
173
178
|
segments:
|
174
179
|
- 0
|
175
|
-
hash:
|
180
|
+
hash: 1999589509538481455
|
176
181
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
177
182
|
none: false
|
178
183
|
requirements:
|
@@ -181,7 +186,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
181
186
|
version: '0'
|
182
187
|
segments:
|
183
188
|
- 0
|
184
|
-
hash:
|
189
|
+
hash: 1999589509538481455
|
185
190
|
requirements: []
|
186
191
|
rubyforge_project: intercom-rails
|
187
192
|
rubygems_version: 1.8.23
|
@@ -193,7 +198,9 @@ test_files:
|
|
193
198
|
- test/import_test_setup.rb
|
194
199
|
- test/intercom-rails/auto_include_filter_test.rb
|
195
200
|
- test/intercom-rails/config_test.rb
|
201
|
+
- test/intercom-rails/current_user_test.rb
|
196
202
|
- test/intercom-rails/import_network_test.rb
|
197
203
|
- test/intercom-rails/import_unit_test.rb
|
198
204
|
- test/intercom-rails/script_tag_helper_test.rb
|
205
|
+
- test/intercom-rails/script_tag_test.rb
|
199
206
|
- test/test_setup.rb
|