Sha256: 0f25a3f4610f20a4008a2207e05413876d74d2803e9954ca6ba06e539719a001

Contents?: true

Size: 1.28 KB

Versions: 6

Compression:

Stored size: 1.28 KB

Contents

require 'erb'
require 'rack/livereload/processing_skip_analyzer'
require 'rack/livereload/body_processor'

module Rack
  class LiveReload
    attr_reader :app

    def initialize(app, options = {})
      @app, @options = app, options
    end

    def call(env)
      dup._call(env)
    end

    def _call(env)
      _, path, file = (env['PATH_INFO'] || '').split('/')

      if path == '__rack' && ::File.file?(target = ::File.expand_path("../../../js/#{file}", __FILE__))
        deliver_file(target)
      else
        status, headers, body = result = @app.call(env)

        return result if ProcessingSkipAnalyzer.skip_processing?(result, env, @options)

        processor = BodyProcessor.new(body, @options)
        processor.process!(env)

        headers['Content-Length'] = processor.content_length.to_s

        if processor.livereload_added
          headers['X-Rack-LiveReload'] = '1'
        end

        [ status, headers, processor.new_body ]
      end
    end

    private
    def deliver_file(file)
      type = case ::File.extname(file)
             when '.js'
               'text/javascript'
             when '.swf'
               'application/swf'
             end

      [ 200, { 'Content-Type' => type, 'Content-Length' => ::File.size(file).to_s }, [ ::File.read(file) ] ]
    end
  end
end

Version data entries

6 entries across 6 versions & 2 rubygems

Version Path
rack-livereload-0.5.2 lib/rack/livereload.rb
rack-livereload-0.3.17 lib/rack/livereload.rb
leifcr-rack-livereload-0.3.16 lib/rack/livereload.rb
rack-livereload-0.3.16 lib/rack/livereload.rb
rack-livereload-0.3.15 lib/rack/livereload.rb
rack-livereload-0.3.14 lib/rack/livereload.rb