class Rack::NoAnimations

Constants

CONTENT_LENGTH
DISABLE_ANIMATIONS_SNIPPET
SNIPPET_LENGTH
TEXT_HTML

Public Class Methods

new(app) click to toggle source
# File lib/rack/no_animations.rb, line 30
def initialize(app)
  @app = app
end

Public Instance Methods

call(env) click to toggle source
# File lib/rack/no_animations.rb, line 34
def call(env)
  status, headers, body = env = @app.call(env)

  return env unless is_html?(headers)

  response(status, headers, body)
end

Protected Instance Methods

disable_animations(response, body = '') click to toggle source
# File lib/rack/no_animations.rb, line 66
def disable_animations(response, body = '')
  response.each do |s|
    body << s.to_s# read the whole body
  end

  if (body_index = body.rindex('</body>'.freeze))
    body.insert(body_index, DISABLE_ANIMATIONS_SNIPPET)
  end

  [ body_index, [ body ] ]
end
is_html?(headers) click to toggle source
# File lib/rack/no_animations.rb, line 52
def is_html?(headers)
  headers['Content-Type'.freeze] =~ TEXT_HTML
end
response(status, headers, body) click to toggle source
# File lib/rack/no_animations.rb, line 44
def response(status, headers, body)
  changed, body = disable_animations(body)

  update_length(headers) if changed

  [status, headers, body]
end
update_length(headers) click to toggle source
# File lib/rack/no_animations.rb, line 56
def update_length(headers)
  content_length = headers[CONTENT_LENGTH]
  length = content_length.to_i

  if length.to_s == content_length
    headers[CONTENT_LENGTH] = (length + SNIPPET_LENGTH).to_s
  end
end