Sha256: f5006d0f4f57c85c9dd234936bab7c0b24a28518dd60e3990c405a4548cd0bd5

Contents?: true

Size: 1.14 KB

Versions: 3

Compression:

Stored size: 1.14 KB

Contents

module Restfulie::Server::ActionController
  class RestfulResponder < ActionController::Responder
    
    def to_format
      return if do_http_cache? && do_http_cache!
      super
    end
      
  protected
    def do_http_cache!
      timestamp = resources.flatten.map do |resource|
        (resource.updated_at || Time.now).utc if resource.respond_to?(:updated_at)
      end.compact.max

      controller.response.last_modified = timestamp if timestamp
      set_public_cache_control!

      head :not_modified if fresh = request.fresh?(controller.response)
      fresh
    end

    def do_http_cache?
      get? && @http_cache != false && ActionController::Base.perform_caching &&
        !new_record? && controller.response.last_modified.nil?
    end

    def new_record?
      resource.respond_to?(:new_record?) && resource.new_record?
    end
    
    def set_public_cache_control!
      cache_control = response.headers["Cache-Control"].split(",").map {|k| k.strip }
      cache_control.delete("private")
      cache_control.delete("no-cache")
      cache_control << "public"
      response.headers["Cache-Control"] = cache_control.join(', ')
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
restfulie-0.7.2 lib/restfulie/server/action_controller/restful_responder.rb
restfulie-0.7.1 lib/restfulie/server/action_controller/restful_responder.rb
restfulie-0.7.0 lib/restfulie/server/action_controller/restful_responder.rb