Skip to content

Instantly share code, notes, and snippets.

@jimjh

jimjh/monitor.rb

Created May 24, 2013
Embed
What would you like to do?
Simple script to verify HTML content on enteract.in
# ~*~ encoding: utf-8 ~*~
require 'faraday'
require 'logger'
require 'uri'
require 'base64'
require 'fileutils'
BOOTHS = ['Alpha Chi Omega', 'Alpha Epsilon Pi', 'Alpha Kappa Psi', 'Alpha Phi', 'Alpha Phi Omega', 'Asian Students', 'BioSAC', 'Delta Delta Delta', 'Delta Gamma', 'Delta Tau Delta', 'Delta Upsilon', 'Fringe', 'Kappa Alpha Theta', 'Kappa Alpha Theta', 'Kappa Kappa Gamma', 'Kappa Sigma', 'KGB', 'Mayur', 'Mudge', 'Sigma Phi Epsilon', 'Singapore Students', 'Spirit', 'Stever', 'Students Dormitory Council', 'Taiwanese Students']
INTERVAL = 15 * 60
# setup basic logging
LOGS_DIR = '/mnt/log/html-validation'
FileUtils.mkdir_p LOGS_DIR
log_file = File.basename($0, File.extname($0))
@logger = Logger.new "#{LOGS_DIR}/#{log_file}"
def assert_status(response, expected)
url = response.env[:url].to_s
actual = response.status
raise "Expected #{expected}, was #{actual} from #{url}" unless actual == expected
end
def get_sign_in_token
response = @conn.get '/users/sign_in'
assert_status response, 200
raise 'CSRF Token not found' unless response.body.match(/meta content="([^"]+)" name="csrf-token"/)
@token = $1
@cookie = response.headers['set-cookie'].split('; ').first
end
def sign_in
response = @conn.post '/users/sign_in?user%5Bemail%5D=codex.is.poetry%40gmail.com&user%5Bpassword%5D=rw3rumoy87', { authenticity_token: @token } do |req|
req.headers['Accept'] = 'text/html'
req.headers['Content-Type'] = 'application/x-www-form-urlencoded'
req.headers['Cookie'] = @cookie
end
assert_status response, 302
@cookie = response.headers['set-cookie'].split('; ').first
end
def get_home_page
response = @conn.get '/' do |req|
req.headers['Accept'] = 'text/html'
req.headers['Cookie'] = @cookie
end
assert_status response, 200
missing = BOOTHS.map { |booth| (response.body.include? booth) ? nil : booth }.compact
raise "#{missing.join ', '} are missing from the home page." unless missing.empty?
end
loop do
begin
@conn = Faraday.new(url: 'http://enteract.in') do |c|
c.request :url_encoded
c.use Faraday::Response::Logger, @logger
c.adapter Faraday.default_adapter
end
get_sign_in_token
sign_in
get_home_page
rescue => e
@logger.error e.message
if system('aws', *%W[sns publish --topic-arn arn:aws:sns:us-east-1:xxxxxx:carnival-emergency --message #{e.message}])
@logger.info "Alerted SNS"
else @logger.error "Unable to alert SNS"
end
end
sleep INTERVAL
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment