Skip to content

Commit

Permalink
supporting cache through use_trait
Browse files Browse the repository at this point in the history
  • Loading branch information
guilhermesilveira committed Oct 26, 2010
1 parent e77bb11 commit eae9552
Show file tree
Hide file tree
Showing 9 changed files with 106 additions and 149 deletions.
2 changes: 2 additions & 0 deletions Gemfile
Expand Up @@ -9,6 +9,8 @@ gem "json_pure"
gem "sqlite3-ruby"
gem "yard"

gem "respondie"

if RUBY_VERSION < "1.9"
gem "ruby-debug"
else
Expand Down
2 changes: 2 additions & 0 deletions Gemfile.lock
Expand Up @@ -72,6 +72,7 @@ GEM
thor (~> 0.14.0)
rake (0.8.7)
rcov (0.9.8)
respondie (0.9.0)
rspec (2.0.0.beta.19)
rspec-core (= 2.0.0.beta.19)
rspec-expectations (= 2.0.0.beta.19)
Expand Down Expand Up @@ -117,6 +118,7 @@ DEPENDENCIES
rack-conneg
rails (>= 3.0.0)
rcov
respondie
rspec-rails (>= 2.0.0.beta.19)
ruby-debug
sinatra
Expand Down
4 changes: 4 additions & 0 deletions lib/restfulie/client/feature/cache.rb
@@ -1,7 +1,11 @@
class Restfulie::Client::Feature::Cache

def execute(flow, request, response, env)
Restfulie::Client.cache_provider.get([request.host, request.path], request)
resp = flow.continue(request, response, env)
unless resp.kind_of? Exception
Restfulie::Client.cache_provider.put([request.host, request.path], request, response)
end
end

end
67 changes: 0 additions & 67 deletions lib/restfulie/client/http/response_handler.rb

This file was deleted.

6 changes: 6 additions & 0 deletions lib/restfulie/server.rb
@@ -1,3 +1,4 @@
require 'respondie'
require 'restfulie/common'

module Restfulie
Expand All @@ -16,4 +17,9 @@ class ActionController::Base
def self.restfulie
include Restfulie::Server::ActionController::Base
end

def self.use_trait(&block)
Respondie::Builder.new("Restfulie::Server::ActionController::Trait::$trait$", self).instance_eval(&block)
end

end
6 changes: 1 addition & 5 deletions lib/restfulie/server/action_controller.rb
Expand Up @@ -2,11 +2,7 @@ module Restfulie
module Server
module ActionController #:nodoc:
if defined?(::ActionController) || defined?(::ApplicationController)
autoload :ParamsParser, 'restfulie/server/action_controller/params_parser'
autoload :CacheableResponder, 'restfulie/server/action_controller/cacheable_responder'
autoload :CreatedResponder, 'restfulie/server/action_controller/created_responder'
autoload :RestfulResponder, 'restfulie/server/action_controller/restful_responder'
autoload :Base, 'restfulie/server/action_controller/base'
Dir["#{File.dirname(__FILE__)}/action_controller/*.rb"].each {|f| autoload File.basename(f)[0..-4].camelize.to_sym, f }
end
end
end
Expand Down
77 changes: 0 additions & 77 deletions lib/restfulie/server/action_controller/cacheable_responder.rb

This file was deleted.

10 changes: 10 additions & 0 deletions lib/restfulie/server/action_controller/trait.rb
@@ -0,0 +1,10 @@
module Restfulie
module Server
module ActionController #:nodoc:
module Trait #:nodoc:
puts "loading trait..."
Dir["#{File.dirname(__FILE__)}/trait/*.rb"].each {|f| autoload File.basename(f)[0..-4].camelize.to_sym, f }
end
end
end
end
81 changes: 81 additions & 0 deletions lib/restfulie/server/action_controller/trait/cacheable.rb
@@ -0,0 +1,81 @@
class Expires
def do_http_cache(responder, headers)
if responder.options[:expires_in]
headers << "max-age=#{responder.options[:expires_in]}"
true
else
false
end
end
end

class LastModifieds
# default implementation that will check whether caching can be applied
def accepts?(responder)
responder.resources.flatten.select do |resource|
resource.respond_to?(:updated_at)
end &&
responder.controller.response.last_modified.nil? &&
!new_record?(responder)
end

def do_http_cache(responder, headers)
return false unless accepts?(responder)

timestamps = responder.resources.flatten.select do |resource|
resource.respond_to?(:updated_at)
end.map do |resource|
(resource.updated_at || Time.now).utc
end

timestamp = timestamps.max

responder.controller.response.last_modified = timestamp if timestamp
end

def new_record?(responder)
responder.resource.respond_to?(:new_record?) && responder.resource.new_record?
end

end

module Restfulie::Server::ActionController
module Trait
module Cacheable

CACHES = [::Expires.new, ::LastModifieds.new]

def to_format
headers = cache_control_headers
cached = CACHES.inject(false) do |cached, cache|
cached || cache.do_http_cache(self, headers)
end
if ::ActionController::Base.perform_caching && cached
set_public_cache_control(headers)
controller.response.headers["Cache-Control"] = headers.join(', ')
fresh = request.fresh?(controller.response)
if fresh
head :not_modified
else
super
end
else
super
end
end

private

def set_public_cache_control(headers)
headers.delete("private")
headers.delete("no-cache")
headers << "public"
end

def cache_control_headers
(controller.response.headers["Cache-Control"] || "").split(",").map {|k| k.strip }
end

end
end
end

0 comments on commit eae9552

Please sign in to comment.