Sha256: a504c6337a1ee31a8ae33badf01939dea1ab3586edf2b923da5ee5b64425d31e

Contents?: true

Size: 1.72 KB

Versions: 1

Compression:

Stored size: 1.72 KB

Contents

module Rad
  class Response < Rack::Response
    include ::OpenConstructor
    
    STATUS_MESSAGES = {
      ok: 200,
      not_found: 404,
      failed: 500,
      error: 500,
      redirect: 301
    }
    
    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
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
rad_core-0.0.13 lib/rad/http/support/rack/response.rb