Why Gemfury? Push, build, and install  RubyGems npm packages Python packages Maven artifacts PHP packages Go Modules Debian packages RPM packages NuGet packages

Repository URL to install this package:

Details    
e / lib / e-core / instance / response.rb
Size: Mime:
# The response object. See Rack::Response and Rack::ResponseHelpers for more info:
# http://rack.rubyforge.org/doc/classes/Rack/Response.html
# http://rack.rubyforge.org/doc/classes/Rack/Response/Helpers.html
class EResponse < Rack::Response # kindly borrowed from Sinatra
  def initialize(*)
    super
    headers['Content-Type'] ||= 'text/html'
  end

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

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

  def finish
    result = body

    if drop_content_info?
      headers.delete "Content-Length"
      headers.delete "Content-Type"
    end

    if drop_body?
      close
      result = []
    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"] = body.inject(0) { |l, p| l + Rack::Utils.bytesize(p) }.to_s
    end

    [status.to_i, header, result]
  end

  private

  def calculate_content_length?
    headers["Content-Type"] and not headers["Content-Length"] and Array === body
  end

  def drop_content_info?
    status.to_i / 100 == 1 or drop_body?
  end

  def drop_body?
    [204, 205, 304].include?(status.to_i)
  end
end