Skip to content

Commit

Permalink
Some cleanup & refactoring of #70
Browse files Browse the repository at this point in the history
  • Loading branch information
thibaudgg committed Apr 14, 2013
1 parent a364e1f commit e06e711
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 73 deletions.
4 changes: 2 additions & 2 deletions Guardfile
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
guard 'rspec', :version => 2, :cli => '-f doc' do
guard 'rspec', :cli => '-f doc' do
watch(%r{spec/.+_spec.rb})
watch(%r{lib/(.+)\.rb}) { |m| "spec/#{m[1]}_spec.rb" }
watch('spec/spec_helper.rb') { "spec" }
end
end
13 changes: 2 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,6 @@ $ guard init livereload

Use [rack-livereload](https://github.com/johnbintz/rack-livereload) or install [LiveReload Safari/Chrome extension](http://feedback.livereload.com/knowledgebase/articles/86242-how-do-i-install-and-use-the-browser-extensions-)

### Optional

To optimize communication with the LiveReload extension, install the yajl-ruby to increase JSON performance:

``` bash
$ gem install yajl-ruby
```

## Usage

Please read [Guard usage doc](http://github.com/guard/guard#readme) and [rack-livereload how it works readme section](https://github.com/johnbintz/rack-livereload#readme) or [LiveReload extension usage doc](http://github.com/mockko/livereload#readme)
Expand All @@ -65,19 +57,18 @@ end
LiveReload guard has 6 options that you can set like this:

``` ruby
guard 'livereload', :api_version => '1.4', :port => '35728' do
guard 'livereload' do
# ...
end
```

Available options:

``` ruby
:api_version => '1.4' # default '1.6'
:host => '127.3.3.1' # default '0.0.0.0'
:port => '12345' # default '35729'
:apply_js_live => false # default true
:apply_css_live => false # default true
:override_url => false # default false
:grace_period => 0.5 # default 0 (seconds)
```

Expand Down
13 changes: 7 additions & 6 deletions lib/guard/livereload.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@

module Guard
class LiveReload < Guard
attr_accessor :reactor
autoload :Reactor, 'guard/livereload/reactor'
require 'guard/livereload/websocket'
require 'guard/livereload/reactor'

attr_accessor :reactor, :options

# =================
# = Guard methods =
Expand All @@ -13,25 +15,24 @@ class LiveReload < Guard
def initialize(watchers = [], options = {})
super
@options = {
:api_version => '1.6',
:host => '0.0.0.0',
:port => '35729',
:apply_js_live => true,
:apply_css_live => true,
:override_url => false,
:grace_period => 0
}.update(options)
end

def start
@reactor = Reactor.new(@options)
@reactor = Reactor.new(options)
end

def stop
reactor.stop
end

def run_on_changes(paths)
sleep @options[:grace_period]
sleep options[:grace_period]
reactor.reload_browser(paths)
end

Expand Down
25 changes: 10 additions & 15 deletions lib/guard/livereload/reactor.rb
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
require 'multi_json'
require 'guard/livereload/http-ws'

module Guard
class LiveReload
class Reactor

attr_reader :thread, :web_sockets
attr_reader :web_sockets, :thread, :options

def initialize(options)
@web_sockets = []
Expand All @@ -21,15 +19,15 @@ def reload_browser(paths = [])
UI.info "Reloading browser: #{paths.join(' ')}"
paths.each do |path|
data = {
:command => 'reload',
:path => "#{Dir.pwd}/#{path}",
:liveCSS => @options[:apply_css_live]
:command => 'reload',
:path => "#{Dir.pwd}/#{path}",
:liveCSS => @options[:apply_css_live]
}
if @options[:overrideURL] && File.exist?(path)
if options[:override_url] && File.exist?(path)
data[:overrideURL] = '/' + path
end
UI.debug data
@web_sockets.each { |ws| ws.send(MultiJson.encode(data)) }
web_sockets.each { |ws| ws.send(MultiJson.encode(data)) }
end
end

Expand All @@ -39,16 +37,13 @@ def start_threaded_reactor(options)
Thread.new do
EventMachine.run do
UI.info "LiveReload is waiting for a browser to connect."
EventMachine.start_server(options[:host], options[:port], HTTP_Websocket, {}) do |ws|
EventMachine.start_server(options[:host], options[:port], WebSocket, {}) do |ws|
ws.onopen do
begin
UI.info "Browser connected."
ws.send MultiJson.encode({
:command => 'hello',
:protocols =>
[
'http://livereload.com/protocols/official-7',
],
:command => 'hello',
:protocols => ['http://livereload.com/protocols/official-7'],
:serverName => 'guard-livereload'
})
@web_sockets << ws
Expand All @@ -66,7 +61,7 @@ def start_threaded_reactor(options)
end

ws.onclose do
@web_sockets.delete ws
@web_sockets.delete(ws)
UI.info "Browser disconnected."
end
end
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
require 'eventmachine'
require 'em-websocket'
require "http/parser"

module Guard
class LiveReload
class HTTP_Websocket < EventMachine::WebSocket::Connection
class WebSocket < EventMachine::WebSocket::Connection
def dispatch data
parser = Http::Parser.new
parser << data
Expand All @@ -22,21 +23,15 @@ def dispatch data
def serve_file path
UI.debug "Serving file #{path}"
content_type = case File.extname(path)
when '.css'
'text/css'
when '.js'
'application/ecmascript'
when '.gif'
'image/gif'
when '.jpeg', '.jpg'
'image/jpeg'
when '.png'
'image/png'
else
'text/plain'
when '.css' then 'text/css'
when '.js' then 'application/ecmascript'
when '.gif' then 'image/gif'
when '.jpeg', '.jpg' then 'image/jpeg'
when '.png' then 'image/png'
else; 'text/plain'
end
send_data "HTTP/1.1 200 OK\r\nContent-Type: #{content_type}\r\nContent-Length: #{File.size path}\r\n\r\n"
stream_file_data(path).callback{ close_connection_after_writing }
stream_file_data(path).callback { close_connection_after_writing }
end

end
Expand Down
37 changes: 12 additions & 25 deletions spec/guard/livereload_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,6 @@
subject { Guard::LiveReload.new }

describe "#initialize" do
describe ":api_version option" do
it "is '1.6' by default" do
subject = Guard::LiveReload.new([])
subject.options[:api_version].should == '1.6'
end

it "can be set to '1.3'" do
subject = Guard::LiveReload.new([], { :api_version => '1.3' })
subject.options[:api_version].should == '1.3'
end
end

describe ":host option" do
it "is '0.0.0.0' by default" do
subject = Guard::LiveReload.new([])
Expand All @@ -40,27 +28,27 @@
end
end

describe ":apply_js_live option" do
describe ":apply_css_live option" do
it "is true by default" do
subject = Guard::LiveReload.new([])
subject.options[:apply_js_live].should be_true
subject.options[:apply_css_live].should be_true
end

it "can be set to false" do
subject = Guard::LiveReload.new([], { :apply_js_live => false })
subject.options[:apply_js_live].should be_false
subject = Guard::LiveReload.new([], { :apply_css_live => false })
subject.options[:apply_css_live].should be_false
end
end

describe ":apply_css_live option" do
it "is true by default" do
describe ":override_url option" do
it "is false by default" do
subject = Guard::LiveReload.new([])
subject.options[:apply_css_live].should be_true
subject.options[:override_url].should be_false
end

it "can be set to false" do
subject = Guard::LiveReload.new([], { :apply_css_live => false })
subject.options[:apply_css_live].should be_false
subject = Guard::LiveReload.new([], { :override_url => true })
subject.options[:override_url].should be_true
end
end

Expand All @@ -81,24 +69,23 @@
it "creates reactor with default options" do
subject = Guard::LiveReload.new([])
Guard::LiveReload::Reactor.should_receive(:new).with(
:api_version => '1.6',
:host => '0.0.0.0',
:port => '35729',
:apply_css_live => true,
:apply_js_live => true,
:override_url => false,
:grace_period => 0
)
subject.start
end

it "creates reactor with given options" do
subject = Guard::LiveReload.new([], { :api_version => '1.3', :host => '127.3.3.1', :port => '12345', :apply_js_live => false, :apply_css_live => false, :grace_period => 1 })
subject = Guard::LiveReload.new([], { :api_version => '1.3', :host => '127.3.3.1', :port => '12345', :apply_css_live => false, :override_url => true, :grace_period => 1 })
Guard::LiveReload::Reactor.should_receive(:new).with(
:api_version => '1.3',
:host => '127.3.3.1',
:port => '12345',
:apply_css_live => false,
:apply_js_live => false,
:override_url => true,
:grace_period => 1
)
subject.start
Expand Down

0 comments on commit e06e711

Please sign in to comment.