Permalink
Browse files

Purgeable::Client#purge: response object

  • Loading branch information...
1 parent 7d2945a commit 1aedb1d301ccc7f9e15fdcd1e10df146ae48ccdd @kawamanza kawamanza committed Mar 16, 2012
View
@@ -11,7 +11,7 @@ Configuration file structure:
sample: &sample
http_cache:
- resource_hosts:
- - "www1.example.com"
+ - "www.example.com"
- "www2.example.com"
cache_locations:
- "varnish1.example.com"
@@ -57,9 +57,18 @@ Simple usage:
client = Purgeable::Client.new
urls = %w[
http://www.example.com/article/my-example
- http://www.example.com/article/id/1234
+ http://www3.example.com/article/id/1234
]
-client.purge(urls)
+client.purge(urls) # => {
+ # "http://www.example.com/article/my-example" => {
+ # "varnish1.example.com" => #<Net::HTTPOK 200 Purged readbody=true>,
+ # "varnish2.example.com" => #<Net::HTTPOK 200 Purged readbody=true>
+ # },
+ # "http://www3.example.com/article/id/1234" => {
+ # "varnish3.example.com" => #<Net::HTTPOK 200 Purged readbody=true>,
+ # "varnish4.example.com" => #<Net::HTTPOK 200 Purged readbody=true>
+ # },
+ # }
```
Model helpers usage:
@@ -69,7 +78,7 @@ class Article < ActiveRecord::Base
include Purgeable::HttpResource
def resource_url
- "http://www.example.com/article/id/#{id}"
+ "http://www3.example.com/article/id/#{id}"
end
http_purge { "http://www.example.com/article/#{friendly_title}" }
View
@@ -2,15 +2,22 @@
module Purgeable
class Client
def purge(urls)
+ purge_all(urls)
+ end
+
+ private
+ def purge_all(urls, responses = {})
if urls.is_a?(String)
- cache_locations_for(urls).map{ |location| perform_purge(location, urls) }
+ cache_locations_for(urls).each do |location|
+ next if (responses[urls] ||= {}).has_key?(location)
+ responses[urls][location] = perform_purge(location, urls)
+ end
elsif urls.is_a?(Array)
- urls.map{ |url| purge(url) }
+ urls.each{ |url| purge_all(url, responses) }
end
+ responses
end
- private
-
def perform_purge(location, url)
ip, port = location.split(/:/)
port = 80 if port.nil? || port.empty?
@@ -16,7 +16,7 @@ def perform_purge!
end
def perform_purge
- perform_purge! rescue []
+ perform_purge! rescue {}
end
private
View
@@ -26,12 +26,13 @@
end
it "should purge urls" do
- res = @client.purge(["http://www.example.com/test/index.html"])
- res.should be_kind_of(Array)
- res.size.should be 1
- res.first.should be_kind_of(Array)
- res.first.size.should be 2
- res.first.first.should be_kind_of(::Net::HTTPOK)
- res.first.last.should be_kind_of(::Net::HTTPOK)
+ urls = ["http://www.example.com/test/index.html"]
+ res = @client.purge(urls)
+ res.should be_kind_of(Hash)
+ res.keys.should == urls
+ res[urls.first].keys == ["localhost2:80", "localhost:80"]
+ response = res[urls.first]["localhost:80"]
+ response.should be_kind_of(Net::HTTPOK)
+ response.code.should == "200"
end
end
@@ -6,22 +6,25 @@ class FakeModel
attr_accessor :slug, :uuid
include ::Purgeable::HttpResource
- http_purge { "http://varnish.example.com/#{slug}" }
+ http_purge { "http://www.example.com/#{slug}" }
http_purge :resource_url
# after_save :perform_purge
def resource_url
- "http://varnish.example.com/id/#{uuid}" unless uuid.nil?
+ "http://www3.example.com/id/#{uuid}" unless uuid.nil?
end
end
before { @model = FakeModel.new }
it "should has purgeable urls" do
@model.slug = "teste"
- @model.purgeable_urls.should == ["http://varnish.example.com/teste"]
+ @model.purgeable_urls.should == ["http://www.example.com/teste"]
@model.uuid = 10
@model.purgeable_urls.should ==
- ["http://varnish.example.com/teste", "http://varnish.example.com/id/10"]
+ ["http://www.example.com/teste", "http://www3.example.com/id/10"]
+ purged = @model.send(:perform_purge)
+ purged.keys.should ==
+ ["http://www.example.com/teste"]
end
end
@@ -1,3 +1,4 @@
+# encoding: UTF-8
RSpec.configure do |config|
config.before :suite do
FakeWeb.register_uri(:purge, /http:\/\/localhost/, :body => "")
@@ -6,3 +6,10 @@ test:
cache_locations:
- "localhost:80"
- "localhost2:80"
+
+empty:
+ http_cache:
+
+empty_http_cache:
+ http_cache: []
+
View
@@ -1,2 +1,9 @@
-config = File.expand_path("../purgeable_test.yml", __FILE__)
-Purgeable.load_settings(config, "test")
+# encoding: UTF-8
+RSpec.configure do |config|
+ config.before :suite do
+ config = File.expand_path("../purgeable_test.yml", __FILE__)
+ Purgeable.load_settings(config, "empty")
+ Purgeable.load_settings(config, "empty_http_cache")
+ Purgeable.load_settings(config, "test")
+ end
+end

0 comments on commit 1aedb1d

Please sign in to comment.