-
Notifications
You must be signed in to change notification settings - Fork 7
/
example_test.go
193 lines (158 loc) · 5 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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
package telegraph_test
import (
"log"
"time"
"source.toby3d.me/toby3d/telegraph"
)
// Content in a string format (for this example).
// Be sure to wrap every media in a <figure> tag, okay? Be easy.
const data = `
<figure>
<img src="/file/6a5b15e7eb4d7329ca7af.jpg"/>
</figure>
<p><i>Hello</i>, my name is <b>Page</b>, <u>look at me</u>!</p>
<figure>
<iframe src="https://youtu.be/fzQ6gRAEoy0"></iframe>
<figcaption>
Yes, you can embed youtube, vimeo and twitter widgets too!
</figcaption>
</figure>
`
var (
account *telegraph.Account //nolint:gochecknoglobals
page *telegraph.Page //nolint:gochecknoglobals
content []telegraph.Node //nolint:gochecknoglobals
)
func errCheck(err error) {
if err != nil {
log.Fatalln(err.Error())
}
}
func Example_fastStart() {
var err error
// Create new Telegraph account.
requisites := telegraph.Account{
ShortName: "toby3d", // required
// Author name/link can be epmty. So secure. Much anonymously. Wow.
AuthorName: "Maxim Lebedev", // optional
AuthorURL: "https://t.me/toby3d", // optional
}
account, err = telegraph.CreateAccount(requisites)
errCheck(err)
// Make sure that you have saved acc.AuthToken for create new pages or make
// other actions by this account in next time!
// Format content to []telegraph.Node array. Input data can be string, []byte
// or io.Reader.
content, err = telegraph.ContentFormat(data)
errCheck(err)
// Boom!.. And your text will be understandable for Telegraph. MAGIC.
// Create new Telegraph page
pageData := telegraph.Page{
Title: "My super-awesome page", // required
Content: content, // required
// Not necessarily, but, hey, it's just an example.
AuthorName: account.AuthorName, // optional
AuthorURL: account.AuthorURL, // optional
}
page, err = account.CreatePage(pageData, false)
errCheck(err)
// Show link from response on created page.
log.Println("Kaboom! Page created, look what happened:", page.URL)
}
func ExampleCreateAccount() {
var err error
account, err = telegraph.CreateAccount(telegraph.Account{
ShortName: "Sandbox",
AuthorName: "Anonymous",
})
errCheck(err)
log.Println("AccessToken:", account.AccessToken)
log.Println("AuthURL:", account.AuthorURL)
log.Println("ShortName:", account.ShortName)
log.Println("AuthorName:", account.AuthorName)
}
func ExampleAccount_EditAccountInfo() {
var err error
account, err = account.EditAccountInfo(telegraph.Account{
ShortName: "Sandbox",
AuthorName: "Anonymous",
})
errCheck(err)
log.Println("AuthURL:", account.AuthorURL)
log.Println("ShortName:", account.ShortName)
log.Println("AuthorName:", account.AuthorName)
}
func ExampleAccount_GetAccountInfo() {
info, err := account.GetAccountInfo(
telegraph.FieldShortName,
telegraph.FieldPageCount,
)
errCheck(err)
log.Println("ShortName:", info.ShortName)
log.Println("PageCount:", info.PageCount, "pages")
}
func ExampleAccount_RevokeAccessToken() {
var err error
// You must rewrite current variable with account structure for further usage.
account, err = account.RevokeAccessToken()
errCheck(err)
log.Println("AccessToken:", account.AccessToken)
}
func ExampleAccount_CreatePage() {
var err error
page, err = account.CreatePage(telegraph.Page{
Title: "Sample Page",
AuthorName: account.AuthorName,
Content: content,
}, true)
errCheck(err)
log.Println(page.Title, "by", page.AuthorName, "has been created!")
log.Println("PageURL:", page.URL)
}
func ExampleAccount_EditPage() {
var err error
page, err = account.EditPage(telegraph.Page{
Title: "Sample Page",
AuthorName: account.AuthorName,
Content: content,
}, true)
errCheck(err)
log.Println("Page on", page.Path, "path has been updated!")
log.Println("PageURL:", page.URL)
}
func ExampleGetPage() {
info, err := telegraph.GetPage("Sample-Page-12-15", true)
errCheck(err)
log.Println("Getted info about", info.Path, "page:")
log.Println("Author:", info.AuthorName)
log.Println("Views:", info.Views)
log.Println("CanEdit:", info.CanEdit)
}
func ExampleAccount_GetPageList() {
list, err := account.GetPageList(0, 3)
errCheck(err)
log.Println("Getted", list.TotalCount, "pages")
for i := range list.Pages {
p := list.Pages[i]
log.Printf("%s: %s\n~ %s\n\n", p.Title, p.URL, p.Description)
}
}
func ExampleGetViews() {
pagePath := "Sample-Page-12-15"
dateTime := time.Date(2016, time.December, 0, 0, 0, 0, 0, time.UTC)
views, err := telegraph.GetViews(pagePath, dateTime)
errCheck(err)
log.Println(pagePath, "has been viewed", views.Views, "times")
}
func ExampleContentFormat() {
const data = `<figure>
<img src="http://telegra.ph/file/6a5b15e7eb4d7329ca7af.jpg" /></figure>
<p><i>Hello</i>, my name is <b>Page</b>, <u>look at me</u>!</p>
<figure><iframe src="https://youtu.be/fzQ6gRAEoy0"></iframe>
<figcaption>Yes, you can embed youtube, vimeo and twitter widgets too!</figcaption>
</figure>`
var err error
content, err = telegraph.ContentFormat(data)
errCheck(err)
log.Printf("Content: %#v", content)
}