2011-05-27 22:14:45 +00:00
|
|
|
require 'rack/utils'
|
|
|
|
|
|
|
|
module Locomotive
|
|
|
|
module Hosting
|
|
|
|
module Bushido
|
|
|
|
class Middleware
|
|
|
|
|
2011-05-27 22:39:48 +00:00
|
|
|
# BUSHIDO_JS_URL = 'http://localhost:4567/javascripts/bushido.js'
|
|
|
|
BUSHIDO_JS_URL = 'http://bushi.do/api/bushido.js'
|
2011-05-27 22:14:45 +00:00
|
|
|
|
|
|
|
include Rack::Utils
|
|
|
|
|
|
|
|
def initialize(app, opts = {})
|
|
|
|
@app = app
|
2011-06-01 21:47:06 +00:00
|
|
|
@bushido_app_name = ENV['BUSHIDO_APP']
|
|
|
|
@bushido_metrics_token = ENV['BUSHIDO_METRICS_TOKEN']
|
|
|
|
@bushido_claimed = ::Locomotive.bushido_app_claimed?
|
2011-05-27 22:14:45 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def call(env)
|
|
|
|
status, headers, response = @app.call(env)
|
|
|
|
|
2011-05-27 23:24:11 +00:00
|
|
|
content = ""
|
|
|
|
response.each { |part| content += part }
|
|
|
|
|
|
|
|
# "claiming" bar + stats ?
|
|
|
|
content.gsub!(/<\/body>/i, <<-STR
|
|
|
|
<script type="text/javascript">
|
|
|
|
var _bushido_app = '#{@bushido_app_name}';
|
|
|
|
var _bushido_claimed = #{@bushido_claimed.to_s};
|
2011-06-01 21:47:06 +00:00
|
|
|
var _bushido_metrics_token = '#{@bushido_metrics_token}';
|
2011-05-27 23:24:11 +00:00
|
|
|
(function() {
|
|
|
|
var bushido = document.createElement('script'); bushido.type = 'text/javascript'; bushido.async = true;
|
2011-06-01 19:09:09 +00:00
|
|
|
bushido.src = '#{BUSHIDO_JS_URL}?#{::Bushido::VERSION.gsub('.', '')}';
|
2011-05-27 23:24:11 +00:00
|
|
|
var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(bushido, s);
|
|
|
|
})();
|
2011-06-03 22:05:55 +00:00
|
|
|
</script>
|
2011-05-27 23:24:11 +00:00
|
|
|
</body>
|
|
|
|
STR
|
|
|
|
)
|
|
|
|
|
|
|
|
headers['content-length'] = bytesize(content).to_s
|
|
|
|
|
|
|
|
[status, headers, [content]]
|
2011-05-27 22:14:45 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|