/
rules.go
130 lines (102 loc) · 3 KB
/
rules.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
package rules
import (
"encoding/json"
"net/url"
"dev.freespoke.com/twitter-stream/httpclient"
)
type (
//IRules is the interface the rules struct implements.
IRules interface {
Create(rules CreateRulesRequest, dryRun bool) (*TwitterRuleResponse, error)
Delete(req DeleteRulesRequest, dryRun bool) (*TwitterRuleResponse, error)
Get() (*TwitterRuleResponse, error)
}
//AddRulesRequest
//TwitterRuleResponse is what is returned from twitter when adding or deleting a rule.
TwitterRuleResponse struct {
Data []DataRule
Meta MetaRule
Errors []ErrorRule
}
//DataRule is what is returned as "Data" when adding or deleting a rule.
DataRule struct {
Value string `json:"Value"`
Tag string `json:"Tag"`
Id string `json:"id"`
}
//MetaRule is what is returned as "Meta" when adding or deleting a rule.
MetaRule struct {
Sent string `json:"sent"`
Summary MetaSummary `json:"summary"`
}
//MetaSummary is what is returned as "Summary" in "Meta" when adding or deleting a rule.
MetaSummary struct {
Created uint `json:"created"`
NotCreated uint `json:"not_created"`
}
//ErrorRule is what is returned as "Errors" when adding or deleting a rule.
ErrorRule struct {
Value string `json:"Value"`
Id string `json:"id"`
Title string `json:"title"`
Type string `json:"type"`
}
rules struct {
httpClient httpclient.IHttpClient
}
)
// NewRules creates a "rules" instance. This is used to create Twitter Filtered Stream rules.
// https://developer.twitter.com/en/docs/twitter-api/tweets/filtered-stream/integrate/build-a-rule.
func NewRules(httpClient httpclient.IHttpClient) IRules {
return &rules{httpClient: httpClient}
}
// Create will create new twitter streaming rules.
func (t *rules) Create(rules CreateRulesRequest, dryRun bool) (*TwitterRuleResponse, error) {
body, err := json.Marshal(rules)
if err != nil {
return nil, err
}
res, err := t.httpClient.AddRules(t.addDryRun(dryRun), string(body))
if err != nil {
return nil, err
}
defer res.Body.Close()
data := new(TwitterRuleResponse)
err = json.NewDecoder(res.Body).Decode(data)
return data, err
}
// Delete will delete rules twitter rules by their id.
func (t *rules) Delete(req DeleteRulesRequest, dryRun bool) (*TwitterRuleResponse, error) {
body, err := json.Marshal(req)
if err != nil {
return nil, err
}
res, err := t.httpClient.AddRules(t.addDryRun(dryRun), string(body))
if err != nil {
return nil, err
}
defer res.Body.Close()
data := new(TwitterRuleResponse)
err = json.NewDecoder(res.Body).Decode(data)
return data, err
}
// Get will fetch the current rules.
func (t *rules) Get() (*TwitterRuleResponse, error) {
res, err := t.httpClient.GetRules()
if err != nil {
return nil, err
}
defer res.Body.Close()
data := new(TwitterRuleResponse)
json.NewDecoder(res.Body).Decode(data)
return data, nil
}
func (t *rules) addDryRun(dryRun bool) *url.Values {
if dryRun {
query := new(url.URL).Query()
query.Add("dry_run", "true")
return &query
} else {
return nil
}
}