-
Notifications
You must be signed in to change notification settings - Fork 24
/
flowdock.coffee
133 lines (112 loc) · 3.45 KB
/
flowdock.coffee
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
url = require 'url'
events = require 'events'
request = require 'request'
Stream = require './stream'
extend = (objects...) ->
result = {}
for object in objects
for key, value of object
result[key] = value
result
class Session extends events.EventEmitter
constructor: (@email, @password, @url = process.env.FLOWDOCK_API_URL || 'https://api.flowdock.com') ->
@auth = 'Basic ' + new Buffer(@email + ':' + @password).toString('base64')
flows: (callback) ->
@get('/flows', {users: 1}, callback)
# Start streaming flows given as argument using authentication credentials
#
# flows - Flow id or array of flow ids
# options - query string hash
#
# Returns Stream object
stream: (flows, options = {}) ->
flows = [flows] unless Array.isArray(flows)
Stream.connect @auth, flows, options
# Send message to Flowdock
send: (path, message, callback) ->
@post path, message, callback
# Send a chat message to Flowdock
message: (flowId, message, tags, callback) ->
data =
flow: flowId
event: 'message'
content: message
tags: tags || []
@send "/messages", data, callback
# Send a thread message to Flowdock
threadMessage: (flowId, threadId, message, tags, callback) ->
data =
flow: flowId
thread_id: threadId
event: 'message'
content: message
tags: tags || []
@send "/messages", data, callback
# Send a chat comment to Flowdock
comment: (flowId, parentId, comment, tags, callback) ->
data =
flow: flowId
message: parentId
event: 'comment'
content: comment
tags: tags || []
@send "/comments", data, callback
# Send a private message to Flowdock
privateMessage: (userId, message, tags, callback) ->
data =
event: 'message'
content: message
tags: tags || []
@send "/private/#{userId}/messages", data, callback
# Change status on Flowdock
status: (flowId, status, callback) ->
data =
event: 'status'
content: status
flow: flowId
@send "/messages", data, callback
# Invite a user to an organization's flow
invite: (flowId, organizationId, email, message, callback) ->
data =
email: email
message: message
@send "/flows/#{organizationId}/#{flowId}/invitations", data, callback
editMessage: (flowId, organizationId, messageId, data, callback) ->
@put "/flows/#{organizationId}/#{flowId}/messages/#{messageId}", data, callback
# API access
post: (path, data, cb) ->
@_request('post', path, data, cb)
get: (path, data, cb) ->
@_request('get', path, data, cb)
put: (path, data, cb) ->
@_request('put', path, data, cb)
delete: (path, cb) ->
@_request('delete', path, {}, cb)
_request: (method, path, data, cb) ->
uri = @baseURL()
uri.pathname = path
if method.toLowerCase() == 'get'
qs = data
data = {}
options =
uri: url.format(uri)
method: method
json: data
qs: qs
headers:
'Authorization': @auth
'Accept': 'application/json'
'Content-Type': 'application/json'
request options, (err, res, body) =>
if err
error = new Error('Couldn\'t connect to Flowdock:' + err.toString())
else if res.statusCode >= 300
error = new Error('Received status ' + res.statusCode)
if error?
@emit 'error', error
cb?(error)
else
cb?(null, body, res)
baseURL: () ->
url.parse(@url)
exports.Session = Session