Skip to content

Instantly share code, notes, and snippets.

@mikaa123
Created March 30, 2013 16:30
Show Gist options
  • Save mikaa123/5277344 to your computer and use it in GitHub Desktop.
Save mikaa123/5277344 to your computer and use it in GitHub Desktop.
Unmarshal JSON from API.
// vim:set sw=4 sts=4:
package main
import (
"io/ioutil"
"net/http"
"encoding/json"
"time"
"os/exec"
)
type Stats struct {
Users, Stories, Scenarios int
}
// Fills the provided Stats with data from the server.
func getStats(destination *Stats) (err error) {
var (
r *http.Response
b []byte
)
if r, err = http.Get("http://localhost:5000/api/stats"); err == nil {
if b, err = ioutil.ReadAll(r.Body); err == nil {
err = json.Unmarshal(b, &destination);
}
}
return err
}
func say(message string) {
cmd := exec.Command("say", message)
cmd.Output()
return
}
func main() {
var stats, buffer Stats
getStats(&stats);
for {
time.Sleep(1 * time.Second)
getStats(&buffer);
switch {
case stats.Users < buffer.Users:
say("New User!")
case stats.Stories < buffer.Stories:
say("New story!")
case stats.Scenarios < buffer.Scenarios:
say("New proposal!")
}
stats = buffer
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment