forked from brettbuddin/victor
/
room.go
70 lines (51 loc) · 1.44 KB
/
room.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 campfire
import (
"encoding/json"
"errors"
"io/ioutil"
"strconv"
)
type Room struct {
client *Client
Id int `json:"id"`
Name string `json:"name"`
Users []*User `json:"users"`
}
func (self *Room) Show() (*Room, error) {
resp, err := self.client.Get("/room/" + strconv.Itoa(self.Id) + ".json")
if err != nil {
return nil, err
}
out, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, err
}
var fetch struct {
Room *Room `json:"room"`
}
err = json.Unmarshal(out, &fetch)
if err != nil {
return nil, err
}
return fetch.Room, nil
}
func (self *Room) Join() error {
resp, err := self.client.Post("/room/"+strconv.Itoa(self.Id)+"/join", "")
if err == nil && resp.StatusCode == 200 {
return nil
}
return errors.New("Could not join room.")
}
func (self *Room) Stream(channel chan *Message) {
self.client.Stream(self.Id, channel)
}
func (self *Room) Say(message string) {
msg := &MessageWrapper{Message: &Message{Type: "TextMessage", Body: message}}
buf, _ := json.Marshal(msg)
self.client.Post("/room/"+strconv.Itoa(self.Id)+"/speak", string(buf))
}
func (self *Room) Paste(message string) {
msg := &MessageWrapper{Message: &Message{Type: "PasteMessage", Body: message}}
buf, _ := json.Marshal(msg)
self.client.Post("/room/"+strconv.Itoa(self.Id)+"/speak", string(buf))
}