Loop in template

master
Michael Dvorkin 12 years ago
parent 5fe6f51555
commit 7b96ba9443
  1. 108
      lib/google.go
  2. 7
      mop.go

@ -3,40 +3,68 @@
package mop package mop
import ( import (
// "fmt"
"bytes" "bytes"
"encoding/json" "encoding/json"
"io/ioutil" // "io/ioutil"
"net/http" // "net/http"
// "strings"
) )
const real_time_url = "http://finance.google.com/finance/info?client=ig&q=" const real_time_url = "http://finance.google.com/finance/info?client=ig&q="
const body = `
// body := ` // [
// // [ {
// { "id": "665300"
// "id": "22144" ,"t" : "COH"
// ,"t" : "AAPL" ,"e" : "NYSE"
// ,"e" : "NASDAQ" ,"l" : "56.54"
// ,"l" : "416.84" ,"l_cur" : "56.54"
// ,"l_cur" : "416.84" ,"s": "0"
// ,"s": "2" ,"ltt":"4:01PM EDT"
// ,"ltt":"4:00PM EDT" ,"lt" : "Jun 21, 4:01PM EDT"
// ,"lt" : "Jun 20, 4:00PM EDT" ,"c" : "-0.75"
// ,"c" : "-6.16" ,"cp" : "-1.31"
// ,"cp" : "-1.46" ,"ccol" : "chr"
// ,"ccol" : "chr" }
// ,"el": "416.74" ,{
// ,"el_cur": "416.74" "id": "353353"
// ,"elt" : "Jun 20, 7:16PM EDT" ,"t" : "ATVI"
// ,"ec" : "-0.10" ,"e" : "NASDAQ"
// ,"ecp" : "-0.02" ,"l" : "13.55"
// ,"eccol" : "chr" ,"l_cur" : "13.55"
// ,"div" : "3.05" ,"s": "0"
// ,"yld" : "2.93" ,"ltt":"3:59PM EDT"
// } ,"lt" : "Jun 21, 3:59PM EDT"
// ]` ,"c" : "-0.33"
,"cp" : "-2.38"
,"ccol" : "chr"
}
,{
"id": "17154"
,"t" : "HPQ"
,"e" : "NYSE"
,"l" : "24.15"
,"l_cur" : "24.15"
,"s": "0"
,"ltt":"4:01PM EDT"
,"lt" : "Jun 21, 4:01PM EDT"
,"c" : "-0.57"
,"cp" : "-2.31"
,"ccol" : "chr"
}
,{
"id": "18241"
,"t" : "IBM"
,"e" : "NYSE"
,"l" : "195.46"
,"l_cur" : "195.46"
,"s": "0"
,"ltt":"4:02PM EDT"
,"lt" : "Jun 21, 4:02PM EDT"
,"c" : "-1.89"
,"cp" : "-0.96"
,"ccol" : "chr"
}
]`
type Message struct { type Message struct {
Ticker string `json:"t"` Ticker string `json:"t"`
@ -46,18 +74,22 @@ type Message struct {
func Quote(ticker string) []Message { func Quote(ticker string) []Message {
// Send the request. // Send the request.
response, err := http.Get(real_time_url + ticker) // response, err := http.Get(real_time_url + ticker)
if err != nil { // if err != nil {
panic(err) // panic(err)
} // }
//
// Fetch response and get its body. // // Fetch response and get its body.
defer response.Body.Close() // defer response.Body.Close()
body, err := ioutil.ReadAll(response.Body) // body, err := ioutil.ReadAll(response.Body)
//
// // Parse JSON.
// var message []Message
// err = json.Unmarshal(sanitize(body, &message)
// Parse JSON. // Parse JSON.
var message []Message var message []Message
err = json.Unmarshal(sanitize(body), &message) err := json.Unmarshal(sanitize([]byte(body)), &message)
if err != nil { if err != nil {
panic(err) panic(err)
} }

@ -107,9 +107,8 @@ func main() {
} }
defer termbox.Close() defer termbox.Close()
markup := `<green>line 1</green>: <white>Hello</white> world markup := `{{range .}}<green>{{.Ticker}}</green> ${{.LastTrade}} <red>{{.Change}}</red>
<green>line 2</green>: <white>Hello</white> again {{end}}...`
<green>line 3</green>: <white>Hello</white> one more time :-)`
template, err := template.New("screen").Parse(markup) template, err := template.New("screen").Parse(markup)
if err != nil { if err != nil {
@ -117,7 +116,7 @@ func main() {
} }
buffer := new(bytes.Buffer) buffer := new(bytes.Buffer)
err = template.Execute(buffer, nil) err = template.Execute(buffer, message)
if err != nil { if err != nil {
panic(err) panic(err)
} }

Loading…
Cancel
Save