purgeable 0.0.1 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1 @@
1
+ v0.1.0
@@ -0,0 +1,91 @@
1
+ # Purgeable
2
+
3
+ Purge contents from shared caches easily by sending a HTTP method "PURGE".
4
+
5
+ ## Configuration
6
+
7
+ Configuration file structure:
8
+
9
+ ```yaml
10
+ # ./purgeable.yml
11
+ sample: &sample
12
+ http_cache:
13
+ - resource_hosts:
14
+ - "www.example.com"
15
+ - "www2.example.com"
16
+ cache_locations:
17
+ - "varnish1.example.com"
18
+ - "varnish2.example.com"
19
+ - resource_hosts:
20
+ - "www3.example.com"
21
+ - "www4.example.com"
22
+ cache_locations:
23
+ - "varnish3.example.com"
24
+ - "varnish4.example.com"
25
+
26
+ production:
27
+ <<: *sample
28
+ ```
29
+
30
+ Configuration load example:
31
+
32
+ ```ruby
33
+ Purgeable.load_settings("./purgeable.yml", "production")
34
+ ```
35
+
36
+ ## Rails generators
37
+
38
+ Rails 2 generator:
39
+
40
+ ```bash
41
+ $ script/generate purgeable_install
42
+ create config/purgeable.yml
43
+ ```
44
+
45
+ Rails 3 generator:
46
+
47
+ ```bash
48
+ $ script/rails generate purgeable:install
49
+ create config/purgeable.yml
50
+ ```
51
+
52
+ ## Usage
53
+
54
+ Simple usage:
55
+
56
+ ```ruby
57
+ client = Purgeable::Client.new
58
+ urls = %w[
59
+ http://www.example.com/article/my-example
60
+ http://www3.example.com/article/id/1234
61
+ ]
62
+ client.purge(urls) # => {
63
+ # "http://www.example.com/article/my-example" => {
64
+ # "varnish1.example.com" => #<Net::HTTPOK 200 Purged readbody=true>,
65
+ # "varnish2.example.com" => #<Net::HTTPOK 200 Purged readbody=true>
66
+ # },
67
+ # "http://www3.example.com/article/id/1234" => {
68
+ # "varnish3.example.com" => #<Net::HTTPOK 200 Purged readbody=true>,
69
+ # "varnish4.example.com" => #<Net::HTTPOK 200 Purged readbody=true>
70
+ # },
71
+ # }
72
+ ```
73
+
74
+ Model helpers usage:
75
+
76
+ ```ruby
77
+ class Article < ActiveRecord::Base
78
+ include Purgeable::HttpResource
79
+
80
+ def resource_url
81
+ "http://www3.example.com/article/id/#{id}"
82
+ end
83
+
84
+ http_purge { "http://www.example.com/article/#{friendly_title}" }
85
+ http_purge :resource_url
86
+
87
+ after_save :perform_purge # send the HTTP PURGE to configured caches on save
88
+ end
89
+ ```
90
+
91
+ Enjoy it.
@@ -0,0 +1,27 @@
1
+ require 'rbconfig'
2
+
3
+ class PurgeableInstallGenerator < Rails::Generator::Base
4
+ def manifest
5
+ record do |m|
6
+ m.file "purgeable.yml", "config/purgeable.yml"
7
+ end
8
+ end
9
+
10
+ def self.gem_root
11
+ File.expand_path('../../../', __FILE__)
12
+ end
13
+
14
+ def self.source_root
15
+ File.join(gem_root, 'templates', 'install')
16
+ end
17
+
18
+ def source_root
19
+ self.class.source_root
20
+ end
21
+
22
+ private
23
+
24
+ def banner
25
+ "Usage: #{$0} purgeable_install"
26
+ end
27
+ end
@@ -0,0 +1,19 @@
1
+ require 'rbconfig'
2
+
3
+ module Purgeable
4
+ class InstallGenerator < ::Rails::Generators::Base
5
+
6
+ def generate
7
+ copy_file "purgeable.yml", "config/purgeable.yml"
8
+ end
9
+
10
+ def self.gem_root
11
+ File.expand_path("../../../../", __FILE__)
12
+ end
13
+
14
+ def self.source_root
15
+ File.join(gem_root, 'templates/install')
16
+ end
17
+
18
+ end
19
+ end
@@ -2,7 +2,9 @@
2
2
  module Purgeable
3
3
  autoload :HttpResource, "purgeable/http_resource"
4
4
  autoload :Client, "purgeable/client"
5
+ autoload :VERSION, "purgeable/version"
5
6
  end
6
7
 
7
8
  require "purgeable/http/purge"
8
9
  require "purgeable/settings"
10
+ require "purgeable/railtie" if defined?(::Rails) && defined?(::Rails::Railtie)
@@ -2,15 +2,22 @@
2
2
  module Purgeable
3
3
  class Client
4
4
  def purge(urls)
5
+ purge_all(urls)
6
+ end
7
+
8
+ private
9
+ def purge_all(urls, responses = {})
5
10
  if urls.is_a?(String)
6
- cache_locations_for(urls).map{ |location| perform_purge(location, urls) }
11
+ cache_locations_for(urls).each do |location|
12
+ next if (responses[urls] ||= {}).has_key?(location)
13
+ responses[urls][location] = perform_purge(location, urls)
14
+ end
7
15
  elsif urls.is_a?(Array)
8
- urls.map{ |url| purge(url) }
16
+ urls.each{ |url| purge_all(url, responses) }
9
17
  end
18
+ responses
10
19
  end
11
20
 
12
- private
13
-
14
21
  def perform_purge(location, url)
15
22
  ip, port = location.split(/:/)
