Sha256: 3bf09bb64c2c331bd8e45fd9b7425b4ccccb2c55044ccf669f05b12b2396198d

Contents?: true

Size: 584 Bytes

Versions: 2

Compression:

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

        [status, headers, ["#{env.params['callback']}(#{response})"]]
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
goliath-0.9.4 lib/goliath/rack/jsonp.rb
goliath-0.9.2 lib/goliath/rack/jsonp.rb