Sha256: 4be8b3e947c26f1ba3a9e8f3b48ee1f4572c2923b76f1c19067be89fc555126b

Contents?: true

Size: 1.79 KB

Versions: 2

Compression:

Stored size: 1.79 KB

Contents

module ZMachine
  # A simple hash is returned for each request made by HttpClient with the
  # headers that were given by the server for that request.
  class HttpResponseHeader < Hash
    # The reason returned in the http response ("OK","File not found",etc.)
    attr_accessor :http_reason

    # The HTTP version returned.
    attr_accessor :http_version

    # The status code (as a string!)
    attr_accessor :http_status

    # Raw headers
    attr_accessor :raw

    # E-Tag
    def etag
      self[HttpClient::ETAG]
    end

    def last_modified
      self[HttpClient::LAST_MODIFIED]
    end

    # HTTP response status as an integer
    def status
      @status ||= Integer(http_status) rescue 0
    end

    # Length of content as an integer, or nil if chunked/unspecified
    def content_length
      @content_length ||= ((s = self[HttpClient::CONTENT_LENGTH]) &&
                           (s =~ /^(\d+)$/)) ? $1.to_i : nil
    end

    # Cookie header from the server
    def cookie
      self[HttpClient::SET_COOKIE]
    end

    # Is the transfer encoding chunked?
    def chunked_encoding?
      /chunked/i === self[HttpClient::TRANSFER_ENCODING]
    end

    def keepalive?
      /keep-alive/i === self[HttpClient::KEEP_ALIVE]
    end

    def compressed?
      /gzip|compressed|deflate/i === self[HttpClient::CONTENT_ENCODING]
    end

    def location
      self[HttpClient::LOCATION]
    end

    def [](key)
      super(key) || super(key.upcase.gsub('-','_'))
    end

    def informational?
      100 <= status && 200 > status
    end

    def successful?
      200 <= status && 300 > status
    end

    def redirection?
      300 <= status && 400 > status
    end

    def client_error?
      400 <= status && 500 > status
    end

    def server_error?
      500 <= status && 600 > status
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
z-http-request-0.2.0 lib/z-http/http_header.rb
z-http-request-0.1.0 lib/z-http/http_header.rb