Created
August 9, 2012 21:46
-
-
Save mattgaidica/3308319 to your computer and use it in GitHub Desktop.
Simple Status Ticker for API Endpoints
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'HTTParty' | |
require 'terminal-display-colors' | |
require 'json' | |
# set your endpoints! | |
endpoints = { | |
"Google" => "http://google.com", | |
"Twitter" => "http://twitter.com" | |
} | |
# seconds to sleep between pings | |
sleep_seconds = 10 | |
buffer = [] | |
def diff start, finish | |
((finish-start) * 1000.0).round(2) | |
end | |
def print_flush str | |
print str | |
$stdout.flush | |
end | |
while(true) | |
# clear terminal | |
buffer << "\e[H\e[2J" | |
response_times = [] | |
# loop endpoints | |
endpoints.each do |name, url| | |
begin | |
t1 = Time.now | |
response = HTTParty.get url | |
t2 = Time.now | |
if response.code == 200 | |
response_times << diff(t1,t2) | |
buffer << "#{name.upcase} : " + response.message.green + " (#{response_times.last}ms)".yellow | |
else | |
buffer << "#{name.upcase} : " + response.message.red | |
end | |
rescue => e | |
buffer << "#{name.upcase} : " + 'Connection Error'.red | |
end | |
end | |
buffer << "\n" | |
# display avg and max response times | |
unless response_times.empty? | |
buffer << "avg : #{(response_times.inject{ |sum, el| sum + el }.to_f / response_times.size).round(2)}ms".cyan | |
buffer << "max : #{response_times.max}ms".cyan | |
else | |
buffer << 'No response times.'.cyan | |
end | |
# send buffered text to terminal | |
puts buffer.join("\n") | |
buffer.clear | |
# sleep | |
sleep_seconds.times do | |
sleep 1 | |
print_flush '.' | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment