public
Last active

OS X CLI for gaug.es stats

  • Download Gist
gauges.rb
Ruby
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100
#!/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

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

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.