Class: Slimmer::App

Inherits:
Object
  • Object
show all
Defined in:
lib/slimmer/app.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(app, *args) ⇒ App

Returns a new instance of App.



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/slimmer/app.rb', line 7

def initialize(app, *args)
  options = args.first || {}
  @app = app

  logger = options[:logger] || NullLogger.instance
  self.logger = logger
  if logger&.level&.zero? && !(options[:enable_debugging])
    self.logger = logger.dup
    self.logger.level = 1 # info
  end

  if options.key? :template_path
    raise "Template path should not be used. Use asset_host instead."
  end

  unless options[:asset_host]
    options[:asset_host] = Plek.find("static")
  end

  @skin = Skin.new options.merge(logger: self.logger)
end

Instance Attribute Details

#loggerObject

Returns the value of attribute logger.



5
6
7
# File 'lib/slimmer/app.rb', line 5

def logger
  @logger
end

Instance Method Details

#call(env) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/slimmer/app.rb', line 29

def call(env)
  logger.debug "Slimmer: capturing response"
  status, headers, body = @app.call(env)

  if response_can_be_rewritten?(status, headers)
    response = Rack::Response.new(body, status, headers)

    unless skip_slimmer?(env, response)
      status, headers, body = rewrite_response(env, response)
    end
  end

  [status, strip_slimmer_headers(headers), body]
end

#content_length(rewritten_body) ⇒ Object



73
74
75
76
77
# File 'lib/slimmer/app.rb', line 73

def content_length(rewritten_body)
  size = 0
  rewritten_body.each { |part| size += part.bytesize }
  size.to_s
end

#in_development?Boolean

Returns:

  • (Boolean)


52
53
54
# File 'lib/slimmer/app.rb', line 52

def in_development?
  ENV["RAILS_ENV"] == "development"
end

#response_can_be_rewritten?(status, headers) ⇒ Boolean

Returns:

  • (Boolean)


44
45
46
# File 'lib/slimmer/app.rb', line 44

def response_can_be_rewritten?(status, headers)
  Rack::Headers.new.merge(headers)["Content-Type"] =~ /text\/html/ && ![301, 302, 304].include?(status)
end

#rewrite_response(env, response) ⇒ Object



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/slimmer/app.rb', line 79

def rewrite_response(env, response)
  request = Rack::Request.new(env)
  return response.finish unless response.status == 200

  # Store the request id so it can be passed on with any template requests
  GovukRequestId.value = env["HTTP_GOVUK_REQUEST_ID"]

  rewritten_body = @skin.success request, response, s(response.body)
  rewritten_body = [rewritten_body] unless rewritten_body.respond_to?(:each)

  response.body = rewritten_body
  response.headers["Content-Length"] = content_length(response.body)

  response.finish
end

#s(body) ⇒ Object



65
66
67
68
69
70
71
# File 'lib/slimmer/app.rb', line 65

def s(body)
  return body.to_s unless body.respond_to?(:each)

  b = ""
  body.each { |a| b << a }
  b
end

#skip_slimmer?(env, response) ⇒ Boolean

Returns:

  • (Boolean)


48
49
50
# File 'lib/slimmer/app.rb', line 48

def skip_slimmer?(env, response)
  (in_development? && skip_slimmer_param?(env)) || skip_slimmer_header?(response)
end

#skip_slimmer_header?(response) ⇒ Boolean

Returns:

  • (Boolean)


61
62
63
# File 'lib/slimmer/app.rb', line 61

def skip_slimmer_header?(response)
  response.headers.key?(Headers::SKIP_HEADER)
end

#skip_slimmer_param?(env) ⇒ Boolean

Returns:

  • (Boolean)


56
57
58
59
# File 'lib/slimmer/app.rb', line 56

def skip_slimmer_param?(env)
  skip = Rack::Request.new(env).params["skip_slimmer"]
  skip && skip.to_i.positive?
end

#strip_slimmer_headers(headers) ⇒ Object



95
96
97
# File 'lib/slimmer/app.rb', line 95

def strip_slimmer_headers(headers)
  headers.reject { |k, _v| k =~ /\A#{Headers::HEADER_PREFIX}/i }
end