Class: Cloudfuji::Middleware

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

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of Middleware.



7
8
9
# File 'lib/cloudfuji/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/cloudfuji/middleware.rb', line 11

def call(env)
  if Cloudfuji::Platform.on_cloudfuji? and Cloudfuji::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 _cloudfuji_app = '#{Cloudfuji::Platform.name}';
          var _cloudfuji_claimed = #{Cloudfuji::Platform.claimed?.to_s};
          var _cloudfuji_metrics_token = '#{Cloudfuji::Platform.metrics_token}';
          (function() {
            var cloudfuji = document.createElement('script'); cloudfuji.type = 'text/javascript'; cloudfuji.async = true;
            cloudfuji.src = '#{Cloudfuji::Platform.cloudfuji_js_source}?t=#{::Cloudfuji::VERSION.gsub('.', '')}';
            var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(cloudfuji, s);
          })();
        </script>     
      </body>
    STR
    )

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