planbcd-rails 0.0.2 → 0.0.3
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.
- checksums.yaml +4 -4
- data/README.md +11 -3
- data/lib/generators/planbcd/templates/planbcd.rb +3 -0
- data/lib/planbcd/rails/configuration.rb +1 -1
- data/lib/planbcd/rails/middleware.rb +38 -0
- data/lib/planbcd/rails/railtie.rb +3 -1
- data/lib/planbcd/rails/version.rb +1 -1
- data/lib/planbcd/rails/view_helpers.rb +1 -1
- data/planbcd-rails.gemspec +1 -1
- data/spec/planbcd/rails/middleware_spec.rb +57 -0
- data/spec/planbcd/rails/railtie_spec.rb +6 -2
- metadata +6 -3
    
        checksums.yaml
    CHANGED
    
    | @@ -1,7 +1,7 @@ | |
| 1 1 | 
             
            ---
         | 
| 2 2 | 
             
            SHA1:
         | 
| 3 | 
            -
              metadata.gz:  | 
| 4 | 
            -
              data.tar.gz:  | 
| 3 | 
            +
              metadata.gz: 5c0d0646acb96f02b50d69e52b19e9d8fba08ec4
         | 
| 4 | 
            +
              data.tar.gz: b57d6f8be129bbb7a858698f36ae0867cddb8287
         | 
| 5 5 | 
             
            SHA512:
         | 
| 6 | 
            -
              metadata.gz:  | 
| 7 | 
            -
              data.tar.gz:  | 
| 6 | 
            +
              metadata.gz: 3f086b7daee187846c4cfe6c410b3edd650172ed0b3a498166988e5422834beb683e2188786f5086b33fc8fd9db502c4cfc9bd72f9cc4f718d728b0fedd3c3f3
         | 
| 7 | 
            +
              data.tar.gz: 5c5d42f3aa70326e1c007269707e3adb1248ae089af4d5fd6469a49864f254c09c3c81580d81aa899c298ca4d120388037e7e1468658e1a041cc4c204503da01
         | 
    
        data/README.md
    CHANGED
    
    | @@ -2,7 +2,9 @@ | |
