Class: ViewInspect::Middleware
- Inherits:
-
Object
- Object
- ViewInspect::Middleware
- Defined in:
- lib/view_inspect/rails/middleware.rb
Constant Summary collapse
- HEAD_REGEXP =
/(<head.*?>)/- HTML_CONTENT_TYPE_REGEXP =
/text\/html|application\/xhtml\+xml/
Instance Method Summary collapse
- #add_library_exclude_list(body) ⇒ Object
- #call(env) ⇒ Object
- #get_inline_script ⇒ Object
- #get_insert_position(body) ⇒ Object
- #get_script ⇒ Object
-
#initialize(app) ⇒ Middleware
constructor
A new instance of Middleware.
- #insert_view_inspect_script(body) ⇒ Object
Constructor Details
#initialize(app) ⇒ Middleware
Returns a new instance of Middleware.
6 7 8 |
# File 'lib/view_inspect/rails/middleware.rb', line 6 def initialize(app) @app = app end |
Instance Method Details
#add_library_exclude_list(body) ⇒ Object
29 30 31 32 33 34 35 |
# File 'lib/view_inspect/rails/middleware.rb', line 29 def add_library_exclude_list(body) index = body.index(HEAD_REGEXP) old_head = $1 new_head = old_head.dup new_head.insert("<head".length, " data-orig-exclude-list='#{ViewInspect.library_exclude_list.join(",")}' ") body.sub(old_head,new_head) end |
#call(env) ⇒ Object
10 11 12 13 14 15 16 17 18 19 20 |
# File 'lib/view_inspect/rails/middleware.rb', line 10 def call(env) status, headers, response = @app.call(env) if headers["Content-Type"] =~ HTML_CONTENT_TYPE_REGEXP && response.body.to_s =~ HEAD_REGEXP body = insert_view_inspect_script(response.body) headers["Content-Length"] = body.length.to_s response = [body] end [status, headers, response] end |
#get_inline_script ⇒ Object
48 49 50 51 52 53 54 55 56 |
# File 'lib/view_inspect/rails/middleware.rb', line 48 def get_inline_script " <script type=\"text/javascript\" language=\"javascript\" charset=\"utf-8\">\n //<![CDATA[\n \#{get_script}\n //]]>\n </script>\n CODE\nend\n" |
#get_insert_position(body) ⇒ Object
37 38 39 40 41 |
# File 'lib/view_inspect/rails/middleware.rb', line 37 def get_insert_position(body) index = body.index(HEAD_REGEXP) match = $1 return index + match.length end |
#get_script ⇒ Object
43 44 45 46 |
# File 'lib/view_inspect/rails/middleware.rb', line 43 def get_script filepath = File.("javascript_handler.js","#{File.dirname(__FILE__)}/../handlers") File.read(filepath) end |
#insert_view_inspect_script(body) ⇒ Object
22 23 24 25 26 27 |
# File 'lib/view_inspect/rails/middleware.rb', line 22 def insert_view_inspect_script(body) body = add_library_exclude_list(body) index = get_insert_position(body) body.insert(index, get_inline_script) body end |