purgeable 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,37 @@
1
+ # encoding: UTF-8
2
+ module Purgeable
3
+ class Client
4
+ def purge(urls)
5
+ if urls.is_a?(String)
6
+ cache_locations_for(urls).map{ |location| perform_purge(location, urls) }
7
+ elsif urls.is_a?(Array)
8
+ urls.map{ |url| purge(url) }
9
+ end
10
+ end
11
+
12
+ private
13
+
14
+ def perform_purge(location, url)
15
+ ip, port = location.split(/:/)
16
+ port = 80 if port.nil? || port.empty?
17
+ protocol, host, path = split_url(url)
18
+ req = HTTP::Purge.new(path)
19
+ req["Host"] = host
20
+ Net::HTTP.new(ip, port).start do |http|
21
+ http.request req
22
+ end
23
+ end
24
+
25
+ def split_url(url)
26
+ url.scan(/^([^\/]+)?\/\/([^\/ ]+) ?(.*)?$/).flatten
27
+ end
28
+
29
+ def cache_locations_for(url)
30
+ uri_host = split_url(url)[1]
31
+ SETTINGS.http_cache.each do |config|
32
+ return config.cache_locations if config.resource_hosts.any?{ |host| uri_host == host }
33
+ end
34
+ []
35
+ end
36
+ end
37
+ end
@@ -0,0 +1,12 @@
1
+ # encoding: UTF-8
2
+ require "net/http"
3
+
4
+ module Purgeable
5
+ module HTTP
6
+ class Purge < Net::HTTPRequest
7
+ METHOD = "PURGE"
8
+ REQUEST_HAS_BODY = false
9
+ RESPONSE_HAS_BODY = true
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,34 @@
1
+ # encoding: UTF-8
2
+ module Purgeable
3
+ module HttpResource
4
+ def self.included(base)
5
+ base.extend ClassMethods
6
+ end
7
+
8
+ def purgeable_urls
9
+ purge_generators.map{ |p| [p.is_a?(Symbol) ? send(p) : instance_eval(&p)] }.flatten.compact.uniq
10
+ end
11
+
12
+ protected
13
+
14
+ def perform_purge!
15
+ ::Purgeable::Client.new.purge(purgeable_urls)
16
+ end
17
+
18
+ def perform_purge
19
+ perform_purge! rescue []
20
+ end
21
+
22
+ private
23
+
24
+ def purge_generators
25
+ self.class.instance_variable_get(:@purge_generators) || []
26
+ end
27
+
28
+ module ClassMethods
29
+ def http_purge(method = nil, &block)
30
+ (@purge_generators ||= []) << (block_given? ? block : method)
31
+ end
32
+ end
33
+ end
34
+ end
@@ -0,0 +1,24 @@
1
+ # encoding: UTF-8
2
+ require "erb"
3
+ require "yaml"
4
+ require "methodize"
5
+
6
+ module Purgeable
7
+ SETTINGS = {}.extend ::Methodize
8
+
9
+ def self.load_settings file_path, env
10
+ raise "Invalid config location: #{file_path.inspect}" unless File.exists? file_path
11
+ config = YAML.load(ERB.new(File.read(file_path)).result)[env]
12
+ SETTINGS.merge! config if config.is_a?(Hash)
13
+ if defined? ::MethodizedHash
14
+ SETTINGS.http_cache.each do |cfg|
15
+ cfg.extend ::Methodize unless cfg.kind_of?(MethodizedHash)
16
+ end
17
+ end
18
+ end
19
+
20
+ if defined? ::Rails
21
+ config_file = ::Rails.root.join("config/purgeable.yml")
22
+ load_settings config_file, ::Rails.env if File.exists? config_file
23
+ end
24
+ end
data/lib/purgeable.rb ADDED
@@ -0,0 +1,8 @@
1
+ # encoding: UTF-8
2
+ module Purgeable
3
+ autoload :HttpResource, "purgeable/http_resource"
4
+ autoload :Client, "purgeable/client"
5
+ end
6
+
7
+ require "purgeable/http/purge"
8
+ require "purgeable/settings"
metadata ADDED
@@ -0,0 +1,134 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: purgeable
3
+ version: !ruby/object:Gem::Version
4
+ hash: 29
5
+ prerelease:
6
+ segments:
7
+ - 0
8
+ - 0
9
+ - 1
10
+ version: 0.0.1
11
+ platform: ruby
12
+ authors:
13
+ - Marcelo Manzan
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2012-03-14 00:00:00 Z
19
+ dependencies:
20
+ - !ruby/object:Gem::Dependency
21
+ name: methodize
22
+ prerelease: false
23
+ requirement: &id001 !ruby/object:Gem::Requirement
24
+ none: false
25
+ requirements:
26
+ - - ">="
27
+ - !ruby/object:Gem::Version
28
+ hash: 23
29
+ segments:
30
+ - 0
31
+ - 2
32
+ - 0
33
+ version: 0.2.0
34
+ type: :runtime
35
+ version_requirements: *id001
36
+ - !ruby/object:Gem::Dependency
37
+ name: rspec
38
+ prerelease: false
39
+ requirement: &id002 !ruby/object:Gem::Requirement
40
+ none: false
41
+ requirements:
42
+ - - ~>
43
+ - !ruby/object:Gem::Version
44
+ hash: 23
45
+ segments:
46
+ - 2
47
+ - 6
48
+ - 0
49
+ version: 2.6.0
50
+ type: :development
51
+ version_requirements: *id002
52
+ - !ruby/object:Gem::Dependency
53
+ name: mocha
54
+ prerelease: false
55
+ requirement: &id003 !ruby/object:Gem::Requirement
56
+ none: false
57
+ requirements:
58
+ - - ~>
59
+ - !ruby/object:Gem::Version
60
+ hash: 31
61
+ segments:
62
+ - 0
63
+ - 10
64
+ version: "0.10"
65
+ type: :development
66
+ version_requirements: *id003
67
+ - !ruby/object:Gem::Dependency
68
+ name: fakeweb
69
+ prerelease: false
70
+ requirement: &id004 !ruby/object:Gem::Requirement
71
+ none: false
72
+ requirements:
73
+ - - ~>
74
+ - !ruby/object:Gem::Version
75
+ hash: 27
76
+ segments:
77
+ - 1
78
+ - 3
79
+ - 0
80
+ version: 1.3.0
81
+ type: :development
82
+ version_requirements: *id004
83
+ description: Purge contents from shared caches easily by sending a HTTP method "PURGE".
84
+ email:
85
+ - manzan@gmail.com
86
+ executables: []
87
+
88
+ extensions: []
89
+
90
+ extra_rdoc_files: []
91
+
92
+ files:
93
+ - lib/purgeable.rb
94
+ - lib/purgeable/client.rb
95
+ - lib/purgeable/http/purge.rb
96
+ - lib/purgeable/http_resource.rb
97
+ - lib/purgeable/settings.rb
98
+ homepage: http://github.com/abril/
99
+ licenses: []
100
+
101
+ post_install_message:
102
+ rdoc_options: []
103
+
104
+ require_paths:
105
+ - lib
106
+ required_ruby_version: !ruby/object:Gem::Requirement
107
+ none: false
108
+ requirements:
109
+ - - ">="
110
+ - !ruby/object:Gem::Version
111
+ hash: 3
112
+ segments:
113
+ - 0
114
+ version: "0"
115
+ required_rubygems_version: !ruby/object:Gem::Requirement
116
+ none: false
117
+ requirements:
118
+ - - ">="
119
+ - !ruby/object:Gem::Version
120
+ hash: 23
121
+ segments:
122
+ - 1
123
+ - 3
124
+ - 6
125
+ version: 1.3.6
126
+ requirements: []
127
+
128
+ rubyforge_project:
129
+ rubygems_version: 1.8.15
130
+ signing_key:
131
+ specification_version: 3
132
+ summary: HTTP Purge utility
133
+ test_files: []
134
+