a little more code shuffling to clean up the body processor class a bit

This commit is contained in:
John Bintz 2013-05-06 09:36:15 -04:00
parent 13e09428f3
commit 176ce40003
2 changed files with 28 additions and 17 deletions

View File

@ -61,6 +61,7 @@ module Rack
end
def process!(env)
@env = env
@body.close if @body.respond_to?(:close)
@new_body = [] ; @body.each { |line| @new_body << line.to_s }
@ -70,22 +71,6 @@ module Rack
@new_body.each do |line|
if !@livereload_added && line['<head']
host_to_use = (@options[:host] || env['HTTP_HOST'] || 'localhost').gsub(%r{:.*}, '')
app_root = ENV['RAILS_RELATIVE_URL_ROOT'] || ''
if use_vendored?
src = "#{app_root}#{LIVERELOAD_JS_PATH.dup}?host=#{host_to_use}"
else
src = livereload_local_uri.dup.gsub('localhost', host_to_use) + '?'
end
src << "&amp;mindelay=#{@options[:min_delay]}" if @options[:min_delay]
src << "&amp;maxdelay=#{@options[:max_delay]}" if @options[:max_delay]
src << "&amp;port=#{@options[:port]}" if @options[:port]
template = ERB.new(::File.read(::File.expand_path('../../../../skel/livereload.html.erb', __FILE__)))
line.gsub!(HEAD_TAG_REGEX) { |match| %{#{match}#{template.result(binding)}} }
@livereload_added = true
@ -95,6 +80,32 @@ module Rack
@processed = true
end
end
def app_root
ENV['RAILS_RELATIVE_URL_ROOT'] || ''
end
def host_to_use
(@options[:host] || @env['HTTP_HOST'] || 'localhost').gsub(%r{:.*}, '')
end
def template
ERB.new(::File.read(::File.expand_path('../../../../skel/livereload.html.erb', __FILE__)))
end
def livereload_source
if use_vendored?
src = "#{app_root}#{LIVERELOAD_JS_PATH.dup}?host=#{host_to_use}"
else
src = livereload_local_uri.dup.gsub('localhost', host_to_use) + '?'
end
src << "&amp;mindelay=#{@options[:min_delay]}" if @options[:min_delay]
src << "&amp;maxdelay=#{@options[:max_delay]}" if @options[:max_delay]
src << "&amp;port=#{@options[:port]}" if @options[:port]
src
end
end
end
end

View File

@ -11,5 +11,5 @@
<script type="text/javascript">
RACK_LIVERELOAD_PORT = <%= @options[:live_reload_port] %>;
</script>
<script type="text/javascript" src="<%= src %>"></script>
<script type="text/javascript" src="<%= livereload_source %>"></script>