Skip to content

Instantly share code, notes, and snippets.

@ttscoff
Created April 11, 2013 20:41
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save ttscoff/5367004 to your computer and use it in GitHub Desktop.
Save ttscoff/5367004 to your computer and use it in GitHub Desktop.
OS X CLI for gaug.es stats
#!/usr/bin/ruby
require 'rubygems'
require 'json'
require 'net/https'
class KeyChain
def initialize(user=nil)
@user ||= %x{dscl . -read /Users/$(whoami)|grep "EMailAddress:"|sed 's/^EMailAddress: //'}
@host="com.brettterpstra.gauges"
@path="/secure"
end
def add_key(pass)
%x{security add-internet-password -a "#{@user}" -s "#{@host}" -p "#{@path}" -w "#{pass}"}
end
def find_key
result = %x{security find-internet-password -g -a "#{@user}" -s "#{@host}" -p "#{@path}" 2>&1 >/dev/null}
result =~ /^password: "(.*)"$/ ? $1 : nil
end
end
def key
kc = KeyChain.new
k = kc.find_key
if k.nil?
puts "This script requires an API key from gaug.es"
puts "Please log in to your Gauges account and visit"
puts "https://secure.gaug.es/dashboard#/account/clients"
puts "Create an app and paste the key below. The key"
puts "will be stored in your secure keychain."
puts
print "Client key: "
k = nil
until k =~ /^\w+$/
k = STDIN.gets.chomp
end
kc.add_key(k)
end
k
end
def gauges_api_call(key,type)
type.gsub!(/https:\/\/secure.gaug.es\//,'') if type =~ /^https:/
res = nil
begin
uri = URI.parse("https://secure.gaug.es/#{type}")
http = Net::HTTP.new(uri.host, uri.port)
http.use_ssl = true
http.verify_mode = OpenSSL::SSL::VERIFY_NONE
request = Net::HTTP::Get.new(uri.request_uri)
request.add_field("X-Gauges-Token", "#{key}")
res = http.request(request)
rescue Exception => e
p e
end
return false if res.nil?
JSON.parse(res.body)
end
json = gauges_api_call(key,"gauges")
gauges = []
json['gauges'].each {|g|
gauge = {}
gauge['title'] = g['title']
gauge['visits'] = g['today']['people']
gauge['views'] = g['today']['views']
gauges.push(gauge)
}
RED = "\033[31m"
GREEN = "\033[32m"
YELLOW = "\033[33m"
BLUE = "\033[34m"
MAGENTA = "\033[35m"
CYAN = "\033[1;36m"
WHITE = "\033[1;37m"
DEFAULT="\033[0;39m"
output = ""
if ARGV.length > 0
gauges.each { |gauge|
if ARGV[0].downcase == gauge['title'].downcase
if ARGV[1] && ARGV[1] == 'views'
output += gauge['views'].to_s
else
output += gauge['visits'].to_s
end
end
}
else
gauges.each { |gauge|
output += "#{CYAN}#{gauge['title']}: #{WHITE}#{gauge['visits']}/#{gauge['views']}\n\033[0m"
}
end
print output
@jamesstout
Copy link

The \033[0m on line 96 displays nothing when running from the command line (just the newline), but when running from within GeekTool I see a black "[0m" on a new line:

GeekTool

Deleting \033[0m gets rid of it in GeekTool - the newline is still output when running from the command line. I'm no expert on ANSI escape sequences - what does that one do?

Thanks
James

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment