Class: Rack::FaradayInspector::Middleware
- Inherits:
-
Object
- Object
- Rack::FaradayInspector::Middleware
- Defined in:
- lib/rack/faraday_inspector/middleware.rb
Instance Method Summary collapse
- #asset_path(*paths) ⇒ Object
- #call(env) ⇒ Object
- #clear_requests ⇒ Object
- #compile_css ⇒ Object
- #config ⇒ Object
- #content ⇒ Object
-
#initialize(app) ⇒ Middleware
constructor
A new instance of Middleware.
- #inject_inspector_into(response) ⇒ Object
- #read_compiled_css ⇒ Object
- #set_css ⇒ Object
- #set_javascript ⇒ Object
- #should_compile_css_at_runtime? ⇒ Boolean
- #should_inject_inspector?(headers) ⇒ Boolean
- #template ⇒ Object
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.(::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_requests ⇒ Object
51 52 53 |
# File 'lib/rack/faraday_inspector/middleware.rb', line 51 def clear_requests Thread.current[:faraday_requests] = nil end |
#compile_css ⇒ Object
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 |
#config ⇒ Object
8 9 10 |
# File 'lib/rack/faraday_inspector/middleware.rb', line 8 def config Rack::FaradayInspector.config end |
#content ⇒ Object
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_css ⇒ Object
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_css ⇒ Object
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_javascript ⇒ Object
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
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
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 |
#template ⇒ Object
41 42 43 |
# File 'lib/rack/faraday_inspector/middleware.rb', line 41 def template ::File.open(asset_path('html', 'inspector.html.erb')).read end |