-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrallly.go
95 lines (78 loc) · 2.03 KB
/
rallly.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
package main
import (
"fmt"
"time"
)
type RalllyPollInitiator struct {
Name string `json:"name"`
Email string `json:"email"`
}
type RalllyPollCreate struct {
Title string `json:"title"`
Description string `json:"description"`
Options []RalllyDateOption `json:"options"`
User RalllyPollInitiator `json:"user"`
Timezone string `json:"timezone"`
}
type RalllyDateOption struct {
StartDate string `json:"startDate"`
EndDate string `json:"endDate,omitempty"`
}
type RalllyPollCreateRequest struct {
Root struct {
JSON RalllyPollCreate `json:"json"`
} `json:"0"`
}
type RalllyPollCreated struct {
ID string `json:"id"`
URLID string `json:"urlId"`
}
type RalllyPollCreatedResponse struct {
Result struct {
Data struct {
Json RalllyPollCreated `json:"json"`
} `json:"data"`
} `json:"result"`
}
func composeRalllyOptions() []RalllyDateOption {
options := make([]RalllyDateOption, 0, 7)
now := time.Now()
start := now
for start.Weekday() != time.Monday {
start = start.AddDate(0, 0, 1)
}
end := start.AddDate(0, 0, 7)
for start.Before(end) {
options = append(options, RalllyDateOption{
StartDate: start.Format("2006-01-02"),
})
start = start.AddDate(0, 0, 1)
}
return options
}
func newRalllyPollRequest() *RalllyPollCreateRequest {
return &RalllyPollCreateRequest{
Root: struct {
JSON RalllyPollCreate `json:"json"`
}{
JSON: RalllyPollCreate{
Title: composeTitle(),
Description: "Whelp, who could've thought that this would be so easy?",
Options: composeRalllyOptions(),
User: RalllyPollInitiator{
Name: "Your friendly bot",
Email: hostEmail,
},
Timezone: hostTimeZone,
},
},
}
}
func createRalllyPoll() (*RalllyPollCreated, error) {
var pollCreated []RalllyPollCreatedResponse
err := postJSON(fmt.Sprintf("https://%s/api/trpc/polls.create?batch=1", ralllyEndpoint), newRalllyPollRequest(), &pollCreated)
if err != nil {
return nil, err
}
return &pollCreated[0].Result.Data.Json, nil
}