class Rack::Unscripted
Constants
- VERSION
Attributes
warning_message[W]
Public Class Methods
new(app, warning_message = nil)
click to toggle source
# File lib/rack/unscripted.rb, line 3 def initialize(app, warning_message = nil) self.warning_message = warning_message if warning_message @app = app end
Public Instance Methods
call(env)
click to toggle source
# File lib/rack/unscripted.rb, line 8 def call(env) @status, @headers, @response = @app.call(env) if valid_content_type?(@headers['Content-Type']) && valid_status?(@status) @response.each do |response_line| insert_js(response_line) if response_line =~ head_regex insert_html(response_line) if response_line =~ body_regex end end [@status, @headers, @response] end
Private Instance Methods
add_to_content_length(number)
click to toggle source
Appends the given number to the current content length in the headers.
# File lib/rack/unscripted.rb, line 55 def add_to_content_length(number) if @headers['Content-Length'] @headers['Content-Length'] = (@headers['Content-Length'].to_i + number.to_i).to_s end end
body_regex()
click to toggle source
Regular Expression to find the opening body tag.
# File lib/rack/unscripted.rb, line 45 def body_regex /(<\s*body[^>]*>)/i end
head_regex()
click to toggle source
Regular expression to find the closing </head> tag in a document.
# File lib/rack/unscripted.rb, line 50 def head_regex /(<\s*\/head[^>]*>)/i end
inline_code()
click to toggle source
# File lib/rack/unscripted.rb, line 38 def inline_code <<-END <script type="text/javascript">document.write('<style>#rack-unscripted-no-javascript-warning{display:none;}</style>');</script> END end
insert_html(response_line)
click to toggle source
Inserts HTML of javascript warning after the opening body tag.
# File lib/rack/unscripted.rb, line 29 def insert_html(response_line) add_to_content_length(no_javascript_warning.length) response_line.sub!(body_regex, '\1'+ no_javascript_warning) end
insert_js(response_line)
click to toggle source
# File lib/rack/unscripted.rb, line 23 def insert_js(response_line) add_to_content_length(inline_code.length) response_line.sub!(head_regex, inline_code + '\1') end
no_javascript_warning()
click to toggle source
# File lib/rack/unscripted.rb, line 34 def no_javascript_warning "<div id='rack-unscripted-no-javascript-warning'>#{warning_message}</div>" end
valid_content_type?(content_type)
click to toggle source
Returns true
if the content type is text/html. No need to add this message to any other types of content.
# File lib/rack/unscripted.rb, line 63 def valid_content_type?(content_type) content_type.respond_to?(:include?) && content_type.include?("text/html") end
valid_status?(response_code)
click to toggle source
Returns true
if the HTTP response code is such that we should append this warning message.
# File lib/rack/unscripted.rb, line 68 def valid_status?(response_code) [200, 404].include? response_code.to_i end
warning_message()
click to toggle source
Attribute reader for the warning message. Sets a default value.
# File lib/rack/unscripted.rb, line 73 def warning_message @warning_message ||= 'Warning, this site requires javascript to function properly. Please enable it.' end