Sha256: 6f656a1d53be68aa307211eeb680b0a425135652f42f6fb88feb8318b4c80a60

Contents?: true

Size: 815 Bytes

Versions: 2

Compression:

Stored size: 815 Bytes

Contents

module Goliath
  module Rack
    class DefaultResponseFormat
      def initialize(app)
        @app = app
      end

      def call(env)
        async_cb = env['async.callback']
        env['async.callback'] = Proc.new do |status, headers, body|
          async_cb.call(post_process(status, headers, body))
        end

        status, headers, body = @app.call(env)
        post_process(status, headers, body)
      end

      def post_process(status, headers, body)
        return [status, headers, body] if body.respond_to?(:to_ary)

        new_body = []
        if body.respond_to?(:each)
          body.each { |chunk| new_body << chunk }
        else
          new_body << body
        end
        new_body.collect! { |item| item.to_s }

        [status, headers, new_body.flatten]
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
goliath-0.9.1 lib/goliath/rack/default_response_format.rb
goliath-0.9.0 lib/goliath/rack/default_response_format.rb