Sha256: 66f95d7e60605d91b4064e22a8f31f76df35e390610f7bc5f385412a79bd3b1c

Contents?: true

Size: 1.51 KB

Versions: 20

Compression:

Stored size: 1.51 KB

Contents

module JQuery
  module FileUpload
    module Rails
      class Middleware
        def initialize(app)
          @app = app
        end

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

        def _call(env)
          @status, @headers, @response = @app.call(env)
          @request = Rack::Request.new(env)

          if iframe_transport?
            @headers['Content-Type'] = 'text/html'
            [@status, @headers, self]
          else
            [@status, @headers, @response]
          end
        end

        def each(&block)
          block.call(html_document_left) if iframe_transport?
          @response.each(&block)
          block.call(html_document_right) if iframe_transport?
        end

        def iframe_transport?
          @request.params['X-Requested-With'] == 'IFrame'
        end

        def html_document_left
          "<!DOCTYPE html><html><body><textarea #{metadata}>"
        end

        def html_document_right
          "</textarea></body></html>"
        end

        def metadata
          meta = {}
          meta['data-status'] = @response.status if @response.respond_to? :status
          meta['data-statusText'] = @response.status_message if @response.respond_to? :status_message
          meta['data-type'] = @headers['Content-Type'] if @headers.has_key?('Content-Type')
          meta.map {|key,value| "#{key}='#{value}'" }.join(' ')
        end

        private

        def method_missing(method, *args)
          @response.send(method.intern, *args)
        end
      end
    end
  end
end

Version data entries

20 entries across 20 versions & 6 rubygems

Version Path
jquery-fileupload-rails-1.0.0 lib/jquery/fileupload/rails/middleware.rb
sn-jquery-fileupload-rails-0.4.9 lib/jquery/fileupload/rails/middleware.rb
sn-jquery-fileupload-rails-0.4.8 lib/jquery/fileupload/rails/middleware.rb
jquery-fileupload-rails-0.4.7 lib/jquery/fileupload/rails/middleware.rb
jquery-fileupload-rails-0.4.6 lib/jquery/fileupload/rails/middleware.rb
jquery-fileupload-rails-0.4.5 lib/jquery/fileupload/rails/middleware.rb
jquery-fileupload-rails-0.4.4 lib/jquery/fileupload/rails/middleware.rb
better-jquery-fileupload-rails-1.0.1 lib/jquery/fileupload/rails/middleware.rb
jquery-fileupload-rails-0.4.3 lib/jquery/fileupload/rails/middleware.rb
better-jquery-fileupload-rails-1.0.0 lib/jquery/fileupload/rails/middleware.rb
jquery-fileupload-rails-0.4.2 lib/jquery/fileupload/rails/middleware.rb
kf-jquery-fileupload-rails-1.0.1 lib/jquery/fileupload/rails/middleware.rb
kf-jquery-fileupload-rails-1.0.0 lib/jquery/fileupload/rails/middleware.rb
rails-jquery-fileupload-1.0.2 lib/jquery/fileupload/rails/middleware.rb
JCG-jquery-fileupload-0.0.4 lib/jquery/fileupload/rails/middleware.rb
JCG-jquery-fileupload-0.0.3 lib/jquery/fileupload/rails/middleware.rb
JCG-jquery-fileupload-0.0.2 lib/jquery/fileupload/rails/middleware.rb
JCG-jquery-fileupload-0.0.1 lib/jquery/fileupload/rails/middleware.rb
jquery-fileupload-rails-0.4.1 lib/jquery/fileupload/rails/middleware.rb
jquery-fileupload-rails-0.4.0 lib/jquery/fileupload/rails/middleware.rb