| 2 2 |  | 
| 3 3 | 
             
            [](https://rubygems.org/gems/planbcd-rails) [](http://travis-ci.org/dtaniwaki/planbcd-rails) [](https://coveralls.io/r/dtaniwaki/planbcd-rails)
         | 
| 4 4 |  | 
| 5 | 
            -
            PlanBCD For Rails! So great.
         | 
| 5 | 
            +
            [PlanBCD](https://planb.cd/) For Rails! So great.
         | 
| 6 | 
            +
             | 
| 7 | 
            +
            This is NOT a official gem from [KAIZEN Platform Inc](http://kaizenplatform.in/).
         | 
| 6 8 |  | 
| 7 9 | 
             
            ## Installation
         | 
| 8 10 |  | 
| @@ -21,9 +23,15 @@ Then, initialize planbcd. | |
| 21 23 | 
             
            bundle exec rails generate planbcd:install
         | 
| 22 24 | 
             
            ```
         | 
| 23 25 |  | 
| 24 | 
            -
            ##  | 
| 26 | 
            +
            ## Configuration
         | 
| 27 | 
            +
             | 
| 28 | 
            +
            ### `js`
         | 
| 29 | 
            +
             | 
| 30 | 
            +
            JS URL which you can find on PlanBCD offer page
         | 
| 31 | 
            +
             | 
| 32 | 
            +
            ### `auto_insert` (Optional)
         | 
| 25 33 |  | 
| 26 | 
            -
             | 
| 34 | 
            +
            Set true if you want to insert the PlanBCD tag to all the pages generated by Rails
         | 
| 27 35 |  | 
| 28 36 | 
             
            ## Contributing
         | 
| 29 37 |  | 
| @@ -2,4 +2,7 @@ PlanBCD::Rails.configure do |c| | |
| 2 2 | 
             
              # Set your JS file found in your offer.
         | 
| 3 3 | 
             
              # c.js = "//localhost/abc/def.js"
         | 
| 4 4 | 
             
              # c.js = proc { |controller| controller.is_something? ? "//localhost/abc/def_1.js" : "//localhost/abc/def_2.js" }
         | 
| 5 | 
            +
             | 
| 6 | 
            +
              # Insert PlanBCD js code to any response for html request
         | 
| 7 | 
            +
              # c.auto_insert = true
         | 
| 5 8 | 
             
            end
         | 
| @@ -0,0 +1,38 @@ | |
| 1 | 
            +
            module PlanBCD
         | 
| 2 | 
            +
              module Rails
         | 
| 3 | 
            +
                class Middleware
         | 
| 4 | 
            +
                  include Rack::Utils
         | 
| 5 | 
            +
                  include ViewHelpers
         | 
| 6 | 
            +
             | 
| 7 | 
            +
                  def initialize(app)
         | 
| 8 | 
            +
                    @app = app
         | 
| 9 | 
            +
                  end
         | 
| 10 | 
            +
                  
         | 
| 11 | 
            +
                  def call(env)
         | 
| 12 | 
            +
                    status, headers, response = @app.call(env)
         | 
| 13 | 
            +
             | 
| 14 | 
            +
                    headers = HeaderHash.new(headers)
         | 
| 15 | 
            +
             | 
| 16 | 
            +
                    if headers['Content-Type'].to_s =~ %r{^text/html;}i && PlanBCD::Rails.configuration.auto_insert
         | 
| 17 | 
            +
                      begin
         | 
| 18 | 
            +
                        response = insert_js(response)
         | 
| 19 | 
            +
                      rescue PlanBCD::Rails::Exception => e
         | 
| 20 | 
            +
                        $stderr.write "Failed to insert planbcd tag: #{e.message}\n"
         | 
| 21 | 
            +
                      end
         | 
| 22 | 
            +
                    end
         | 
| 23 | 
            +
             | 
| 24 | 
            +
                    [status, headers, response]
         | 
| 25 | 
            +
                  end
         | 
| 26 | 
            +
             | 
| 27 | 
            +
                  private
         | 
| 28 | 
            +
             | 
| 29 | 
            +
                  def insert_js(response)
         | 
| 30 | 
            +
                    body = ""
         | 
| 31 | 
            +
                    response.each do |r|
         | 
| 32 | 
            +
                      body.concat r.to_s
         | 
| 33 | 
            +
                    end
         | 
| 34 | 
            +
                    [body.sub(/(<head[^>]*>)/, "\\1#{pbcd_init}")]
         | 
| 35 | 
            +
                  end
         | 
| 36 | 
            +
                end
         | 
| 37 | 
            +
              end
         | 
| 38 | 
            +
            end
         | 
| @@ -1,11 +1,13 @@ | |
| 1 1 | 
             
            require 'rails/railtie'
         | 
| 2 2 | 
             
            require 'planbcd/rails/view_helpers'
         | 
| 3 | 
            +
            require 'planbcd/rails/middleware'
         | 
| 3 4 |  | 
| 4 5 | 
             
            module PlanBCD
         | 
| 5 6 | 
             
              module Rails
         | 
| 6 7 | 
             
                class Railtie < ::Rails::Railtie
         | 
| 7 | 
            -
                  initializer "planbcd-rails" do
         | 
| 8 | 
            +
                  initializer "planbcd-rails" do |app|
         | 
| 8 9 | 
             
                    ActionView::Base.send :include, ViewHelpers
         | 
| 10 | 
            +
                    app.middleware.use Middleware
         | 
| 9 11 | 
             
                  end
         | 
| 10 12 | 
             
                end
         | 
| 11 13 | 
             
              end
         | 
| @@ -6,7 +6,7 @@ module PlanBCD | |
| 6 6 | 
             
                module ViewHelpers
         | 
| 7 7 | 
             
                  def pbcd_init
         | 
| 8 8 | 
             
                    unless js = PlanBCD::Rails.configuration.js.presence
         | 
| 9 | 
            -
                      raise PlanBCD::Rails::RuntimeError, "PlanBCD Javascript  | 
| 9 | 
            +
                      raise PlanBCD::Rails::RuntimeError, "PlanBCD Javascript has not been set."
         | 
| 10 10 | 
             
                    end
         | 
| 11 11 |  | 
| 12 12 | 
             
                    if js.respond_to?(:call)
         | 
    
        data/planbcd-rails.gemspec
    CHANGED
    
    | @@ -6,7 +6,7 @@ Gem::Specification.new do |gem| | |
| 6 6 | 
             
              gem.platform    = Gem::Platform::RUBY
         | 
| 7 7 | 
             
              gem.authors     = ["Daisuke Taniwaki"]
         | 
| 8 8 | 
             
              gem.email       = ["daisuketaniwaki@gmail.com"]
         | 
| 9 | 
            -
              gem.homepage    = " | 
| 9 | 
            +
              gem.homepage    = "https://github.com/dtaniwaki/planbcd-rails"
         | 
| 10 10 | 
             
              gem.summary     = "Use PlanBCD with Rails 3+"
         | 
| 11 11 | 
             
              gem.description = "This gem provides view helpers for PlanBCD."
         | 
| 12 12 | 
             
              gem.license     = "MIT"
         | 
| @@ -0,0 +1,57 @@ | |
| 1 | 
            +
            require 'spec_helper'
         | 
| 2 | 
            +
             | 
| 3 | 
            +
            describe PlanBCD::Rails::Middleware do
         | 
| 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' }
         | 
| 6 | 
            +
              subject { PlanBCD::Rails::Middleware.new(app) }
         | 
| 7 | 
            +
              before do
         | 
| 8 | 
            +
                PlanBCD::Rails.configure do |c|
         | 
| 9 | 
            +
                  c.js = js
         | 
| 10 | 
            +
                  c.auto_insert = true
         | 
| 11 | 
            +
                end
         | 
| 12 | 
            +
              end
         | 
| 13 | 
            +
             | 
| 14 | 
            +
              it "inserts planbcd tag" do
         | 
| 15 | 
            +
                status, headers, body = subject.call({})
         | 
| 16 | 
            +
                expect(status).to eq(200)
         | 
| 17 | 
            +
                expect(headers).to eq({'Content-Type' => 'text/html; charset=utf-8'})
         | 
| 18 | 
            +
                expect(body).to eq([%Q~<html><head><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>something</head><body></body></html>~])
         | 
| 19 | 
            +
              end
         | 
| 20 | 
            +
              context "head tag with something" do
         | 
| 21 | 
            +
                let(:app) { double call: [200, {'Content-Type' => 'text/html; charset=utf-8'}, ['<html><head abcdefg>something</head><body></body></html>']] }
         | 
| 22 | 
            +
                it "inserts planbcd tag" do
         | 
| 23 | 
            +
                  status, headers, body = subject.call({})
         | 
| 24 | 
            +
                  expect(status).to eq(200)
         | 
| 25 | 
            +
                  expect(headers).to eq({'Content-Type' => 'text/html; charset=utf-8'})
         | 
| 26 | 
            +
                  expect(body).to eq([%Q~<html><head abcdefg><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>something</head><body></body></html>~])
         | 
| 27 | 
            +
                end
         | 
| 28 | 
            +
              end
         | 
| 29 | 
            +
              context "no head tag in body" do
         | 
| 30 | 
            +
                let(:app) { double call: [200, {'Content-Type' => 'text/html; charset=utf-8'}, ['<html><body></body></html>']] }
         | 
| 31 | 
            +
                it "does not insert planbcd tag if the body does not have head tag" do
         | 
| 32 | 
            +
                  status, headers, body = subject.call({})
         | 
| 33 | 
            +
                  expect(status).to eq(200)
         | 
| 34 | 
            +
                  expect(headers).to eq({'Content-Type' => 'text/html; charset=utf-8'})
         | 
| 35 | 
            +
                  expect(body).to eq([%Q~<html><body></body></html>~])
         | 
| 36 | 
            +
                end
         | 
| 37 | 
            +
              end
         | 
| 38 | 
            +
              context "not html request" do
         | 
| 39 | 
            +
                let(:app) { double call: [200, {'Content-Type' => 'application/json;'}, ['{}']] }
         | 
| 40 | 
            +
                it "does not insert planbcd tag if the body does not have head tag" do
         | 
| 41 | 
            +
                  expect(subject).not_to receive(:insert_js)
         | 
| 42 | 
            +
                  status, headers, body = subject.call({})
         | 
| 43 | 
            +
                  expect(status).to eq(200)
         | 
| 44 | 
            +
                  expect(headers).to eq({'Content-Type' => 'application/json;'})
         | 
| 45 | 
            +
                  expect(body).to eq(['{}'])
         | 
| 46 | 
            +
                end
         | 
| 47 | 
            +
              end
         | 
| 48 | 
            +
              context "no js" do
         | 
| 49 | 
            +
                let(:js) { nil }
         | 
| 50 | 
            +
                it "does not raise error but write the log" do
         | 
| 51 | 
            +
                  expect($stderr).to receive(:write).with(/^Failed to insert planbcd tag: /)
         | 
| 52 | 
            +
                  expect {
         | 
| 53 | 
            +
                    subject.call({})
         | 
| 54 | 
            +
                  }.not_to raise_error
         | 
| 55 | 
            +
                end
         | 
| 56 | 
            +
              end
         | 
| 57 | 
            +
            end
         | 
| @@ -3,12 +3,16 @@ require 'action_view' | |
| 3 3 |  | 
| 4 4 | 
             
            describe PlanBCD::Rails::Railtie do
         | 
| 5 5 | 
             
              before :all do
         | 
| 6 | 
            -
                app = Class.new(Rails::Application)
         | 
| 7 | 
            -
                app.initialize!
         | 
| 6 | 
            +
                @app = Class.new(Rails::Application)
         | 
| 7 | 
            +
                @app.initialize!
         | 
| 8 8 | 
             
              end
         | 
| 9 9 | 
             
              subject { ActionView::Base.new }
         | 
| 10 10 |  | 
| 11 11 | 
             
              it "should have planbcd helpers" do
         | 
| 12 12 | 
             
                expect(subject).to respond_to(:pbcd_init)
         | 
| 13 13 | 
             
              end
         | 
| 14 | 
            +
             | 
| 15 | 
            +
              it "should add rack middleware" do
         | 
| 16 | 
            +
                expect(@app.middleware.middlewares).to include(PlanBCD::Rails::Middleware)
         | 
| 17 | 
            +
              end
         | 
| 14 18 | 
             
            end
         | 
    
        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. | 
| 4 | 
            +
              version: 0.0.3
         | 
| 5 5 | 
             
            platform: ruby
         | 
| 6 6 | 
             
            authors:
         | 
| 7 7 | 
             
            - Daisuke Taniwaki
         | 
| @@ -96,14 +96,16 @@ files: | |
| 96 96 | 
             
            - lib/planbcd/rails.rb
         | 
| 97 97 | 
             
            - lib/planbcd/rails/configuration.rb
         | 
| 98 98 | 
             
            - lib/planbcd/rails/error.rb
         | 
| 99 | 
            +
            - lib/planbcd/rails/middleware.rb
         | 
| 99 100 | 
             
            - lib/planbcd/rails/railtie.rb
         | 
| 100 101 | 
             
            - lib/planbcd/rails/version.rb
         | 
| 101 102 | 
             
            - lib/planbcd/rails/view_helpers.rb
         | 
| 102 103 | 
             
            - planbcd-rails.gemspec
         | 
| 104 | 
            +
            - spec/planbcd/rails/middleware_spec.rb
         | 
| 103 105 | 
             
            - spec/planbcd/rails/railtie_spec.rb
         | 
| 104 106 | 
             
            - spec/planbcd/rails/view_helpers_spec.rb
         | 
| 105 107 | 
             
            - spec/spec_helper.rb
         | 
| 106 | 
            -
            homepage:  | 
| 108 | 
            +
            homepage: https://github.com/dtaniwaki/planbcd-rails
         | 
| 107 109 | 
             
            licenses:
         | 
| 108 110 | 
             
            - MIT
         | 
| 109 111 | 
             
            metadata: {}
         | 
| @@ -123,11 +125,12 @@ required_rubygems_version: !ruby/object:Gem::Requirement | |
| 123 125 | 
             
                  version: '0'
         | 
| 124 126 | 
             
            requirements: []
         | 
| 125 127 | 
             
            rubyforge_project: 
         | 
| 126 | 
            -
            rubygems_version: 2. | 
| 128 | 
            +
            rubygems_version: 2.2.2
         | 
| 127 129 | 
             
            signing_key: 
         | 
| 128 130 | 
             
            specification_version: 4
         | 
| 129 131 | 
             
            summary: Use PlanBCD with Rails 3+
         | 
| 130 132 | 
             
            test_files:
         | 
| 133 | 
            +
            - spec/planbcd/rails/middleware_spec.rb
         | 
| 131 134 | 
             
            - spec/planbcd/rails/railtie_spec.rb
         | 
| 132 135 | 
             
            - spec/planbcd/rails/view_helpers_spec.rb
         | 
| 133 136 | 
             
            - spec/spec_helper.rb
         |