-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsurvey.go
70 lines (59 loc) · 1.66 KB
/
survey.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
package iterate
import (
"encoding/json"
"fmt"
"net/url"
"time"
)
// Survey represents a survey.
type Survey struct {
Id string `json:"id,omitempty"`
Name string `json:"id,omitempty"`
Questions []interface{} `json:"questions,omitempty"`
}
// SendParams is the set of parameters that can be used when
// sending an email survey.
// For more details see: http://docs.iterate.apiary.io/#reference/0/surveysidsend/post
type SendParams struct {
Email string
FirstName string
LastName string
Delay time.Duration
Date time.Time
}
// ListSurveys returns a list of your surveys.
// For more details see: http://docs.iterate.apiary.io/#reference/0/surveys/get
func (c Client) ListSurveys() (surveys []Survey, err error) {
results, err := c.get("/surveys", url.Values{})
if err != nil {
return
}
err = json.Unmarshal(results, &surveys)
return
}
// EmailSurvey emails a survey to the specified email address with
// optional additional send parameters.
// For more details see: http://docs.iterate.apiary.io/#reference/0/surveysidsend/post
func (c Client) EmailSurvey(surveyId string, params SendParams) error {
path := fmt.Sprintf("/surveys/%s/send", surveyId)
values := url.Values{}
if params.Email != "" {
values.Add("email", params.Email)
}
if params.FirstName != "" {
values.Add("first_name", params.FirstName)
}
if params.LastName != "" {
values.Add("last_name", params.LastName)
}
delay := int(params.Delay.Seconds())
if delay > 0 {
values.Add("delay", fmt.Sprintf("%v", delay))
}
date := params.Date.Unix()
if date > 0 {
values.Add("date", fmt.Sprintf("%v", date))
}
_, err := c.post(path, values)
return err
}