Skip to content

Instantly share code, notes, and snippets.

@mk-qi
Forked from jiphex/purge.rb
Created June 1, 2012 05:54
Show Gist options
  • Save mk-qi/2849308 to your computer and use it in GitHub Desktop.
Save mk-qi/2849308 to your computer and use it in GitHub Desktop.
Ruby Varnish Purge Script
#!/usr/bin/env ruby
# Ruby varnish purger
require 'net/http'
module Net
class HTTP::Purge < HTTPRequest
METHOD='PURGE'
REQUEST_HAS_BODY = false
RESPONSE_HAS_BODY = true
end
end
reqarg = ARGV[0]
if !(reqarg =~ /^http:\/\//)
reqarg = "http://#{reqarg}"
end
uri = URI(reqarg)
if uri.scheme != "http"
puts "Not a HTTP URL! Try with http://....?"
Process.exit(1)
end
puts "Purging: #{uri.to_s}"
Net::HTTP.start(uri.host,uri.port) do |http|
presp = http.request Net::HTTP::Purge.new uri.request_uri
puts "#{presp.code}: #{presp.message}"
unless (200...400).include?(presp.code.to_i)
STDERR.puts "A problem occurred. PURGE was not performed."
Process.exit(1)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment