-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexample_test.go
67 lines (57 loc) · 1.83 KB
/
example_test.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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
package gravatar_test
import (
"bytes"
"fmt"
gr "github.com/ftrvxmtrx/gravatar"
"image"
_ "image/png"
)
func ExampleGetAvatar() {
// get avatar image (128x128) using HTTP transport
emailHash := gr.EmailHash("[email protected]")
raw, err := gr.GetAvatar("http", emailHash, 128)
// get avatar image (32x32) using HTTP transport
// allow images of any rating level
raw, err = gr.GetAvatar("http", emailHash, gr.RatingX, 32)
// get avatar image (default size, png format) with fallback to "retro"
// generated avatar.
// use HTTPS transport
emailHash = "cfcd208495d565ef66e7dff9f98764da.png"
raw, err = gr.GetAvatar("https", emailHash, gr.DefaultRetro)
if err == nil {
var cfg image.Config
var format string
rawb := bytes.NewReader(raw)
cfg, format, err = image.DecodeConfig(rawb)
fmt.Println(cfg, format)
}
}
func ExampleGetAvatarURL() {
// get URL to avatar image of size 256x256
// fall back to "monster" generated avatar
emailHash := gr.EmailHash("[email protected]")
url := gr.GetAvatarURL("https", emailHash, gr.DefaultMonster, 256)
fmt.Println(url.String())
}
func ExampleGetProfile() {
// get profile using HTTPS transport
emailHash := gr.EmailHash("[email protected]")
profile, err := gr.GetProfile("https", emailHash)
if err == nil {
fmt.Println(profile.PreferredUsername)
fmt.Println(profile.ProfileUrl)
}
}
func ExampleSetAvatarURLOptions() {
// get URL to avatar image of default size
emailHash := gr.EmailHash("[email protected]")
url := gr.GetAvatarURL("https", emailHash)
fmt.Printf("default URL: %s", url.String())
// set size to 256x256
// fall back to "monster" generated avatar
gr.SetAvatarURLOptions(url, gr.DefaultMonster, 256)
fmt.Printf("modified URL: %s", url.String())
// reset back to the default one
gr.SetAvatarURLOptions(url)
fmt.Printf("URL after reset: %s", url.String())
}