rack-livereload-0.3.17/ 0000750 0023420 0023420 00000000000 13657146530 014104 5 ustar duck dc-users rack-livereload-0.3.17/spec/ 0000750 0023420 0023420 00000000000 13657146530 015036 5 ustar duck dc-users rack-livereload-0.3.17/spec/spec_helper.rb 0000640 0023420 0023420 00000000357 13657146530 017662 0 ustar duck dc-users require 'mocha/api'
require 'webmock/rspec'
require 'rack-livereload'
RSpec.configure do |c|
c.mock_with :mocha
end
module RSpec::Matchers
define :use_vendored do
match do |subject|
subject.use_vendored?
end
end
end
rack-livereload-0.3.17/spec/rack/ 0000750 0023420 0023420 00000000000 13657146530 015756 5 ustar duck dc-users rack-livereload-0.3.17/spec/rack/livereload_spec.rb 0000640 0023420 0023420 00000001122 13657146530 021440 0 ustar duck dc-users require 'spec_helper'
require 'nokogiri'
describe Rack::LiveReload do
let(:middleware) { described_class.new(app, options) }
let(:app) { stub }
subject { middleware }
it 'should be an app' do
middleware.app.should be == app
end
let(:env) { {} }
let(:options) { {} }
context '/__rack/livereload.js' do
let(:env) { { 'PATH_INFO' => described_class::BodyProcessor::LIVERELOAD_JS_PATH } }
before do
middleware.expects(:deliver_file).returns(true)
end
it 'should return the js file' do
middleware._call(env).should be_truthy
end
end
end
rack-livereload-0.3.17/spec/rack/livereload/ 0000750 0023420 0023420 00000000000 13657146530 020104 5 ustar duck dc-users rack-livereload-0.3.17/spec/rack/livereload/processing_skip_analyzer_spec.rb 0000640 0023420 0023420 00000005761 13657146530 026564 0 ustar duck dc-users require 'spec_helper'
describe Rack::LiveReload::ProcessingSkipAnalyzer do
subject { described_class.new(result, env, options) }
let(:result) { [ status, headers, body ] }
let(:env) { { 'HTTP_USER_AGENT' => user_agent } }
let(:options) { {} }
let(:user_agent) { 'Firefox' }
let(:status) { 200 }
let(:headers) { {} }
let(:body) { [] }
describe '#skip_processing?' do
it "should skip processing" do
subject.skip_processing?.should be_truthy
end
end
describe '#ignored?' do
let(:options) { { :ignore => [ %r{file} ] } }
context 'path contains ignore pattern' do
let(:env) { { 'PATH_INFO' => '/this/file', 'QUERY_STRING' => '' } }
it { should be_ignored }
end
context 'root path' do
let(:env) { { 'PATH_INFO' => '/', 'QUERY_STRING' => '' } }
it { should_not be_ignored }
end
end
describe '#chunked?' do
context 'regular response' do
it { should_not be_chunked }
end
context 'chunked response' do
let(:headers) { { 'Transfer-Encoding' => 'chunked' } }
it { should be_chunked }
end
end
describe '#inline?' do
context 'inline disposition' do
let(:headers) { { 'Content-Disposition' => 'inline; filename=my_inlined_file' } }
it { should be_inline }
end
end
describe '#ignored?' do
let(:path_info) { 'path info' }
let(:query_string) { 'query_string' }
let(:env) { { 'PATH_INFO' => path_info, 'QUERY_STRING' => query_string } }
context 'no ignore set' do
it { should_not be_ignored }
end
context 'ignore set' do
let(:options) { { :ignore => [ %r{#{path_info}} ] } }
it { should be_ignored }
end
context 'ignore set including query_string' do
let(:options) { { :ignore => [ %r{#{path_info}\?#{query_string}} ] } }
it { should be_ignored }
end
end
describe '#bad_browser?' do
context 'Firefox' do
it { should_not be_bad_browser }
end
context 'BAD browser' do
let(:user_agent) { described_class::BAD_USER_AGENTS.first.source }
it { should be_bad_browser }
end
end
describe '#html?' do
context 'HTML content' do
let(:headers) { { 'Content-Type' => 'text/html' } }
it { should be_html }
end
context 'PDF content' do
let(:headers) { { 'Content-Type' => 'application/pdf' } }
it { should_not be_html }
end
end
describe '#get?' do
context 'GET request' do
let(:env) { { 'REQUEST_METHOD' => 'GET' } }
it { should be_get }
end
context 'PUT request' do
let(:env) { { 'REQUEST_METHOD' => 'PUT' } }
it { should_not be_get }
end
context 'POST request' do
let(:env) { { 'REQUEST_METHOD' => 'POST' } }
it { should_not be_get }
end
context 'DELETE request' do
let(:env) { { 'REQUEST_METHOD' => 'DELETE' } }
it { should_not be_get }
end
context 'PATCH request' do
let(:env) { { 'REQUEST_METHOD' => 'PATCH' } }
it { should_not be_get }
end
end
end
rack-livereload-0.3.17/spec/rack/livereload/body_processor_spec.rb 0000640 0023420 0023420 00000013217 13657146530 024504 0 ustar duck dc-users require 'spec_helper'
require 'nokogiri'
describe Rack::LiveReload::BodyProcessor do
describe 'head tag regex' do
let(:regex) { described_class::HEAD_TAG_REGEX }
subject { regex }
it { should be_kind_of(Regexp) }
it 'only picks a valid
tag' do
regex.match("").to_s.should eq('')
regex.match("").to_s.should eq('')
regex.match("").to_s.should eq("")
end
it 'responds false when no head tag' do
regex.match("").should be_falsey
end
end
let(:processor) { described_class.new(body, options) }
let(:body) { [ page_html ] }
let(:options) { {} }
let(:page_html) { '' }
let(:processor_result) do
if !processor.processed?
processor.process!(env)
end
processor
end
subject { processor }
describe "livereload local uri" do
context 'does not exist' do
before do
stub_request(:any, 'localhost:35729/livereload.js').to_timeout
end
it { should use_vendored }
end
context 'exists' do
before do
stub_request(:any, 'localhost:35729/livereload.js')
end
it { should_not use_vendored }
end
context 'with custom port' do
let(:options) { {:live_reload_port => '12348'}}
context 'exists' do
before do
stub_request(:any, 'localhost:12348/livereload.js')
end
it { should_not use_vendored }
end
end
context 'specify vendored' do
let(:options) { { :source => :vendored } }
it { should use_vendored }
end
context 'specify LR' do
let(:options) { { :source => :livereload } }
it { should_not use_vendored }
end
end
context 'text/html' do
before do
processor.stubs(:use_vendored?).returns(true)
end
let(:host) { 'host' }
let(:env) { { 'HTTP_HOST' => host } }
let(:processed_body) { processor_result.new_body.join('') }
let(:length) { processor_result.content_length }
let(:page_html) { '' }
context 'vendored' do
it 'should add the vendored livereload js script tag' do
processed_body.should include("script")
processed_body.should include(described_class::LIVERELOAD_JS_PATH)
length.to_s.should == processed_body.length.to_s
described_class::LIVERELOAD_JS_PATH.should_not include(host)
processed_body.should include('swfobject')
processed_body.should include('web_socket')
end
end
context 'at the top of the head tag' do
let(:page_html) { '' }
let(:body_dom) { Nokogiri::XML(processed_body) }
it 'should add the livereload js script tag before all other script tags' do
body_dom.at_css("head")[:attribute].should == 'attribute'
body_dom.at_css("script:eq(5)")[:src].should include(described_class::LIVERELOAD_JS_PATH)
body_dom.at_css("script:last-child")[:insert].should == "before"
end
context 'when a relative URL root is specified' do
before do
ENV['RAILS_RELATIVE_URL_ROOT'] = '/a_relative_path'
end
it 'should prepend the relative path to the script src' do
body_dom.at_css("script:eq(5)")[:src].should match(%r{^/a_relative_path/})
end
end
end
describe "LIVERELOAD_PORT value" do
let(:options) { { :live_reload_port => 12345 }}
it "sets the variable at the top of the file" do
processed_body.should include 'RACK_LIVERELOAD_PORT = 12345'
end
end
context 'in header tags' do
let(:page_html) { "" }
let(:body_dom) { Nokogiri::XML(processed_body) }
it 'should not add the livereload js' do
body_dom.at_css("header")[:class].should == 'hero'
body_dom.css('script').should be_empty
end
end
context 'not vendored' do
before do
processor.stubs(:use_vendored?).returns(false)
end
it 'should add the LR livereload js script tag' do
processed_body.should include("script")
processed_body.should include(processor.livereload_local_uri.gsub('localhost', 'host'))
end
end
context 'set options' do
let(:options) { { :host => new_host, :port => port, :min_delay => min_delay, :max_delay => max_delay } }
let(:min_delay) { 5 }
let(:max_delay) { 10 }
let(:port) { 23 }
let(:new_host) { 'myhost' }
it 'should add the livereload.js script tag' do
processed_body.should include("mindelay=#{min_delay}")
processed_body.should include("maxdelay=#{max_delay}")
processed_body.should include("port=#{port}")
processed_body.should include("host=#{new_host}")
end
end
context 'force flash' do
let(:options) { { :force_swf => true } }
it 'should not add the flash shim' do
processed_body.should include('WEB_SOCKET_FORCE_FLASH')
processed_body.should include('swfobject')
processed_body.should include('web_socket')
end
end
context 'no flash' do
let(:options) { { :no_swf => true } }
it 'should not add the flash shim' do
processed_body.should_not include('swfobject')
processed_body.should_not include('web_socket')
end
end
context 'no host at all' do
let(:env) { {} }
it 'should use localhost' do
processed_body.should include('localhost')
end
end
end
end
rack-livereload-0.3.17/skel/ 0000750 0023420 0023420 00000000000 13657146530 015042 5 ustar duck dc-users rack-livereload-0.3.17/skel/livereload.html.erb 0000640 0023420 0023420 00000001054 13657146530 020626 0 ustar duck dc-users <% if with_swf? %>
<% end %>
rack-livereload-0.3.17/rack-livereload.gemspec 0000640 0023420 0023420 00000003214 13657146530 020516 0 ustar duck dc-users # -*- encoding: utf-8 -*-
$:.push File.expand_path("../lib", __FILE__)
require "rack-livereload"
Gem::Specification.new do |s|
s.name = "rack-livereload"
s.version = Rack::LiveReload::VERSION
s.authors = ["John Bintz"]
s.email = ["john@coswellproductions.com"]
s.homepage = "https://github.com/onesupercoder/rack-livereload"
s.license = "MIT"
s.summary = %q{Insert LiveReload into your app easily as Rack middleware}
s.description = %q{Insert LiveReload into your app easily as Rack middleware}
s.rubyforge_project = "rack-livereload"
s.files = `git ls-files`.split("\n")
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
s.require_paths = ["lib"]
# specify any dependencies here; for example:
s.add_development_dependency "rspec"
s.add_development_dependency "cucumber", "< 3"
s.add_development_dependency "httparty"
s.add_development_dependency "sinatra"
s.add_development_dependency "shotgun"
s.add_development_dependency "thin"
s.add_development_dependency "rake"
s.add_development_dependency "mocha"
s.add_development_dependency "guard"
s.add_development_dependency "guard-rspec"
s.add_development_dependency "guard-cucumber"
s.add_development_dependency "guard-livereload"
s.add_development_dependency "guard-bundler"
s.add_development_dependency "webmock"
s.add_development_dependency "nokogiri", ("< 1.6" if RUBY_VERSION < "1.9") # Nokogiri >= 1.6 requires Ruby >= 1.9
s.add_development_dependency 'appraisal', '~> 2.2.0'
s.add_runtime_dependency "rack"
end
rack-livereload-0.3.17/lib/ 0000750 0023420 0023420 00000000000 13657146530 014652 5 ustar duck dc-users rack-livereload-0.3.17/lib/rack/ 0000750 0023420 0023420 00000000000 13657146530 015572 5 ustar duck dc-users rack-livereload-0.3.17/lib/rack/livereload/ 0000750 0023420 0023420 00000000000 13657146530 017720 5 ustar duck dc-users rack-livereload-0.3.17/lib/rack/livereload/processing_skip_analyzer.rb 0000640 0023420 0023420 00000002200 13657146530 025347 0 ustar duck dc-users require 'rack/livereload'
module Rack
class LiveReload
class ProcessingSkipAnalyzer
BAD_USER_AGENTS = [ %r{MSIE} ]
def self.skip_processing?(result, env, options)
new(result, env, options).skip_processing?
end
def initialize(result, env, options)
@env, @options = env, options
@status, @headers, @body = result
end
def skip_processing?
!html? || chunked? || inline? || ignored? || bad_browser? || !get?
end
def chunked?
@headers['Transfer-Encoding'] == 'chunked'
end
def inline?
@headers['Content-Disposition'] =~ %r{^inline}
end
def ignored?
path = @env['QUERY_STRING'].empty? ? @env['PATH_INFO'] : "#{@env['PATH_INFO']}?#{@env['QUERY_STRING']}"
@options[:ignore] and @options[:ignore].any? { |filter| path[filter] }
end
def bad_browser?
BAD_USER_AGENTS.any? { |pattern| @env['HTTP_USER_AGENT'] =~ pattern }
end
def html?
@headers['Content-Type'] =~ %r{text/html}
end
def get?
@env['REQUEST_METHOD'] == 'GET'
end
end
end
end
rack-livereload-0.3.17/lib/rack/livereload/body_processor.rb 0000640 0023420 0023420 00000005466 13657146530 023315 0 ustar duck dc-users require 'rack/livereload'
module Rack
class LiveReload
class BodyProcessor
LIVERELOAD_JS_PATH = '/__rack/livereload.js'
HEAD_TAG_REGEX = /|/
LIVERELOAD_PORT = 35729
attr_reader :content_length, :new_body, :livereload_added
def protocol
@options[:protocol] || "http"
end
def livereload_local_uri
"#{protocol}://localhost:#{@options[:live_reload_port]}/livereload.js"
end
def initialize(body, options)
@body, @options = body, options
@options[:live_reload_port] ||= LIVERELOAD_PORT
@processed = false
end
def force_swf?
@options[:force_swf]
end
def with_swf?
!@options[:no_swf]
end
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
def processed?
@processed
end
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['