16
23
  port = 80 if port.nil? || port.empty?
@@ -16,7 +16,7 @@ module Purgeable
16
16
  end
17
17
 
18
18
  def perform_purge
19
- perform_purge! rescue []
19
+ perform_purge! rescue {}
20
20
  end
21
21
 
22
22
  private
@@ -0,0 +1,14 @@
1
+ # encoding: UTF-8
2
+ module Purgeable
3
+ class Railtie < ::Rails::Railtie
4
+ initializer "setup http cache" do
5
+ config_file = ::Rails.root.join("config/purgeable.yml")
6
+ if File.exists? config_file
7
+ Purgeable.load_settings config_file, ::Rails.env
8
+ else
9
+ puts "Purgeable config not found. Create a config file at: config/purgeable.yml"
10
+ puts "to generate one run: rails generate purgeable:install"
11
+ end
12
+ end
13
+ end
14
+ end
@@ -6,19 +6,32 @@ require "methodize"
6
6
  module Purgeable
7
7
  SETTINGS = {}.extend ::Methodize
8
8
 
9
- def self.load_settings file_path, env
9
+ def self.load_settings(file_path, env)
10
10
  raise "Invalid config location: #{file_path.inspect}" unless File.exists? file_path
11
11
  config = YAML.load(ERB.new(File.read(file_path)).result)[env]
12
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
13
+ normalize_settings
18
14
  end
19
15
 
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
16
+ private
17
+
18
+ def self.normalize_settings
19
+ SETTINGS["http_cache"] = [] unless SETTINGS.http_cache.is_a?(Array)
20
+ SETTINGS.http_cache.each do |cfg|
21
+ next unless cfg.is_a?(Hash)
22
+ cfg.extend ::Methodize if defined?(::MethodizedHash) && !cfg.kind_of?(MethodizedHash)
23
+ cfg["cache_locations"] =
24
+ case cfg["cache_locations"]
25
+ when String then [cfg["cache_locations"]]
26
+ when Array then cfg["cache_locations"]
27
+ else []
28
+ end
29
+ cfg["resource_hosts"] =
30
+ case cfg["resource_hosts"]
31
+ when String then [cfg["resource_hosts"]]
32
+ when Array then cfg["resource_hosts"]
33
+ else []
34
+ end
35
+ end
23
36
  end
24
37
  end
@@ -0,0 +1,17 @@
1
+ # encoding: UTF-8
2
+ module Purgeable
3
+ version = nil
4
+ version_file = ::File.expand_path('../../../GEM_VERSION', __FILE__)
5
+ version = File.read(version_file) if version.nil? && ::File.exists?(version_file)
6
+ version = $1 if version.nil? && ::File.expand_path('../..', __FILE__) =~ /\/purgeable-(\d+(?:\.\d+)+)/
7
+ if version.nil? && ::File.exists?(::File.expand_path('../../../.git', __FILE__))
8
+ begin
9
+ require 'step-up'
10
+ version = ::StepUp::Driver::Git.new.last_version_tag("HEAD", true)
11
+ rescue
12
+ version = "v0.0.0"
13
+ end
14
+ ::File.open(version_file, "w"){ |f| f.write version }
15
+ end
16
+ VERSION = version.gsub(/^v?([^\+]+)\+?\d*$/, '\1')
17
+ end
@@ -0,0 +1,9 @@
1
+ # encoding: UTF-8
2
+ config_file = ::Rails.root.join("config/purgeable.yml")
3
+ if File.exists? config_file
4
+ Purgeable.load_settings config_file, ::Rails.env
5
+ else
6
+ puts "Purgeable config not found. Create a config file at: config/purgeable.yml"
7
+ puts "to generate one run: script/generate purgeable_install"
8
+ end
9
+
@@ -0,0 +1,23 @@
1
+ sample: &sample
2
+ http_cache:
3
+ - resource_hosts:
4
+ - "www1.example.com"
5
+ - "www2.example.com"
6
+ cache_locations:
7
+ - "varnish1.example.com"
8
+ - "varnish2.example.com"
9
+ - resource_hosts:
10
+ - "www3.example.com"
11
+ - "www4.example.com"
12
+ cache_locations:
13
+ - "varnish3.example.com"
14
+ - "varnish4.example.com"
15
+
16
+ development:
17
+ <<: *sample
18
+
19
+ test:
20
+ http_cache: []
21
+
22
+ production:
23
+ http_cache: []
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: purgeable
3
3
  version: !ruby/object:Gem::Version
4
- hash: 29
4
+ hash: 27
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
- - 0
9
8
  - 1
10
- version: 0.0.1
9
+ - 0
10
+ version: 0.1.0
11
11
  platform: ruby
12
12
  authors:
13
13
  - Marcelo Manzan
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2012-03-14 00:00:00 Z
18
+ date: 2012-03-16 00:00:00 Z
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency
21
21
  name: methodize
@@ -90,11 +90,19 @@ extensions: []
90
90
  extra_rdoc_files: []
91
91
 
92
92
  files:
93
+ - README.md
94
+ - generators/purgeable/purgeable_install_generator.rb
95
+ - lib/generators/purgeable/install_generator.rb
93
96
  - lib/purgeable.rb
94
97
  - lib/purgeable/client.rb
95
98
  - lib/purgeable/http/purge.rb
96
99
  - lib/purgeable/http_resource.rb
100
+ - lib/purgeable/railtie.rb
97
101
  - lib/purgeable/settings.rb
102
+ - lib/purgeable/version.rb
103
+ - rails/init.rb
104
+ - templates/install/purgeable.yml
105
+ - GEM_VERSION
98
106
  homepage: http://github.com/abril/
99
107
  licenses: []
100
108