Class: HTTP::Response::Caching

### Home Classes Methods

### In Files

- lib/http/response/caching.rb

### Parent

DelegateClass(HTTP::Response)

### Methods

- ::new
- #body
- #body=
- #cache_headers
- #cacheable?
- #caching
- #current_age
- #expired?
- #received_at
- #requested_at
- #server_response_time
- #stale?
- #to_time_or_epoch
- #validated!
- #vary

### Class/Module Index

Quicksearch

- HTTP
- HTTP::Cache
- HTTP::Cache::Headers
- HTTP::Cache::NullCache
- HTTP::Cache::NullLogger
- HTTP::CacheError
- HTTP::Chainable
- HTTP::Client
- HTTP::Connection
- HTTP::Error
- HTTP::Headers
- HTTP::Headers::Mixin
- HTTP::InvalidHeaderNameError
- HTTP::MimeType
- HTTP::MimeType::Adapter
- HTTP::MimeType::JSON
- HTTP::Options
- HTTP::Redirector
- HTTP::Redirector::EndlessRedirectError
- HTTP::Redirector::TooManyRedirectsError
- HTTP::Request
- HTTP::Request::Caching
- HTTP::Request::UnsupportedMethodError
- HTTP::Request::UnsupportedSchemeError
- HTTP::Request::Writer
- HTTP::RequestError
- HTTP::Response
- HTTP::Response::Body
- HTTP::Response::Caching
- HTTP::Response::IoBody
- HTTP::Response::Parser
- HTTP::Response::Status
- HTTP::Response::StringBody
- HTTP::ResponseError
- HTTP::StateError
- HTTP::Timeout
- HTTP::Timeout::Global
- HTTP::Timeout::Null
- HTTP::Timeout::PerOperation
- HTTP::TimeoutError
- HTTP::URI
- Object

No matching classes.

# HTTP::Response::Caching

Decorator class for responses to provide convenience methods related to caching.

### Constants

CACHEABLE_RESPONSE_CODES

### Attributes

received_at[W]
requested_at[W]

### Public Class Methods

new(obj) click to toggle source

    # File lib/http/response/caching.rb, line 13
    def initialize(obj)
      super
      @requested_at = nil
      @received_at  = nil
    end

### Public Instance Methods

body() click to toggle source

    # File lib/http/response/caching.rb, line 92
    def body
      @body ||= if __getobj__.body.respond_to? :each
                  __getobj__.body
                else
                  StringBody.new(__getobj__.body.to_s)
                end
    end

body=(new_body) click to toggle source

    # File lib/http/response/caching.rb, line 100
    def body=(new_body)
      @body = if new_body.respond_to?(:readpartial) && new_body.respond_to?(:read)
                # IO-ish, probably a rack cache response body
                IoBody.new(new_body)

              elsif new_body.respond_to? :join
                # probably an array of body parts (rack cache does this sometimes)
                StringBody.new(new_body.join(""))

              elsif new_body.respond_to? :readpartial
                # normal body, just use it.
                new_body

              else
                # backstop, just to_s it
                StringBody.new(new_body.to_s)
              end
    end

cache_headers() click to toggle source

@return [HTTP::Cache::Headers] cache control headers helper object.

    # File lib/http/response/caching.rb, line 88
    def cache_headers
      @cache_headers ||= HTTP::Cache::Headers.new headers
    end

cacheable?() click to toggle source

@return [Boolean] true iff this response is cacheable

A Vary header field-value of "*" always fails to match and subsequent requests on that resource can only be properly interpreted by the

    # File lib/http/response/caching.rb, line 40
    def cacheable?
      @cacheable ||=
        begin
          CACHEABLE_RESPONSE_CODES.include?(code)                && !(cache_headers.vary_star? ||
                 cache_headers.no_store?  ||
                 cache_headers.no_cache?)
        end
    end

caching() click to toggle source

@return [HTTP::Response::Caching]

    # File lib/http/response/caching.rb, line 20
    def caching
      self
    end

current_age() click to toggle source

@return [Numeric] the current age (in seconds) of this response

Algo from tools.ietf.org/html/rfc2616#section-13.2.3

    # File lib/http/response/caching.rb, line 54
    def current_age
      now = Time.now
      age_value  = headers.get(HTTP::Headers::AGE).map(&:to_i).max || 0

      apparent_age = [0, received_at - server_response_time].max
      corrected_received_age = [apparent_age, age_value].max
      response_delay = [0, received_at - requested_at].max
      corrected_initial_age = corrected_received_age + response_delay
      resident_time = [0, now - received_at].max

      corrected_initial_age + resident_time
    end

expired?() click to toggle source

@return [Boolean] true iff this response has expired

    # File lib/http/response/caching.rb, line 30
    def expired?
      current_age >= cache_headers.max_age
    end

received_at() click to toggle source

@return [Time] the time at which this response was received

    # File lib/http/response/caching.rb, line 74
    def received_at
      @received_at ||= Time.now
    end

requested_at() click to toggle source

@return [Time] the time at which this response was requested

    # File lib/http/response/caching.rb, line 68
    def requested_at
      @requested_at ||= received_at
    end

stale?() click to toggle source

@return [Boolean] true iff this response is stale

    # File lib/http/response/caching.rb, line 25
    def stale?
      expired? || cache_headers.must_revalidate?
    end

validated!(validating_response) click to toggle source

Update self based on this response being revalidated by the server.

    # File lib/http/response/caching.rb, line 81
    def validated!(validating_response)
      headers.merge!(validating_response.headers)
      self.requested_at  = validating_response.requested_at
      self.received_at   = validating_response.received_at
    end

vary() click to toggle source

    # File lib/http/response/caching.rb, line 119
    def vary
      headers.get(HTTP::Headers::VARY).first
    end

### Protected Instance Methods

server_response_time() click to toggle source

@return [Time] the time at which the server generated this response.

    # File lib/http/response/caching.rb, line 126
    def server_response_time
      headers.get(HTTP::Headers::DATE).
        map(&method(:to_time_or_epoch)).
        max || begin
                  # set it if it is not already set
                  headers[HTTP::Headers::DATE] = received_at.httpdate
                  received_at
                end
    end

to_time_or_epoch(t_str) click to toggle source

    # File lib/http/response/caching.rb, line 136
    def to_time_or_epoch(t_str)
      Time.httpdate(t_str)
    rescue ArgumentError
      Time.at(0)
    end

[Validate]

Generated with the Darkfish Rdoc Generator 2.