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

#asset_path(*paths) ⇒ Object



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

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

#call(env) ⇒ Object



69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/rack/faraday_inspector/middleware.rb', line 69

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

  if should_inject_inspector?(headers)
    response = inject_inspector_into(response)
    headers['Content-Length'] = response.inject(0) do |len, body|
      len + body.bytesize
    end.to_s
  end

  [status, headers, response]
ensure
  clear_requests
end

#clear_requestsObject



51
52
53
# File 'lib/rack/faraday_inspector/middleware.rb', line 51

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

#compile_cssObject



16
17
18
19
20
21
22
23
# File 'lib/rack/faraday_inspector/middleware.rb', line 16

def compile_css
  path_to_sass = asset_path('sass', 'style.scss')
  @css = Sass::Engine.new(::File.open(path_to_sass).read,
                          cache: true,
                          syntax: :scss,
                          style: :compressed,
                          filename: path_to_sass).render
end

#configObject



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

def config
  Rack::FaradayInspector.config
end

#contentObject



45
46
47
48
49
# File 'lib/rack/faraday_inspector/middleware.rb', line 45

def content
  set_css
  set_javascript
  ERB.new(template).result(binding)
end

#inject_inspector_into(response) ⇒ Object



55
56
57
58
59
60
61
62
63
# File 'lib/rack/faraday_inspector/middleware.rb', line 55

def inject_inspector_into(response)
  new_response = []
  response.each do |body|
    partition = body.rpartition('</body>')
    partition[0] += content.to_s
    new_response << partition.join
  end
  new_response
end

#read_compiled_cssObject



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

def read_compiled_css
  ::File.open(asset_path('css', 'style.css')).read
end

#set_cssObject



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

def set_css
  @css = should_compile_css_at_runtime? ? compile_css : read_compiled_css
end

#set_javascriptObject



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

def set_javascript
  @javascript = ::File.open(asset_path('javascripts', 'inspector.js')).read
end

#should_compile_css_at_runtime?Boolean

Returns:

  • (Boolean)


25
26
27
# File 'lib/rack/faraday_inspector/middleware.rb', line 25

def should_compile_css_at_runtime?
  config.development_mode && defined?(Sass)
end

#should_inject_inspector?(headers) ⇒ Boolean

Returns:

  • (Boolean)


65
66
67
# File 'lib/rack/faraday_inspector/middleware.rb', line 65

def should_inject_inspector?(headers)
  config.enabled && headers['Content-Type'].to_s.include?('text/html')
end

#templateObject



41
42
43
# File 'lib/rack/faraday_inspector/middleware.rb', line 41

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