Class: Bushido::Middleware

Inherits:
Object
  • Object
show all
Includes:
Rack::Utils
Defined in:
lib/bushido/middleware.rb

Instance Method Summary collapse

Constructor Details

#initialize(app, opts = {}) ⇒ Middleware

Returns a new instance of Middleware.



7
8
9
# File 'lib/bushido/middleware.rb', line 7

def initialize(app, opts = {})
  @app = app
end

Instance Method Details

#call(env) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/bushido/middleware.rb', line 11

def call(env)
  if Bushido::Platform.on_bushido? and Bushido::Bar.in_bar_display_path?(env)
    status, headers, response = @app.call(env)

    content = ""
    response.each { |part| content += part }

    # "claiming" bar + stats ?
    content.gsub!(/<\/body>/i, <<-STR
        <script type="text/javascript">
          var _bushido_app = '#{Bushido::Platform.name}';
          var _bushido_claimed = #{Bushido::Platform.claimed?.to_s};
          var _bushido_metrics_token = '#{Bushido::Platform.metrics_token}';
          (function() {
            var bushido = document.createElement('script'); bushido.type = 'text/javascript'; bushido.async = true;
            bushido.src = '#{Bushido::Platform.bushido_js_source}?t=#{::Bushido::VERSION.gsub('.', '')}';
            var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(bushido, s);
          })();
        </script>     
      </body>
    STR
    )

    headers['content-length'] = bytesize(content).to_s
    [status, headers, [content]]
  else
    @app.call(env)
  end
end