Class: Safrano::Response

Inherits:
Rack::Response
  • Object
show all
Defined in:
lib/safrano/response.rb

Overview

borrowed fro Sinatra The response object. See Rack::Response and Rack::Response::Helpers for more info: rubydoc.info/github/rack/rack/master/Rack/Response rubydoc.info/github/rack/rack/master/Rack/Response/Helpers

Constant Summary collapse

DROP_BODY_RESPONSES =
[204, 205, 304].freeze

Instance Method Summary collapse

Constructor Details

#initializeResponse

Returns a new instance of Response.



15
16
17
18
# File 'lib/safrano/response.rb', line 15

def initialize(*)
  super
  headers[CONTENT_TYPE] ||= APPJSON_UTF8
end

Instance Method Details

#body=(value) ⇒ Object



20
21
22
23
# File 'lib/safrano/response.rb', line 20

def body=(value)
  value = value.body while value.is_a?(::Rack::Response)
  @body = value.is_a?(String) ? [value.to_str] : value
end

#eachObject



25
26
27
# File 'lib/safrano/response.rb', line 25

def each
  block_given? ? super : enum_for(:each)
end

#finishObject



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/safrano/response.rb', line 29

def finish
  result = body

  if 
    headers.delete 'Content-Length'
    headers.delete 'Content-Type'
  end

  if drop_body?
    close
    result = EMPTY_ARRAY
  end

  if calculate_content_length?
    # if some other code has already set Content-Length, don't muck with it
    # currently, this would be the static file-handler
    headers['Content-Length'] = calculated_content_length.to_s
  end

  [status.to_i, headers, result]
end