dennybaa
3/28/2012 - 10:24 AM

check

check

#!/usr/bin/env ruby

require 'net/http'
require 'net/https'

CONNECT_TIMEOUT = 3
READ_TIMEOUT = 5
SLEEP = 5
TRIES = 3

URL='https://secure.payture.com'

def http_start(url)
  uri=URI(url)
  http = Net::HTTP.new(uri.host, uri.port)
  http.open_timeout = CONNECT_TIMEOUT
  http.read_timeout = READ_TIMEOUT
  if uri.scheme == 'https'
    http.use_ssl = true
    http.verify_mode = OpenSSL::SSL::VERIFY_NONE
  end
  block_given? ? yield(http) : http
end

def error(msg)
  puts "[E] #{msg}"
end

def info(msg)
  puts "[I] #{msg}"
end

success = false
(1..TRIES).each do |i|
  res = nil
  begin
    res = http_start(URL) { |http| http.head('/') }
    if res.is_a? Net::HTTPSuccess
      success = true
      break
    end
  rescue Exception => e
    error(e.message)
    if i == TRIES
      error("All #{TRIES} attempts have failed")
      exit(1)
    end
  end
  sleep(SLEEP)
end

if success
  info("Successfuly checked secure.payture.com")
else
  error("Failed to check secure.payture.com")
  exit(1)
end