intercom-rails 0.0.6 → 0.0.7

Sign up to get free protection for your applications and to get access to all the features.
data/Rakefile CHANGED
@@ -1,4 +1,5 @@
1
1
  #!/usr/bin/env rake
2
+
2
3
  begin
3
4
  require 'bundler/setup'
4
5
  rescue LoadError
@@ -10,10 +11,11 @@ Bundler::GemHelper.install_tasks
10
11
  require 'rake/testtask'
11
12
 
12
13
  Rake::TestTask.new("test") do |test|
13
- test.libs.push "lib"
14
- test.libs.push "test"
14
+ test.libs << 'lib'
15
+ test.libs << 'test'
16
+
15
17
  test.test_files = FileList['test/**/*_test.rb']
16
- test.warning = true
18
+ test.warning = false
17
19
  test.verbose = true
18
20
  end
19
21
 
@@ -1 +1,12 @@
1
- require 'intercom-rails/railtie' if defined? Rails
1
+ require 'intercom-rails/script_tag_helper'
2
+ require 'intercom-rails/auto_include_filter'
3
+ require 'intercom-rails/config'
4
+ require 'intercom-rails/railtie' if defined? Rails
5
+
6
+ module IntercomRails
7
+
8
+ def self.config
9
+ block_given? ? yield(Config) : Config
10
+ end
11
+
12
+ end
@@ -0,0 +1,92 @@
1
+ module IntercomRails
2
+
3
+ class AutoIncludeFilter
4
+
5
+ include ScriptTagHelper
6
+ CLOSING_BODY_TAG = %r{</body>}
7
+
8
+ def self.filter(controller)
9
+ auto_include_filter = new(controller)
10
+ return unless auto_include_filter.include_javascript?
11
+
12
+ auto_include_filter.include_javascript!
13
+ end
14
+
15
+ attr_reader :controller
16
+
17
+ def initialize(kontroller)
18
+ @controller = kontroller
19
+ end
20
+
21
+ def include_javascript!
22
+ response.body = response.body.gsub(CLOSING_BODY_TAG, intercom_script_tag + '\\0')
23
+ end
24
+
25
+ def include_javascript?
26
+ !intercom_script_tag_called_manually? &&
27
+ html_content_type? &&
28
+ response_has_closing_body_tag? &&
29
+ intercom_app_id.present? &&
30
+ intercom_user_object.present?
31
+ end
32
+
33
+ private
34
+ def response
35
+ controller.response
36
+ end
37
+
38
+ def html_content_type?
39
+ response.content_type == 'text/html'
40
+ end
41
+
42
+ def response_has_closing_body_tag?
43
+ !!(response.body[CLOSING_BODY_TAG])
44
+ end
45
+
46
+ def intercom_script_tag_called_manually?
47
+ controller.instance_variable_get(SCRIPT_TAG_HELPER_CALLED_INSTANCE_VARIABLE)
48
+ end
49
+
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
+ def intercom_script_tag
79
+ user_details = {:app_id => intercom_app_id}
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)
88
+ end
89
+
90
+ end
91
+
92
+ end
@@ -0,0 +1,24 @@
1
+ module IntercomRails
2
+
3
+ module Config
4
+
5
+ def self.app_id=(value)
6
+ @app_id = value
7
+ end
8
+
9
+ def self.app_id
10
+ @app_id
11
+ end
12
+
13
+ def self.current_user=(value)
14
+ raise ArgumentError, "current_user should be a Proc" unless value.kind_of?(Proc)
15
+ @current_user = value
16
+ end
17
+
18
+ def self.current_user
19
+ @current_user
20
+ end
21
+
22
+ end
23
+
24
+ end
@@ -1,9 +1,11 @@
1
- require 'intercom-rails/script_tag_helper'
2
-
3
1
  module IntercomRails
4
2
  class Railtie < Rails::Railtie
5
3
  initializer "intercom_on_rails.script_tag_helper.rb" do |app|
6
4
  ActionView::Base.send :include, ScriptTagHelper
7
5
  end
6
+
7
+ initializer "intercom_on_rails.auto_include_filter.rb" do |app|
8
+ ActionController::Base.send :after_filter, AutoIncludeFilter
9
+ end
8
10
  end
9
- end
11
+ end
@@ -3,6 +3,8 @@ require "active_support/core_ext/hash/indifferent_access"
3
3
 
4
4
  module IntercomRails
5
5
  # Helper methods for generating Intercom javascript script tags.
6
+ SCRIPT_TAG_HELPER_CALLED_INSTANCE_VARIABLE = :@_intercom_script_tag_helper_called
7
+
6
8
  module ScriptTagHelper
7
9
  # @param user_details [Hash] a customizable hash of user details
8
10
  # @param options [Hash] an optional hash for secure mode and widget customisation
@@ -57,6 +59,8 @@ module IntercomRails
57
59
  })();
58
60
  </script>
59
61
  INTERCOM_SCRIPT
62
+
63
+ controller.instance_variable_set(IntercomRails::SCRIPT_TAG_HELPER_CALLED_INSTANCE_VARIABLE, true) if defined?(controller)
60
64
  intercom_script.respond_to?(:html_safe) ? intercom_script.html_safe : intercom_script
61
65
  end
62
66
 
@@ -1,3 +1,3 @@
1
1
  module IntercomRails
2
- VERSION = "0.0.6"
2
+ VERSION = "0.0.7"
3
3
  end
@@ -0,0 +1,18 @@
1
+ module Intercom
2
+ module Generators
3
+ class ConfigGenerator < ::Rails::Generators::Base
4
+
5
+ def self.source_root
6
+ File.dirname(__FILE__)
7
+ end
8
+
9
+ argument :app_id, :desc => "Your Intercom app-id, which can be found here: https://www.intercom.io/apps/api_keys"
10
+
11
+ def create_config_file
12
+ @app_id = app_id
13
+ template("intercom.rb.erb", "config/initializers/intercom.rb")
14
+ end
15
+
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,8 @@
1
+ IntercomRails.config do |config|
2
+ # Your Intercom app_id
3
+ config.app_id = "<%= @app_id %>"
4
+
5
+ # The method/variable that contains the logged in user in your controllers.
6
+ # If it is `current_user` or `@user`, then you can ignore this
7
+ # config.current_user = Proc.new { current_user }
8
+ end
@@ -0,0 +1,35 @@
1
+ require 'test_setup'
2
+
3
+ require 'pry'
4
+ require 'action_controller'
5
+ require 'action_controller/test_case'
6
+
7
+ def dummy_user(options = {})
8
+ user = Struct.new(:email, :name).new
9
+ user.email = options[:email] || 'ben@intercom.io'
10
+ user.name = options[:name] || 'Ben McRedmond'
11
+ user
12
+ end
13
+
14
+ TestRoutes = ActionDispatch::Routing::RouteSet.new
15
+ TestRoutes.draw do
16
+ get ':controller(/:action)'
17
+ end
18
+
19
+ class ActionController::Base
20
+
21
+ after_filter IntercomRails::AutoIncludeFilter
22
+
23
+ include TestRoutes.url_helpers
24
+ include TestRoutes.mounted_helpers
25
+
26
+ end
27
+
28
+ class ActionController::TestCase
29
+
30
+ def setup
31
+ super
32
+ @routes = TestRoutes
33
+ end
34
+
35
+ end
@@ -0,0 +1,102 @@
1
+ require 'action_controller_test_setup'
2
+
3
+ class TestController < ActionController::Base
4
+
5
+ def without_user
6
+ render :text => params[:body], :content_type => 'text/html'
7
+ end
8
+
9
+ def with_user_instance_variable
10
+ @user = dummy_user
11
+ render :text => params[:body], :content_type => 'text/html'
12
+ end
13
+
14
+ def with_unusable_user_instance_variable
15
+ @user = Object.new
16
+ render :text => params[:body], :content_type => 'text/html'
17
+ end
18
+
19
+ def with_current_user_method
20
+ render :text => params[:body], :content_type => 'text/html'
21
+ end
22
+
23
+ def with_admin_instance_variable
24
+ @admin = dummy_user(:email => 'eoghan@intercom.io', :name => 'Eoghan McCabe')
25
+ render :text => params[:body], :content_type => 'text/html'
26
+ end
27
+
28
+ def current_user
29
+ raise NameError if params[:action] != 'with_current_user_method'
30
+ dummy_user(:email => 'ciaran@intercom.io', :name => 'Ciaran Lee')
31
+ end
32
+
33
+ end
34
+
35
+ class AutoIncludeFilterTest < ActionController::TestCase
36
+
37
+ tests TestController
38
+
39
+ def setup
40
+ super
41
+ ENV['INTERCOM_APP_ID'] = 'my_app_id'
42
+ end
43
+
44
+ def test_no_user_present
45
+ get :without_user, :body => "<body>Hello world</body>"
46
+ assert_equal @response.body, "<body>Hello world</body>"
47
+ end
48
+
49
+ def test_user_present_with_no_body_tag
50
+ get :with_user_instance_variable, :body => "Hello world"
51
+ assert_equal @response.body, "Hello world"
52
+ end
53
+
54
+ def test_user_present_but_unusuable
55
+ get :with_unusable_user_instance_variable, :body => "Hello world"
56
+ assert_equal @response.body, "Hello world"
57
+ end
58
+
59
+ def test_user_instance_variable_present_with_body_tag
60
+ get :with_user_instance_variable, :body => "<body>Hello world</body>"
61
+
62
+ assert_includes @response.body, "<script>"
63
+ assert_includes @response.body, ENV['INTERCOM_APP_ID']
64
+ assert_includes @response.body, "ben@intercom.io"
65
+ assert_includes @response.body, "Ben McRedmond"
66
+ end
67
+
68
+ def test_current_user_method_present_with_body_tag
69
+ get :with_current_user_method, :body => "<body>Hello world</body>"
70
+
71
+ assert_includes @response.body, "<script>"
72
+ assert_includes @response.body, "ciaran@intercom.io"
73
+ assert_includes @response.body, "Ciaran Lee"
74
+ end
75
+
76
+ def test_setting_current_user_with_intercom_config
77
+ IntercomRails.config.current_user = Proc.new { @admin }
78
+
79
+ get :with_admin_instance_variable, :body => "<body>Hello world</body>"
80
+
81
+ assert_includes @response.body, "<script>"
82
+ assert_includes @response.body, "eoghan@intercom.io"
83
+ assert_includes @response.body, "Eoghan McCabe"
84
+ end
85
+
86
+ def test_no_app_id_present
87
+ ENV.delete('INTERCOM_APP_ID')
88
+ get :with_current_user_method, :body => "<body>Hello world</body>"
89
+
90
+ assert_equal @response.body, "<body>Hello world</body>"
91
+ end
92
+
93
+ def test_manual_script_tag_helper_call
94
+ fake_action_view = fake_action_view_class.new
95
+ fake_action_view.instance_variable_set(:@controller, @controller)
96
+ fake_action_view.intercom_script_tag({})
97
+
98
+ get :with_current_user_method, :body => "<body>Hello world</body>"
99
+ assert_equal @response.body, "<body>Hello world</body>"
100
+ end
101
+
102
+ end
@@ -0,0 +1,30 @@
1
+ require 'test_setup'
2
+
3
+ class ConfigTest < MiniTest::Unit::TestCase
4
+
5
+ def test_setting_app_id
6
+ IntercomRails.config.app_id = "1234"
7
+ assert_equal IntercomRails.config.app_id, "1234"
8
+ end
9
+
10
+ def test_setting_current_user
11
+ current_user = Proc.new { @blah }
12
+ IntercomRails.config.current_user = current_user
13
+ assert_equal IntercomRails.config.current_user, current_user
14
+ end
15
+
16
+ def test_setting_current_user_not_to_a_proc
17
+ assert_raises ArgumentError do
18
+ IntercomRails.config.current_user = 1
19
+ end
20
+ end
21
+
22
+ def test_configuring_intercom_with_block
23
+ IntercomRails.config do |config|
24
+ config.app_id = "4567"
25
+ end
26
+
27
+ assert_equal IntercomRails.config.app_id, "4567"
28
+ end
29
+
30
+ end
@@ -1,8 +1,8 @@
1
- require "active_support/core_ext/string/output_safety"
2
- require 'intercom-rails/script_tag_helper'
3
- require 'minitest/autorun'
1
+ require 'active_support/core_ext/string/output_safety'
2
+ require 'test_setup'
3
+
4
+ class ScriptTagHelperTest < MiniTest::Unit::TestCase
4
5
 
5
- class IntercomRailsTest < MiniTest::Unit::TestCase
6
6
  include IntercomRails::ScriptTagHelper
7
7
  def test_output_is_html_safe?
8
8
  assert_equal true, intercom_script_tag({}).html_safe?
@@ -27,4 +27,15 @@ class IntercomRailsTest < MiniTest::Unit::TestCase
27
27
  assert_match(/.user_hash.\s*:\s*"#{Digest::SHA1.hexdigest('abcdefgh' + '1234')}"/, intercom_script_tag({:user_id => 1234}, {:secret => 'abcdefgh'}))
28
28
  assert_match(/.user_hash.\s*:\s*"#{Digest::SHA1.hexdigest('abcdefgh' + '1234')}"/, intercom_script_tag({:user_id => 1234, :email => "ciaran@intercom.io"}, {:secret => 'abcdefgh'}))
29
29
  end
30
+
31
+ def test_sets_instance_variable
32
+ fake_action_view = fake_action_view_class.new
33
+ obj = Object.new
34
+
35
+ fake_action_view.instance_variable_set(:@controller, obj)
36
+
37
+ fake_action_view.intercom_script_tag({})
38
+ assert_equal obj.instance_variable_get(IntercomRails::SCRIPT_TAG_HELPER_CALLED_INSTANCE_VARIABLE), true
39
+ end
40
+
30
41
  end
@@ -0,0 +1,11 @@
1
+ require 'intercom-rails'
2
+ require 'minitest/autorun'
3
+
4
+ def fake_action_view_class
5
+ klass = Class.new(ActionView::Base)
6
+ klass.class_eval do
7
+ include IntercomRails::ScriptTagHelper
8
+ attr_reader :controller
9
+ end
10
+ klass
11
+ end
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.6
4
+ version: 0.0.7
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-08-28 00:00:00.000000000 Z
14
+ date: 2012-11-02 00:00:00.000000000 Z
15
15
  dependencies:
16
16
  - !ruby/object:Gem::Dependency
17
17
  name: activesupport
@@ -30,7 +30,7 @@ dependencies:
30
30
  - !ruby/object:Gem::Version
31
31
  version: '3.0'
32
32
  - !ruby/object:Gem::Dependency
33
- name: minitest
33
+ name: rake
34
34
  requirement: !ruby/object:Gem::Requirement
35
35
  none: false
36
36
  requirements:
@@ -46,7 +46,23 @@ dependencies:
46
46
  - !ruby/object:Gem::Version
47
47
  version: '0'
48
48
  - !ruby/object:Gem::Dependency
49
- name: rake
49
+ name: actionpack
50
+ requirement: !ruby/object:Gem::Requirement
51
+ none: false
52
+ requirements:
53
+ - - ! '>='
54
+ - !ruby/object:Gem::Version
55
+ version: '0'
56
+ type: :development
57
+ prerelease: false
58
+ version_requirements: !ruby/object:Gem::Requirement
59
+ none: false
60
+ requirements:
61
+ - - ! '>='
62
+ - !ruby/object:Gem::Version
63
+ version: '0'
64
+ - !ruby/object:Gem::Dependency
65
+ name: pry
50
66
  requirement: !ruby/object:Gem::Requirement
51
67
  none: false
52
68
  requirements:
@@ -72,15 +88,23 @@ executables: []
72
88
  extensions: []
73
89
  extra_rdoc_files: []
74
90
  files:
91
+ - lib/intercom-rails/auto_include_filter.rb
92
+ - lib/intercom-rails/config.rb
75
93
  - lib/intercom-rails/railtie.rb
76
94
  - lib/intercom-rails/script_tag_helper.rb
77
95
  - lib/intercom-rails/version.rb
78
96
  - lib/intercom-rails.rb
97
+ - lib/rails/generators/intercom/config/config_generator.rb
98
+ - lib/rails/generators/intercom/config/intercom.rb.erb
79
99
  - lib/rails/generators/intercom/install/install_generator.rb
80
100
  - MIT-LICENSE
81
101
  - Rakefile
82
102
  - README.rdoc
103
+ - test/action_controller_test_setup.rb
104
+ - test/intercom-rails/auto_include_filter_test.rb
105
+ - test/intercom-rails/config_test.rb
83
106
  - test/intercom-rails/script_tag_helper_test.rb
107
+ - test/test_setup.rb
84
108
  homepage: http://www.intercom.io
85
109
  licenses: []
86
110
  post_install_message:
@@ -95,7 +119,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
95
119
  version: '0'
96
120
  segments:
97
121
  - 0
98
- hash: -1146691025025650591
122
+ hash: -3817663782507976710
99
123
  required_rubygems_version: !ruby/object:Gem::Requirement
100
124
  none: false
101
125
  requirements:
@@ -104,7 +128,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
104
128
  version: '0'
105
129
  segments:
106
130
  - 0
107
- hash: -1146691025025650591
131
+ hash: -3817663782507976710
108
132
  requirements: []
109
133
  rubyforge_project: intercom-rails
110
134
  rubygems_version: 1.8.23
@@ -112,4 +136,8 @@ signing_key:
112
136
  specification_version: 3
113
137
  summary: Rails helper for emitting javascript script tags for Intercom
114
138
  test_files:
139
+ - test/action_controller_test_setup.rb
140
+ - test/intercom-rails/auto_include_filter_test.rb
141
+ - test/intercom-rails/config_test.rb
115
142
  - test/intercom-rails/script_tag_helper_test.rb
143
+ - test/test_setup.rb