Class: Rack::FaradayInspector::Middleware

Inherits:
Object
  • Object
show all
Defined in:
lib/rack/faraday_inspector/middleware.rb

Instance Method Summary collapse

Constructor Details

#initialize(app) ⇒ Middleware

Returns a new instance of Middleware.



4
5
6
# File 'lib/rack/faraday_inspector/middleware.rb', line 4

def initialize(app)
  @app = app
end

Instance Method Details

#call(env) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/rack/faraday_inspector/middleware.rb', line 42

def call(env)
  status, headers, response = @app.call(env)

  if headers['Content-Type'].to_s.include? 'text/html'
    new_response = []
    response.each do |body|
      partition = body.rpartition('</body>')
      partition[0] += content.to_s
      new_response << partition.join
    end

    # Update the content-length
    headers['Content-Length'] = new_response.inject(0) do |len, body|
      len += body.bytesize
    end.to_s

    [status, headers, new_response]
  else
    [status, headers, response]
  end
ensure
  clear_requests
end

#clear_requestsObject



38
39
40
# File 'lib/rack/faraday_inspector/middleware.rb', line 38

def clear_requests
  Thread.current[:faraday_requests] = nil
end

#contentObject



33
34
35
36
# File 'lib/rack/faraday_inspector/middleware.rb', line 33

def content
  render_css
  ERB.new(template).result(binding)
end

#css_templateObject



16
17
18
# File 'lib/rack/faraday_inspector/middleware.rb', line 16

def css_template
  ::File.open(css_template_path).read
end

#css_template_pathObject



12
13
14
# File 'lib/rack/faraday_inspector/middleware.rb', line 12

def css_template_path
  template_path('sass', 'styles.scss')
end

#render_cssObject



20
21
22
23
24
25
26
27
# File 'lib/rack/faraday_inspector/middleware.rb', line 20

def render_css
  @css = Sass::Engine.new(css_template,{
    cache: true,
    syntax: :scss,
    style: :compressed,
    filename: css_template_path,
  }).render
end

#templateObject



29
30
31
# File 'lib/rack/faraday_inspector/middleware.rb', line 29

def template
  ::File.open(template_path('html', 'inspector.html.erb')).read
end

#template_path(*paths) ⇒ Object



8
9
10
# File 'lib/rack/faraday_inspector/middleware.rb', line 8

def template_path(*paths)
  ::File.expand_path(::File.join(::File.dirname(__FILE__), '..', '..', '..', 'assets', *paths))
end