rack-livereload 0.1.0 → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/README.md CHANGED
@@ -3,6 +3,10 @@ No need for browser extensions anymore! Just plug it in your middleware stack an
3
3
 
4
4
  Use this with [guard-livereload](http://github.com/guard/guard-livereload) for maximum fun!
5
5
 
6
+ ## Install
7
+
8
+ `gem install rack-livereload`
9
+
6
10
  ## Using in...
7
11
 
8
12
  ### Rails
@@ -37,11 +41,20 @@ use Rack::LiveReload, :min_delay => 500, ...
37
41
 
38
42
  ## How it works
39
43
 
40
- The necessary `script` tag to bring in a vendored copy of [livereload.js](https://github.com/livereload/livereload-js) is
44
+ The necessary `script` tag to bring in a copy of [livereload.js](https://github.com/livereload/livereload-js) is
41
45
  injected right before the closing `head` tag in any `text/html` pages that come through. The `script` tag is built in
42
46
  such a way that the `HTTP_HOST` is used as the LiveReload host, so you can connect from external machines (say, to
43
47
  `mycomputer:3000` instead of `localhost:3000`) and as long as the LiveReload port is accessible from the external machine,
44
48
  you'll connect and be LiveReloading away!
45
49
 
50
+ ### Which LiveReload script does it use?
51
+
52
+ * If you've got a LiveReload watcher running on the same machine as the app that responds
53
+ to `http://localhost:35729/livereload.js`, that gets used, with the hostname being changed when
54
+ injected into the HTML page.
55
+ * If you don't, the copy vendored with rack-livereload is used.
56
+ * You can force the use of either one (and save on the cost of checking to see if that file
57
+ is available) with the middleware option `:source => :vendored` or `:source => :livereload`.
58
+
46
59
  As usual, super-alpha!
47
60
 
@@ -1,6 +1,6 @@
1
1
  require "rack/livereload"
2
2
 
3
3
  class Rack::LiveReload
4
- VERSION = '0.1.0'
4
+ VERSION = '0.2.0'
5
5
  end
6
6
 
@@ -1,6 +1,7 @@
1
1
  module Rack
2
2
  class LiveReload
3
3
  LIVERELOAD_JS_PATH = '/__rack/livereload.js'
4
+ LIVERELOAD_LOCAL_URI = 'http://localhost:35729/livereload.js'
4
5
 
5
6
  attr_reader :app
6
7
 
@@ -9,6 +10,31 @@ module Rack
9
10
  @options = options
10
11
  end
11
12
 
13
+ def use_vendored?
14
+ return @use_vendored if @use_vendored
15
+
16
+ if @options[:source]
17
+ @use_vendored = (@options[:source] == :vendored)
18
+ else
19
+ require 'net/http'
20
+ require 'uri'
21
+
22
+ uri = URI.parse(LIVERELOAD_LOCAL_URI)
23
+
24
+ http = Net::HTTP.new(uri.host, uri.port)
25
+ http.read_timeout = 1
26
+
27
+ begin
28
+ http.send_request('GET', uri.path)
29
+ @use_vendored = false
30
+ rescue Timeout::Error, Errno::ECONNREFUSED
31
+ @use_vendored = true
32
+ end
33
+ end
34
+
35
+ @use_vendored
36
+ end
37
+
12
38
  def call(env)
13
39
  if env['PATH_INFO'] == LIVERELOAD_JS_PATH
14
40
  deliver_file(::File.expand_path('../../../js/livereload.js', __FILE__))
@@ -21,12 +47,14 @@ module Rack
21
47
 
22
48
  body.each do |line|
23
49
  if !headers['X-Rack-LiveReload'] && line['</head>']
24
- src = LIVERELOAD_JS_PATH.dup
25
- if @options[:host]
26
- src << "?host=#{@options[:host]}"
50
+ host_to_use = @options[:host] || env['HTTP_HOST'].gsub(%r{:.*}, '')
51
+
52
+ if use_vendored?
53
+ src = LIVERELOAD_JS_PATH.dup + "?host=#{host_to_use}"
27
54
  else
28
- src << "?host=#{env['HTTP_HOST'].gsub(%r{:.*}, '')}" if env['HTTP_HOST']
55
+ src = LIVERELOAD_LOCAL_URI.dup.gsub('localhost', host_to_use) + '?'
29
56
  end
57
+
30
58
  src << "&mindelay=#{@options[:min_delay]}" if @options[:min_delay]
31
59
  src << "&maxdelay=#{@options[:max_delay]}" if @options[:max_delay]
32
60
  src << "&port=#{@options[:port]}" if @options[:port]
@@ -28,6 +28,7 @@ Gem::Specification.new do |s|
28
28
  s.add_development_dependency "mocha"
29
29
  s.add_development_dependency "guard"
30
30
  s.add_development_dependency "guard-rspec"
31
+ s.add_development_dependency "webmock"
31
32
 
32
33
  s.add_runtime_dependency "rack"
33
34
  end
@@ -1,7 +1,7 @@
1
1
  require 'spec_helper'
2
2
 
3
3
  describe Rack::LiveReload do
4
- let(:middleware) { described_class.new(app) }
4
+ let(:middleware) { described_class.new(app, options) }
5
5
  let(:app) { stub }
6
6
 
7
7
  subject { middleware }
@@ -9,6 +9,37 @@ describe Rack::LiveReload do
9
9
  its(:app) { should == app }
10
10
 
11
11
  let(:env) { {} }
12
+ let(:options) { {} }
13
+
14
+ describe described_class::LIVERELOAD_LOCAL_URI do
15
+ context 'does not exist' do
16
+ before do
17
+ stub_request(:any, 'localhost:35729/livereload.js').to_timeout
18
+ end
19
+
20
+ it { should use_vendored }
21
+ end
22
+
23
+ context 'exists' do
24
+ before do
25
+ stub_request(:any, 'localhost:35729/livereload.js')
26
+ end
27
+
28
+ it { should_not use_vendored }
29
+ end
30
+
31
+ context 'specify vendored' do
32
+ let(:options) { { :source => :vendored } }
33
+
34
+ it { should use_vendored }
35
+ end
36
+
37
+ context 'specify LR' do
38
+ let(:options) { { :source => :livereload } }
39
+
40
+ it { should_not use_vendored }
41
+ end
42
+ end
12
43
 
13
44
  context 'not text/html' do
14
45
  let(:ret) { [ 200, { 'Content-Type' => 'image/png' }, [ '<head></head>' ] ] }
@@ -25,6 +56,7 @@ describe Rack::LiveReload do
25
56
  context 'text/html' do
26
57
  before do
27
58
  app.stubs(:call).with(env).returns([ 200, { 'Content-Type' => 'text/html', 'Content-Length' => 0 }, [ '<head></head>' ] ])
59
+ middleware.stubs(:use_vendored?).returns(true)
28
60
  end
29
61
 
30
62
  let(:host) { 'host' }
@@ -34,13 +66,26 @@ describe Rack::LiveReload do
34
66
  let(:body) { ret.last.join }
35
67
  let(:length) { ret[1]['Content-Length'] }
36
68
 
37
- it 'should add the livereload js script tag' do
38
- body.should include("script")
39
- body.should include(described_class::LIVERELOAD_JS_PATH)
69
+ context 'vendored' do
70
+ it 'should add the vendored livereload js script tag' do
71
+ body.should include("script")
72
+ body.should include(described_class::LIVERELOAD_JS_PATH)
73
+
74
+ length.should == body.length.to_s
40
75
 
41
- length.should == body.length.to_s
76
+ described_class::LIVERELOAD_JS_PATH.should_not include(host)
77
+ end
78
+ end
42
79
 
43
- described_class::LIVERELOAD_JS_PATH.should_not include(host)
80
+ context 'not vendored' do
81
+ before do
82
+ middleware.stubs(:use_vendored?).returns(false)
83
+ end
84
+
85
+ it 'should add the LR livereload js script tag' do
86
+ body.should include("script")
87
+ body.should include(described_class::LIVERELOAD_LOCAL_URI.gsub('localhost', 'host'))
88
+ end
44
89
  end
45
90
 
46
91
  context 'set options' do
@@ -1,7 +1,16 @@
1
1
  require 'mocha'
2
+ require 'webmock/rspec'
3
+
2
4
  require 'rack-livereload'
3
5
 
4
6
  RSpec.configure do |c|
5
7
  c.mock_with :mocha
6
8
  end
7
9
 
10
+ module RSpec::Matchers
11
+ define :use_vendored do
12
+ match do |subject|
13
+ subject.use_vendored?
14
+ end
15
+ end
16
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rack-livereload
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,11 +9,11 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2011-11-07 00:00:00.000000000Z
12
+ date: 2011-11-14 00:00:00.000000000Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rspec
16
- requirement: &2153313940 !ruby/object:Gem::Requirement
16
+ requirement: &2164927100 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ! '>='
@@ -21,10 +21,10 @@ dependencies:
21
21
  version: '0'
22
22
  type: :development
23
23
  prerelease: false
24
- version_requirements: *2153313940
24
+ version_requirements: *2164927100
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: httparty
27
- requirement: &2153313340 !ruby/object:Gem::Requirement
27
+ requirement: &2164926440 !ruby/object:Gem::Requirement
28
28
  none: false
29
29
  requirements:
30
30
  - - ! '>='
@@ -32,10 +32,10 @@ dependencies:
32
32
  version: '0'
33
33
  type: :development
34
34
  prerelease: false
35
- version_requirements: *2153313340
35
+ version_requirements: *2164926440
36
36
  - !ruby/object:Gem::Dependency
37
37
  name: sinatra
38
- requirement: &2153312520 !ruby/object:Gem::Requirement
38
+ requirement: &2164926000 !ruby/object:Gem::Requirement
39
39
  none: false
40
40
  requirements:
41
41
  - - ! '>='
@@ -43,10 +43,10 @@ dependencies:
43
43
  version: '0'
44
44
  type: :development
45
45
  prerelease: false
46
- version_requirements: *2153312520
46
+ version_requirements: *2164926000
47
47
  - !ruby/object:Gem::Dependency
48
48
  name: shotgun
49
- requirement: &2153311420 !ruby/object:Gem::Requirement
49
+ requirement: &2164925420 !ruby/object:Gem::Requirement
50
50
  none: false
51
51
  requirements:
52
52
  - - ! '>='
@@ -54,10 +54,10 @@ dependencies:
54
54
  version: '0'
55
55
  type: :development
56
56
  prerelease: false
57
- version_requirements: *2153311420
57
+ version_requirements: *2164925420
58
58
  - !ruby/object:Gem::Dependency
59
59
  name: thin
60
- requirement: &2153309720 !ruby/object:Gem::Requirement
60
+ requirement: &2164924720 !ruby/object:Gem::Requirement
61
61
  none: false
62
62
  requirements:
63
63
  - - ! '>='
@@ -65,10 +65,10 @@ dependencies:
65
65
  version: '0'
66
66
  type: :development
67
67
  prerelease: false
68
- version_requirements: *2153309720
68
+ version_requirements: *2164924720
69
69
  - !ruby/object:Gem::Dependency
70
70
  name: rake
71
- requirement: &2153299320 !ruby/object:Gem::Requirement
71
+ requirement: &2164924060 !ruby/object:Gem::Requirement
72
72
  none: false
73
73
  requirements:
74
74
  - - ! '>='
@@ -76,10 +76,10 @@ dependencies:
76
76
  version: '0'
77
77
  type: :development
78
78
  prerelease: false
79
- version_requirements: *2153299320
79
+ version_requirements: *2164924060
80
80
  - !ruby/object:Gem::Dependency
81
81
  name: mocha
82
- requirement: &2153298900 !ruby/object:Gem::Requirement
82
+ requirement: &2164923580 !ruby/object:Gem::Requirement
83
83
  none: false
84
84
  requirements:
85
85
  - - ! '>='
@@ -87,10 +87,10 @@ dependencies:
87
87
  version: '0'
88
88
  type: :development
89
89
  prerelease: false
90
- version_requirements: *2153298900
90
+ version_requirements: *2164923580
91
91
  - !ruby/object:Gem::Dependency
92
92
  name: guard
93
- requirement: &2153298480 !ruby/object:Gem::Requirement
93
+ requirement: &2164923160 !ruby/object:Gem::Requirement
94
94
  none: false
95
95
  requirements:
96
96
  - - ! '>='
@@ -98,10 +98,10 @@ dependencies:
98
98
  version: '0'
99
99
  type: :development
100
100
  prerelease: false
101
- version_requirements: *2153298480
101
+ version_requirements: *2164923160
102
102
  - !ruby/object:Gem::Dependency
103
103
  name: guard-rspec
104
- requirement: &2153298000 !ruby/object:Gem::Requirement
104
+ requirement: &2164922740 !ruby/object:Gem::Requirement
105
105
  none: false
106
106
  requirements:
107
107
  - - ! '>='
@@ -109,10 +109,21 @@ dependencies:
109
109
  version: '0'
110
110
  type: :development
111
111
  prerelease: false
112
- version_requirements: *2153298000
112
+ version_requirements: *2164922740
113
+ - !ruby/object:Gem::Dependency
114
+ name: webmock
115
+ requirement: &2164922300 !ruby/object:Gem::Requirement
116
+ none: false
117
+ requirements:
118
+ - - ! '>='
119
+ - !ruby/object:Gem::Version
120
+ version: '0'
121
+ type: :development
122
+ prerelease: false
123
+ version_requirements: *2164922300
113
124
  - !ruby/object:Gem::Dependency
114
125
  name: rack
115
- requirement: &2153297500 !ruby/object:Gem::Requirement
126
+ requirement: &2164921880 !ruby/object:Gem::Requirement
116
127
  none: false
117
128
  requirements:
118
129
  - - ! '>='
@@ -120,7 +131,7 @@ dependencies:
120
131
  version: '0'
121
132
  type: :runtime
122
133
  prerelease: false
123
- version_requirements: *2153297500
134
+ version_requirements: *2164921880
124
135
  description: Insert LiveReload into your app easily as Rack middleware
125
136
  email:
126
137
  - john@coswellproductions.com