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( [^<]+)?>/
LIVERELOAD_PORT =
35729
LIVERELOAD_SCHEME =
"ws"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(body, options) ⇒ BodyProcessor

Returns a new instance of BodyProcessor.



22
23
24
25
26
27
28
# File 'lib/rack/livereload/body_processor.rb', line 22

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

  @processed = false
end

Instance Attribute Details

#content_lengthObject (readonly)

Returns the value of attribute content_length.



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

def content_length
  @content_length
end

#livereload_addedObject (readonly)

Returns the value of attribute livereload_added.



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

def livereload_added
  @livereload_added
end

#new_bodyObject (readonly)

Returns the value of attribute new_body.



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

def new_body
  @new_body
end

Instance Method Details

#app_rootObject



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

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

#force_swf?Boolean

Returns:

  • (Boolean)


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

def force_swf?
  @options[:force_swf]
end

#host_to_useObject



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

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

#livereload_local_uriObject



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

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

#livereload_sourceObject



103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/rack/livereload/body_processor.rb', line 103

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



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

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)


66
67
68
# File 'lib/rack/livereload/body_processor.rb', line 66

def processed?
  @processed
end

#protocolObject



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

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

#templateObject



99
100
101
# File 'lib/rack/livereload/body_processor.rb', line 99

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

#use_vendored?Boolean

Returns:

  • (Boolean)


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

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)


34
35
36
# File 'lib/rack/livereload/body_processor.rb', line 34

def with_swf?
  !@options[:no_swf]
end