require 'rubygems'
require 'open-uri'
require 'json'
require "net/http"
require "uri"

uri = URI.parse("http://status.twilio.com/api/v1/services")
http = Net::HTTP.new(uri.host, uri.port)
request = Net::HTTP::Get.new(uri.request_uri)
response = http.request(request)

ok = nil
not_ok = nil

if response.code == "200"
	json = JSON.parse(response.body)

	json['services'].each do |line|
		if (line['current-event'] != nil)
			if (line['current-event']['status']["level"] != "NORMAL")
				not_ok =  "#{not_ok}"+" "+line['id']
			else
				ok =  "#{ok}"+" "+line['id']
			end
		end
		
	end
else
	puts "Unable to access http://status.twilio.com/api/v1/services"
	exit 2
end

if (not_ok != nil)
       puts "The following servies may have a problem: #{not_ok}"
       exit 2
else
       puts "The following services are OK: #{ok}"
       exit 0
end

