planbcd-rails 0.0.3 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 5c0d0646acb96f02b50d69e52b19e9d8fba08ec4
4
- data.tar.gz: b57d6f8be129bbb7a858698f36ae0867cddb8287
3
+ metadata.gz: 52e1e49c64b8c5ea9135d640353e26c118f1080b
4
+ data.tar.gz: 3c7550383de391e858b48396511c8cae6d83f739
5
5
  SHA512:
6
- metadata.gz: 3f086b7daee187846c4cfe6c410b3edd650172ed0b3a498166988e5422834beb683e2188786f5086b33fc8fd9db502c4cfc9bd72f9cc4f718d728b0fedd3c3f3
7
- data.tar.gz: 5c5d42f3aa70326e1c007269707e3adb1248ae089af4d5fd6469a49864f254c09c3c81580d81aa899c298ca4d120388037e7e1468658e1a041cc4c204503da01
6
+ metadata.gz: bcf0fa96c6df01ebe1a74cf9fd0d5b7ea9da5d934af5dfa0fc5971c8e107bc94cc25f9c17c6cababc1cdd8da8a303cb177a817926ca049aba4bb6f3c5e90141c
7
+ data.tar.gz: 52f69b371fdcb75c88aedb5774c2d22169d64896d9955ca4f47ad91d9c012b20af9fae2fb59aad3021aaeade2f933a14c084c0788a8ff0e97f29b0be0530e09e
data/README.md CHANGED
@@ -25,7 +25,7 @@ bundle exec rails generate planbcd:install
25
25
 
26
26
  ## Configuration
27
27
 
28
- ### `js`
28
+ ### `js_url`
29
29
 
30
30
  JS URL which you can find on PlanBCD offer page
31
31
 
@@ -1,7 +1,7 @@
1
1
  module PlanBCD
2
2
  module Rails
3
3
  class Configuration
4
- attr_accessor :js, :auto_insert
4
+ attr_accessor :js_url, :auto_insert
5
5
  end
6
6
  end
7
7
  end
@@ -31,7 +31,7 @@ module PlanBCD
31
31
  response.each do |r|
32
32
  body.concat r.to_s
33
33
  end
34
- [body.sub(/(<head[^>]*>)/, "\\1#{pbcd_init}")]
34
+ [body.sub(/(<head[^>]*>)/i, "\\1#{pbcd_init}")]
35
35
  end
36
36
  end
37
37
  end
@@ -5,8 +5,13 @@ require 'planbcd/rails/middleware'
5
5
  module PlanBCD
6
6
  module Rails
7
7
  class Railtie < ::Rails::Railtie
8
- initializer "planbcd-rails" do |app|
9
- ActionView::Base.send :include, ViewHelpers
8
+ initializer "planbcd-rails:action_view" do |app|
9
+ ActiveSupport.on_load :action_view do
10
+ include ViewHelpers
11
+ end
12
+ end
13
+
14
+ initializer "planbcd-rails:middleware" do |app|
10
15
  app.middleware.use Middleware
11
16
  end
12
17
  end
@@ -1,5 +1,5 @@
1
1
  module PlanBCD
2
2
  module Rails
3
- VERSION = '0.0.3'
3
+ VERSION = '0.1.0'
4
4
  end
5
5
  end
@@ -4,19 +4,21 @@ require 'active_support/core_ext/object/blank'
4
4
  module PlanBCD
5
5
  module Rails
6
6
  module ViewHelpers
7
+ include ERB::Util
8
+
7
9
  def pbcd_init
8
- unless js = PlanBCD::Rails.configuration.js.presence
10
+ unless js_url = PlanBCD::Rails.configuration.js_url.presence
9
11
  raise PlanBCD::Rails::RuntimeError, "PlanBCD Javascript has not been set."
10
12
  end
11
13
 
12
- if js.respond_to?(:call)
13
- js = js.call(controller)
14
+ if js_url.respond_to?(:call)
15
+ js_url = js_url.call(controller)
14
16
  else
