-
Notifications
You must be signed in to change notification settings - Fork 0
/
gist.go
47 lines (42 loc) · 1.16 KB
/
gist.go
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
package gists
import (
"encoding/json"
"fmt"
"log"
"strings"
)
// Gist represents a single Github gist
type Gist struct {
ID string `json:"id,omitempty"`
URL string `json:"url,omitempty"`
Description string `json:"description,omitempty"`
CreatedAt string `json:"created_at"`
Files map[string]GistFile `json:"files,omitempty"`
}
// GetGists accepts a string containing a JSON array of gists
// and passes the individual gists back through a channel.
func GetGists(jsonstr string) chan Gist {
ch := make(chan Gist, 10)
go func() {
defer close(ch)
var gists []Gist
err := json.Unmarshal([]byte(jsonstr), &gists)
if err != nil {
log.Fatal(err)
}
for _, gist := range gists {
ch <- gist
}
}()
return ch
}
// String creates a string representation of a gist.
func (g Gist) String() string {
parts := make([]string, 0)
parts = append(parts, fmt.Sprintf("ID: %q", g.ID))
parts = append(parts, fmt.Sprintf("Description: %q", g.Description))
for _, gistFile := range g.Files {
parts = append(parts, fmt.Sprintf("File:\n\t%s", gistFile))
}
return strings.Join(parts, "\n")
}