aboutsummaryrefslogtreecommitdiffstats
path: root/lib/quiet_opener.rb
diff options
context:
space:
mode:
Diffstat (limited to 'lib/quiet_opener.rb')
-rw-r--r--lib/quiet_opener.rb34
1 files changed, 34 insertions, 0 deletions
diff --git a/lib/quiet_opener.rb b/lib/quiet_opener.rb
new file mode 100644
index 000000000..a077ca323
--- /dev/null
+++ b/lib/quiet_opener.rb
@@ -0,0 +1,34 @@
+require 'open-uri'
+require 'net-purge'
+
+def quietly_try_to_open(url)
+ begin
+ result = open(url).read.strip
+ rescue OpenURI::HTTPError, SocketError, Errno::ETIMEDOUT, Errno::ECONNREFUSED, Errno::EHOSTUNREACH
+ logger.warn("Unable to open third-party URL #{url}")
+ result = ""
+ end
+ return result
+end
+
+def quietly_try_to_purge(host, url)
+ begin
+ result = ""
+ result_body = ""
+ Net::HTTP.start(host) {|http|
+ request = Net::HTTP::Purge.new(url)
+ response = http.request(request)
+ result = response.code
+ result_body = response.body
+ }
+ rescue OpenURI::HTTPError, SocketError, Errno::ETIMEDOUT, Errno::ECONNREFUSED, Errno::EHOSTUNREACH
+ logger.warn("Unable to reach host #{host}")
+ end
+ if result == "200"
+ logger.info("Purged URL #{url} at #{host}: #{result}")
+ else
+ logger.warn("Unable to purge URL #{url} at #{host}: status #{result}")
+ end
+ return result
+end
+