Created
March 19, 2009 05:49
-
-
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
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 '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