Class: Rack::LiveReload::BodyProcessor

Inherits:
Object
  • Object
show all
Defined in:
lib/rack/livereload/body_processor.rb

Constant Summary collapse

LIVERELOAD_JS_PATH =
'/__rack/livereload.js'
HEAD_TAG_REGEX =
/<head>|<head[^(er)][^<]*>/
LIVERELOAD_PORT =
35729

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(body, options) ⇒ BodyProcessor

Returns a new instance of BodyProcessor.



20
21
22
23
24
25
# File 'lib/rack/livereload/body_processor.rb', line 20

def initialize(body, options)
  @body, @options = body, options
  @options[:live_reload_port] ||= LIVERELOAD_PORT

  @processed = false
end

Instance Attribute Details

#content_lengthObject (readonly)

Returns the value of attribute content_length.



10
11
12
# File 'lib/rack/livereload/body_processor.rb', line 10

def content_length
  @content_length
end

#livereload_addedObject (readonly)

Returns the value of attribute livereload_added.



10
11
12
# File 'lib/rack/livereload/body_processor.rb', line 10

def livereload_added
  @livereload_added
end

#new_bodyObject (readonly)

Returns the value of attribute new_body.



10
11
12
# File 'lib/rack/livereload/body_processor.rb', line 10

def new_body
  @new_body
end

Instance Method Details

#app_rootObject



88
89
90
# File 'lib/rack/livereload/body_processor.rb', line 88

def app_root
  ENV['RAILS_RELATIVE_URL_ROOT'] || ''
end

#force_swf?Boolean

Returns:

  • (Boolean)


27
28
29
# File 'lib/rack/livereload/body_processor.rb', line 27

def force_swf?
  @options[:force_swf]
end

#host_to_useObject



92
93
94
# File 'lib/rack/livereload/body_processor.rb', line 92

def host_to_use
  (@options[:host] || @env['HTTP_HOST'] || 'localhost').gsub(%r{:.*}, '')
end

#livereload_local_uriObject



16
17
18
# File 'lib/rack/livereload/body_processor.rb', line 16

def livereload_local_uri
  "#{protocol}://localhost:#{@options[:live_reload_port]}/livereload.js"
end

#livereload_sourceObject



100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/rack/livereload/body_processor.rb', line 100

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

#process!(env) ⇒ Object



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/rack/livereload/body_processor.rb', line 67

def process!(env)
  @env = env
  @body.close if @body.respond_to?(:close)

  @new_body = [] ; @body.each { |line| @new_body << line.to_s }

  @content_length = 0
  @livereload_added = false

  @new_body.each do |line|
    if !@livereload_added && line['<head']
      line.gsub!(HEAD_TAG_REGEX) { |match| %{#{match}#{template.result(binding)}} }

      @livereload_added = true
    end

    @content_length += line.bytesize
    @processed = true
  end
end

#processed?Boolean

Returns:

  • (Boolean)


63
64
65
# File 'lib/rack/livereload/body_processor.rb', line 63

def processed?
  @processed
end

#protocolObject



12
13
14
# File 'lib/rack/livereload/body_processor.rb', line 12

def protocol
  @options[:protocol] || "http"
end

#templateObject



96
97
98
# File 'lib/rack/livereload/body_processor.rb', line 96

def template
  ERB.new(::File.read(::File.expand_path('../../../../skel/livereload.html.erb', __FILE__)))
end

#use_vendored?Boolean

Returns:

  • (Boolean)


35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/rack/livereload/body_processor.rb', line 35

def use_vendored?
  return @use_vendored if @use_vendored

  if @options[:source]
    @use_vendored = (@options[:source] == :vendored)
  else
    require 'net/http'
    require 'uri'

    uri = URI.parse(livereload_local_uri)

    http = Net::HTTP.new(uri.host, uri.port)
    http.read_timeout = 1

    begin
      http.send_request('GET', uri.path)
      @use_vendored = false
    rescue ::Timeout::Error, Errno::ECONNREFUSED, EOFError, IOError
      @use_vendored = true
    rescue => e
      $stderr.puts e.inspect
      raise e
    end
  end

  @use_vendored
end

#with_swf?Boolean

Returns:

  • (Boolean)


31
32
33
# File 'lib/rack/livereload/body_processor.rb', line 31

def with_swf?
  !@options[:no_swf]
end