Skip to content

Instantly share code, notes, and snippets.

@joho
Created March 19, 2009 05:49
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save joho/81616 to your computer and use it in GitHub Desktop.
A little ruby script to give you growl notifications when you get a new sale
require 'rubygems'
require 'open-uri'
require 'json'
USERNAME = "ryan"
API_KEY = "26k6otse2s586e4hcbzjy3quq830t3o4"
SITE = "flashden"
SLEEP_TIME = 60 * 5
api_url = "http://#{SITE}.net/api/edge/#{USERNAME}/#{API_KEY}/recent-sales+vitals.json"
def send_growl_notification(msg, title = "You\\'ve made a new sale")
system "growlnotify -n \"Envato API\" -m \"#{msg}\" #{title}"
end
previous_notified_ids = []
while(true)
begin
data = JSON.parse(open(api_url).read)
sales = data['recent-sales']
current_balance = data['vitals']['balance']
to_notify = sales.reject {|sale| previous_notified_ids.include? sale['id'].to_i}
to_notify.each do |sale|
send_growl_notification "Item \\\"#{sale['item']}\\\" sold, contributing \\$#{sale['amount']} to your total balance of \\$#{current_balance}"
end
previous_notified_ids = sales.collect {|sale| sale['id'].to_i}
rescue
send_growl_notification "There was an error talking to the API"
end
sleep SLEEP_TIME
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment