Sha256: 436908ec67a57a0688e82f3aad67b503391c99b77182de115f03239d761a3007

Contents?: true

Size: 1.52 KB

Versions: 12

Compression:

Stored size: 1.52 KB

Contents

class Rad::Http::Response < Rack::Response
  include ::OpenConstructor

  STATUS_MESSAGES = {
    ok: 200,
    not_found: 404,
    failed: 500,
    error: 500,
    redirect: 302
  }

  def initialize *args
    super
    clear
  end

  def content_type= type; self["Content-Type"] = type end
  def content_type; self["Content-Type"] end
  def content_type?; !!content_type end

  def location; self['Location'] end
  def location= location; self['Location'] = location end

  # def to_a
  #   [status, headers, body]
  # end 

  def body_as_string
    if @body.is_a? String
      @body
    else
      @body ? @body.join : ""
    end
  end

  def inspect
    to_a2.inspect
  end

  def == other
    to_a2 == other
  end

  def cookies
    self['Set-Cookie']
  end
    
  def clear
    @status = 200
    @header = Rack::Utils::HeaderHash.new("Content-Type" => nil)    
    @length = 0
    @body = []
  end    

  def status= code_or_message
    @status = if code_or_message.is_a? Numeric
      code_or_message
    else
      self.class.decode_status_message(code_or_message) || raise("unknown http status message '#{code_or_message}'!")
    end
  end

  def self.decode_status_message message
    STATUS_MESSAGES[message]
  end


  #
  # Status helpers
  #
  STATUS_MESSAGES.each do |message, status_code|
    define_method("#{message}?"){self.status == status_code}
  end
  alias_method :success?, :ok?

  def redirect?
    super or (body and body.include?('window.location'))
  end

  protected
    def to_a2
      [status, header, body_as_string]
    end
end

Version data entries

12 entries across 12 versions & 1 rubygems

Version Path
rad_core-0.2.6 lib/rad/http/_response.rb
rad_core-0.2.5 lib/rad/http/_response.rb
rad_core-0.2.4 lib/rad/http/_response.rb
rad_core-0.2.3 lib/rad/http/_response.rb
rad_core-0.2.2 lib/rad/http/_response.rb
rad_core-0.2.1 lib/rad/http/_response.rb
rad_core-0.2.0 lib/rad/http/_response.rb
rad_core-0.0.30 lib/rad/http/_response.rb
rad_core-0.0.29 lib/rad/http/_response.rb
rad_core-0.0.28 lib/rad/http/_response.rb
rad_core-0.0.27 lib/rad/http/_response.rb
rad_core-0.0.26 lib/rad/http/_response.rb