15
- js.gsub!(%r|^https?://|, '//')
17
+ js_url.gsub!(%r|^https?://|, '//')
16
18
  end
17
19
 
18
20
  s = '<script type="text/javascript">'
19
- s += %Q~(function(){!function(a,b,c,d,e){return d="http"+("https:"===c?"s":"")+":"+d,a.planBCDObject=e,a[e]||(a[e]=function(){var b;return((b=a[e]).q||(b.q=[])).push(arguments),a[e].l=1*new Date}),b.write(unescape('%3Cscript type="text/javascript" src="'+d+'"%3E%3C/script%3E'))}(window,document,document.location.protocol,"#{js}","pbcd")}).call(this);~
21
+ s += %Q~(function(){!function(a,b,c,d,e){return d="http"+("https:"===c?"s":"")+":"+d,a.planBCDObject=e,a[e]||(a[e]=function(){var b;return((b=a[e]).q||(b.q=[])).push(arguments),a[e].l=1*new Date}),b.write(unescape('%3Cscript type="text/javascript" src="'+d+'"%3E%3C/script%3E'))}(window,document,document.location.protocol,"#{html_escape(js_url)}","pbcd")}).call(this);~
20
22
  s += '</script>'
21
23
 
22
24
  s.html_safe
@@ -2,11 +2,11 @@ require 'spec_helper'
2
2
 
3
3
  describe PlanBCD::Rails::Middleware do
4
4
  let(:app) { double call: [200, {'Content-Type' => 'text/html; charset=utf-8'}, ['<html><head>something</head><body></body></html>']] }
5
- let(:js) { '//planb.cd/test.js' }
5
+ let(:js_url) { '//planb.cd/test.js' }
6
6
  subject { PlanBCD::Rails::Middleware.new(app) }
7
7
  before do
8
8
  PlanBCD::Rails.configure do |c|
9
- c.js = js
9
+ c.js_url = js_url
10
10
  c.auto_insert = true
11
11
  end
12
12
  end
@@ -46,7 +46,7 @@ describe PlanBCD::Rails::Middleware do
46
46
  end
47
47
  end
48
48
  context "no js" do
49
- let(:js) { nil }
49
+ let(:js_url) { nil }
50
50
  it "does not raise error but write the log" do
51
51
  expect($stderr).to receive(:write).with(/^Failed to insert planbcd tag: /)
52
52
  expect {
@@ -4,24 +4,24 @@ describe PlanBCD::Rails::ViewHelpers do
4
4
  include PlanBCD::Rails::ViewHelpers
5
5
 
6
6
  describe "#pbcd_init" do
7
- let(:js) { '//planb.cd/test.js' }
7
+ let(:js_url) { '//planb.cd/test.js' }
8
8
  before do
9
9
  PlanBCD::Rails.configure do |c|
10
- c.js = js
10
+ c.js_url = js_url
11
11
  end
12
12
  end
13
13
  it "generate planbcd tag" do
14
14
  expect(pbcd_init).to eq(%Q~<script type=\"text/javascript\">(function(){!function(a,b,c,d,e){return d=\"http\"+(\"https:\"===c?\"s\":\"\")+\":\"+d,a.planBCDObject=e,a[e]||(a[e]=function(){var b;return((b=a[e]).q||(b.q=[])).push(arguments),a[e].l=1*new Date}),b.write(unescape('%3Cscript type=\"text/javascript\" src=\"'+d+'\"%3E%3C/script%3E'))}(window,document,document.location.protocol,\"//planb.cd/test.js\",\"pbcd\")}).call(this);</script>~)
15
15
  end
16
- context "js has scheme" do
17
- let(:js) { 'http://planb.cd/test.js' }
16
+ context "js_url has scheme" do
17
+ let(:js_url) { 'http://planb.cd/test.js' }
18
18
  it "generate planbcd tag without scheme" do
19
19
  expect(pbcd_init).to eq(%Q~<script type=\"text/javascript\">(function(){!function(a,b,c,d,e){return d=\"http\"+(\"https:\"===c?\"s\":\"\")+\":\"+d,a.planBCDObject=e,a[e]||(a[e]=function(){var b;return((b=a[e]).q||(b.q=[])).push(arguments),a[e].l=1*new Date}),b.write(unescape('%3Cscript type=\"text/javascript\" src=\"'+d+'\"%3E%3C/script%3E'))}(window,document,document.location.protocol,\"//planb.cd/test.js\",\"pbcd\")}).call(this);</script>~)
20
20
  end
21
21
  end
22
- context "js is proc" do
22
+ context "js_url is proc" do
23
23
  let(:a_controller) { double }
24
- let(:js) { proc { |controller| '//planb.cd/proc.js' } }
24
+ let(:js_url) { proc { |controller| '//planb.cd/proc.js' } }
25
25
  before do
26
26
  allow(self).to receive(:controller).and_return(a_controller)
27
27
  end
@@ -29,16 +29,16 @@ describe PlanBCD::Rails::ViewHelpers do
29
29
  expect(pbcd_init).to eq(%Q~<script type=\"text/javascript\">(function(){!function(a,b,c,d,e){return d=\"http\"+(\"https:\"===c?\"s\":\"\")+\":\"+d,a.planBCDObject=e,a[e]||(a[e]=function(){var b;return((b=a[e]).q||(b.q=[])).push(arguments),a[e].l=1*new Date}),b.write(unescape('%3Cscript type=\"text/javascript\" src=\"'+d+'\"%3E%3C/script%3E'))}(window,document,document.location.protocol,\"//planb.cd/proc.js\",\"pbcd\")}).call(this);</script>~)
30
30
  end
31
31
  end
32
- context "js is nil" do
33
- let(:js) { nil }
32
+ context "js_url is nil" do
33
+ let(:js_url) { nil }
34
34
  it "raises error" do
35
35
  expect {
36
36
  pbcd_init
37
37
  }.to raise_error(PlanBCD::Rails::RuntimeError)
38
38
  end
39
39
  end
40
- context "js is empty" do
41
- let(:js) { '' }
40
+ context "js_url is empty" do
41
+ let(:js_url) { '' }
42
42
  it "raises error" do
43
43
  expect {
44
44
  pbcd_init
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: planbcd-rails
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.3
4
+ version: 0.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Daisuke Taniwaki