Skip to content

Instantly share code, notes, and snippets.

@jiphex
Created November 14, 2011 10:13
Show Gist options
  • Save jiphex/1363665 to your computer and use it in GitHub Desktop.
Save jiphex/1363665 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
@ekortright
Copy link

Thanks (seven years later). This works nicely with the rest-client gem.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment