Sha256: 2ce7eef41cc207eee1832a2f144caf9421259a3824aaceb294d8d4a92a58006e

Contents?: true

Size: 661 Bytes

Versions: 6

Compression:

Stored size: 661 Bytes

Contents

module Goliath
  module Rack
    # A middleware to wrap the response into a JSONP callback.
    #
    # @example
    #  use Goliath::Rack::JSONP
    #
    class JSONP
      include Goliath::Rack::AsyncMiddleware

      def post_process(env, status, headers, body)
        return [status, headers, body] unless env.params['callback']

        response = ""
        if body.respond_to?(:each)
          body.each { |s| response << s }
        else
          response = body
        end

        headers[Goliath::Constants::CONTENT_TYPE] = 'application/javascript'
        [status, headers, ["#{env.params['callback']}(#{response})"]]
      end
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
goliath-1.0.4 lib/goliath/rack/jsonp.rb
goliath-1.0.3 lib/goliath/rack/jsonp.rb
goliath-1.0.2 lib/goliath/rack/jsonp.rb
goliath-1.0.1 lib/goliath/rack/jsonp.rb
goliath-1.0.0 lib/goliath/rack/jsonp.rb
goliath-1.0.0.beta.1 lib/goliath/rack/jsonp.rb