Class: Goliath::Rack::DefaultResponseFormat

Inherits:
Object
  • Object
show all
Defined in:
lib/goliath/rack/default_response_format.rb

Instance Method Summary (collapse)

Constructor Details

- (DefaultResponseFormat) initialize(app)

A new instance of DefaultResponseFormat



4
5
6
# File 'lib/goliath/rack/default_response_format.rb', line 4

def initialize(app)
  @app = app
end

Instance Method Details

- (Object) call(env)



8
9
10
11
12
13
14
15
16
# File 'lib/goliath/rack/default_response_format.rb', line 8

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

- (Object) post_process(status, headers, body)



18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/goliath/rack/default_response_format.rb', line 18

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