intercom-rails 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,21 @@
1
+ The MIT License
2
+
3
+ Copyright (c) 2011- Intercom App, Inc. (https://www.intercom.io)
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
@@ -0,0 +1,31 @@
1
+ = IntercomRails
2
+
3
+ Rails helper for creating Intercom (https://www.intercom.io) javascript tags.
4
+
5
+ For interacting with the Intercom API, use the intercom gem (https://github.com/intercom/intercom-ruby)
6
+
7
+ == Installation
8
+ Add this to your Gemfile:
9
+
10
+ gem "intercom-rails"
11
+
12
+ <code>bundle install</code>
13
+
14
+ == Usage
15
+ In your layout file:
16
+
17
+ <% if logged_in? %>
18
+ <%= intercom_script_tag({
19
+ :app_id => 'your-app-id'
20
+ :user_id => current_user.id
21
+ :email => current_user.email
22
+ :name => current_user.name
23
+ :created_at => current_user.created_at
24
+ :custom_data => {:plan => "Pro"}}) %>
25
+ <% end %>
26
+
27
+ See {IntercomRails::ScriptTagHelper} for more details.
28
+
29
+ == License
30
+
31
+ This project rocks and uses MIT-LICENSE.
@@ -0,0 +1,20 @@
1
+ #!/usr/bin/env rake
2
+ begin
3
+ require 'bundler/setup'
4
+ rescue LoadError
5
+ puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
6
+ end
7
+
8
+ Bundler::GemHelper.install_tasks
9
+
10
+ require 'rake/testtask'
11
+
12
+ Rake::TestTask.new("test") do |test|
13
+ test.libs.push "lib"
14
+ test.libs.push "test"
15
+ test.test_files = FileList['test/**/*_test.rb']
16
+ test.warning = true
17
+ test.verbose = true
18
+ end
19
+
20
+ task :default => :test
@@ -0,0 +1 @@
1
+ require 'intercom-rails/railtie' if defined? Rails
@@ -0,0 +1,9 @@
1
+ require 'intercom-rails/script_tag_helper'
2
+
3
+ module IntercomRails
4
+ class Railtie < Rails::Railtie
5
+ initializer "intercom_on_rails.script_tag_helper.rb" do |app|
6
+ ActionView::Base.send :include, ScriptTagHelper
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,64 @@
1
+ require "active_support/json"
2
+
3
+ module IntercomRails
4
+ # Helper methods for generating Intercom javascript script tags.
5
+ module ScriptTagHelper
6
+ # @param user_details [Hash] a customizable hash of user details
7
+ # @param widget_options [Hash] an optional hash for widget customisation
8
+ # @option user_details [String] :app_id Your application id (get it here)
9
+ # @option user_details [String] :user_id unique id of this user within your application
10
+ # @option user_details [String] :email email address for this user
11
+ # @option user_details [String] :name the users name, _optional_ but useful for identify people in the Intercom App.
12
+ # @option user_details [Hash] :custom_data custom attributes you'd like saved for this user on Intercom. See
13
+ # @option widget_options [String] :activator a css selector for an element which when clicked should show the Intercom widget
14
+ # @return [String] Intercom script tag
15
+ # @example basic example
16
+ # <%= intercom_script_tag({ :app_id => "your-app-id",
17
+ # :user_id => current_user.id,
18
+ # :email => current_user.email,
19
+ # :custom_data => { :plan => current_user.plan.name },
20
+ # :name => current_user.name }
21
+ # ) %>
22
+ # @example with widget activator for launching then widget when an element matching the css selector '#Intercom' is clicked.
23
+ # <%= intercom_script_tag({ :app_id => "your-app-id",
24
+ # :user_id => current_user.id,
25
+ # :email => current_user.email,
26
+ # :custom_data => { :plan => current_user.plan.name },
27
+ # :name => current_user.name }
28
+ # {:activator => "#Intercom"}
29
+ # ) %>
30
+ def intercom_script_tag(user_details, widget_options={})
31
+ intercom_settings = user_details.merge({:widget => widget_options})
32
+ intercom_settings_with_dates_as_timestamps = convert_dates_to_unix_timestamps(intercom_settings)
33
+ intercom_script = <<-INTERCOM_SCRIPT
34
+ <script id="IntercomSettingsScriptTag">
35
+ var intercomSettings = #{ActiveSupport::JSON.encode(intercom_settings_with_dates_as_timestamps)};
36
+ </script>
37
+ <script>
38
+ (function() {
39
+ function async_load() {
40
+ var s = document.createElement('script');
41
+ s.type = 'text/javascript'; s.async = true;
42
+ s.src = 'https://api.intercom.io/api/js/library.js';
43
+ var x = document.getElementsByTagName('script')[0];
44
+ x.parentNode.insertBefore(s, x);
45
+ }
46
+ if (window.attachEvent) {
47
+ window.attachEvent('onload', async_load);
48
+ } else {
49
+ window.addEventListener('load', async_load, false);
50
+ }
51
+ })();
52
+ </script>
53
+ INTERCOM_SCRIPT
54
+ intercom_script.respond_to?(:html_safe) ? intercom_script.html_safe : intercom_script
55
+ end
56
+
57
+ private
58
+ def convert_dates_to_unix_timestamps(object)
59
+ return Hash[object.map { |k, v| [k, convert_dates_to_unix_timestamps(v)] }] if object.is_a?(Hash)
60
+ return object.strftime('%s').to_i if object.respond_to?(:strftime)
61
+ object
62
+ end
63
+ end
64
+ end
@@ -0,0 +1,3 @@
1
+ module IntercomRails
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,16 @@
1
+ require "active_support/core_ext/string/output_safety"
2
+ require 'intercom-rails/script_tag_helper'
3
+ require 'minitest/autorun'
4
+
5
+ class IntercomRailsTest < MiniTest::Unit::TestCase
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
+ end
metadata ADDED
@@ -0,0 +1,94 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: intercom-rails
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Ben McRedmond
9
+ - Ciaran Lee
10
+ - Darragh Curran
11
+ autorequire:
12
+ bindir: bin
13
+ cert_chain: []
14
+ date: 2012-03-16 00:00:00.000000000Z
15
+ dependencies:
16
+ - !ruby/object:Gem::Dependency
17
+ name: activesupport
18
+ requirement: &70275093835120 !ruby/object:Gem::Requirement
19
+ none: false
20
+ requirements:
21
+ - - ! '>'
22
+ - !ruby/object:Gem::Version
23
+ version: '3.0'
24
+ type: :runtime
25
+ prerelease: false
26
+ version_requirements: *70275093835120
27
+ - !ruby/object:Gem::Dependency
28
+ name: minitest
29
+ requirement: &70275093834480 !ruby/object:Gem::Requirement
30
+ none: false
31
+ requirements:
32
+ - - ! '>='
33
+ - !ruby/object:Gem::Version
34
+ version: '0'
35
+ type: :development
36
+ prerelease: false
37
+ version_requirements: *70275093834480
38
+ - !ruby/object:Gem::Dependency
39
+ name: rake
40
+ requirement: &70275093833620 !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ type: :development
47
+ prerelease: false
48
+ version_requirements: *70275093833620
49
+ description: Intercom (https://www.intercom.io) is a customer relationship management
50
+ and messaging tool for web app owners. This library makes it easier to use the correct
51
+ javascript tracking code in your rails applications.
52
+ email:
53
+ - ben@intercom.io
54
+ - ciaran@intercom.io
55
+ - darragh@intercom.io
56
+ executables: []
57
+ extensions: []
58
+ extra_rdoc_files: []
59
+ files:
60
+ - lib/intercom-rails/railtie.rb
61
+ - lib/intercom-rails/script_tag_helper.rb
62
+ - lib/intercom-rails/version.rb
63
+ - lib/intercom-rails.rb
64
+ - MIT-LICENSE
65
+ - Rakefile
66
+ - README.rdoc
67
+ - test/intercom-rails/script_tag_helper_test.rb
68
+ homepage: http://www.intercom.io
69
+ licenses: []
70
+ post_install_message:
71
+ rdoc_options: []
72
+ require_paths:
73
+ - lib
74
+ required_ruby_version: !ruby/object:Gem::Requirement
75
+ none: false
76
+ requirements:
77
+ - - ! '>='
78
+ - !ruby/object:Gem::Version
79
+ version: '0'
80
+ required_rubygems_version: !ruby/object:Gem::Requirement
81
+ none: false
82
+ requirements:
83
+ - - ! '>='
84
+ - !ruby/object:Gem::Version
85
+ version: '0'
86
+ requirements: []
87
+ rubyforge_project: intercom-rails
88
+ rubygems_version: 1.8.10
89
+ signing_key:
90
+ specification_version: 3
91
+ summary: Rails helper for emitting javascript script tags for Intercom
92
+ test_files:
93
+ - test/intercom-rails/script_tag_helper_test.rb
94
+ has_rdoc: