Sha256: f93a409b188c23a0c6f8f7646b094db8c606e47b864da290d09ac794d0e549eb

Contents?: true

Size: 800 Bytes

Versions: 3

Compression:

Stored size: 800 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

        response = "#{env.params['callback']}(#{response})"

        headers[Goliath::Constants::CONTENT_TYPE] = 'application/javascript'
        if headers['Content-Length']
          headers['Content-Length'] = response.bytesize.to_s
        end

        [status, headers, [response]]
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
goliath-1.0.7 lib/goliath/rack/jsonp.rb
goliath-1.0.6 lib/goliath/rack/jsonp.rb
goliath-1.0.5 lib/goliath/rack/jsonp.rb