Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make Codeclimate happier with Rollbar::Middlware::Js #520

Merged
merged 1 commit into from
Sep 7, 2016
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 32 additions & 34 deletions lib/rollbar/middleware/js.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

module Rollbar
module Middleware
# Middleware to inject the rollbar.js snippet into a 200 html response
class Js
attr_reader :app
attr_reader :config
Expand All @@ -18,38 +19,24 @@ def initialize(app, config)
def call(env)
result = app.call(env)

_call(env, result)
end

private

def _call(env, result)
return result unless should_add_js?(env, result[0], result[1])
begin
return result unless add_js?(env, result[0], result[1])

if response_string = add_js(env, result[2])
env[JS_IS_INJECTED_KEY] = true
response = ::Rack::Response.new(response_string, result[0], result[1])

response.finish
else
response_string = add_js(env, result[2])
build_response(env, result, response_string)
rescue => e
Rollbar.log_error("[Rollbar] Rollbar.js could not be added because #{e} exception")
result
end
rescue => e
Rollbar.log_error("[Rollbar] Rollbar.js could not be added because #{e} exception")
result
end

def enabled?
!!config[:enabled]
end

def should_add_js?(env, status, headers)
enabled? &&
status == 200 &&
!env[JS_IS_INJECTED_KEY] &&
html?(headers) &&
!attachment?(headers) &&
!streaming?(env)
def add_js?(env, status, headers)
enabled? && status == 200 && !env[JS_IS_INJECTED_KEY] &&
html?(headers) && !attachment?(headers) && !streaming?(env)
end

def html?(headers)
Expand All @@ -75,28 +62,39 @@ def add_js(env, response)
head_open_end = find_end_of_head_open(body)
return nil unless head_open_end

if head_open_end
body = body[0..head_open_end] <<
config_js_tag(env) <<
snippet_js_tag(env) <<
body[head_open_end + 1..-1]
end

body
build_body_with_js(env, body, head_open_end)
rescue => e
Rollbar.log_error("[Rollbar] Rollbar.js could not be added because #{e} exception")
nil
end

def build_response(env, app_result, response_string)
return result unless response_string

env[JS_IS_INJECTED_KEY] = true
response = ::Rack::Response.new(response_string, app_result[0],
app_result[1])

response.finish
end

def build_body_with_js(env, body, head_open_end)
return body unless head_open_end

body[0..head_open_end] << config_js_tag(env) << snippet_js_tag(env) <<
body[head_open_end + 1..-1]
end

def find_end_of_head_open(body)
head_open = body.index(/<head\W/)
body.index('>', head_open) if head_open
end

def join_body(response)
source = nil
response.each { |fragment| source ? (source << fragment.to_s) : (source = fragment.to_s)}
source
response.to_enum.reduce('') do |acc, fragment|
acc << fragment.to_s
acc
end
end

def close_old_response(response)
Expand Down