Skip to content

Commit 1b771bc

Browse files
committed
Allow sending private messages using Client#send_message
1 parent caa5df2 commit 1b771bc

File tree

2 files changed

+98
-45
lines changed

2 files changed

+98
-45
lines changed

lib/wonder_llama/client.rb

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,16 @@ def register_event_queue
3232
last_event_id: response['last_event_id'])
3333
end
3434

35-
def send_message(content:, to:, topic:, type:)
36-
params = { content: content, to: to, topic: topic, type: type }
35+
def send_message(content:, to:, topic: nil)
36+
type = topic ? Message::STREAM_TYPE : Message::PRIVATE_TYPE
37+
38+
params = {
39+
content: content,
40+
to: to.is_a?(Array) ? JSON.generate(to) : to,
41+
topic: topic,
42+
type: type
43+
}
44+
3745
response = post(path: '/api/v1/messages', params: params)
3846

3947
message_params = { content: content, id: response['id'], to: to, topic: topic, type: type }

spec/client_spec.rb

Lines changed: 88 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -185,59 +185,104 @@
185185
end
186186

187187
describe '#send_message' do
188-
subject do
189-
client.send_message(content: 'Hello world!', to: 'greetings', topic: 'hello', type: 'stream')
190-
end
188+
context 'when sending a message to a stream' do
189+
context 'with a stream ID'
191190

192-
context 'when the request succeeds' do
193-
before do
194-
response_body = {
195-
id: 1,
196-
msg: '',
197-
result: 'success'
198-
}
191+
context 'with a stream name' do
192+
subject { client.send_message(content: 'Hello world!', to: 'greetings', topic: 'hello') }
199193

200-
stub_request(:post, 'https://test.example.com/api/v1/messages').
201-
with(body: { content: 'Hello world!', to: 'greetings', topic: 'hello', type: 'stream' }).
202-
to_return(body: JSON.generate(response_body))
203-
end
194+
context 'when the request succeeds' do
195+
before do
196+
response_body = {
197+
id: 1,
198+
msg: '',
199+
result: 'success'
200+
}
204201

205-
it 'returns a message' do
206-
expect(subject.content).to eq('Hello world!')
207-
expect(subject.id).to eq(1)
208-
expect(subject.to).to eq('greetings')
209-
expect(subject.topic).to eq('hello')
210-
expect(subject.type).to eq('stream')
211-
end
212-
end
202+
stub_request(:post, 'https://test.example.com/api/v1/messages').
203+
with(body: { content: 'Hello world!', to: 'greetings',
204+
topic: 'hello', type: 'stream' }).
205+
to_return(body: JSON.generate(response_body))
206+
end
207+
208+
it 'returns a stream message' do
209+
expect(subject).to be_stream
210+
expect(subject.content).to eq('Hello world!')
211+
expect(subject.id).to eq(1)
212+
expect(subject.to).to eq('greetings')
213+
expect(subject.topic).to eq('hello')
214+
end
215+
end
213216

214-
context 'when the Zulip server returns an error' do
215-
before do
216-
response_body = {
217-
code: 'STREAM_DOES_NOT_EXIST',
218-
msg: 'Stream \'greetings\' does not exist',
219-
result: 'error',
220-
stream: 'greetings'
221-
}
217+
context 'when the Zulip server returns an error' do
218+
before do
219+
response_body = {
220+
code: 'STREAM_DOES_NOT_EXIST',
221+
msg: 'Stream \'greetings\' does not exist',
222+
result: 'error',
223+
stream: 'greetings'
224+
}
222225

223-
stub_request(:post, 'https://test.example.com/api/v1/messages').
224-
with(body: { content: 'Hello world!', to: 'greetings', topic: 'hello', type: 'stream' }).
225-
to_return(body: JSON.generate(response_body))
226-
end
226+
stub_request(:post, 'https://test.example.com/api/v1/messages').
227+
with(body: { content: 'Hello world!', to: 'greetings', topic: 'hello', type: 'stream' }).
228+
to_return(body: JSON.generate(response_body))
229+
end
227230

228-
it 'raises ZulipError' do
229-
expect { subject }.to raise_error(WonderLlama::ZulipError,
230-
'Stream \'greetings\' does not exist')
231+
it 'raises ZulipError' do
232+
expect { subject }.to raise_error(WonderLlama::ZulipError,
233+
'Stream \'greetings\' does not exist')
234+
end
235+
end
236+
237+
context 'when the API credentials are invalid' do
238+
before do
239+
stub_request(:post, 'https://test.example.com/api/v1/messages').
240+
to_return(status: 401)
241+
end
242+
243+
include_examples 'raises AuthorizationError'
244+
end
231245
end
232246
end
233247

234-
context 'when the API credentials are invalid' do
235-
before do
236-
stub_request(:post, 'https://test.example.com/api/v1/messages').
237-
to_return(status: 401)
238-
end
248+
context 'when sending a private message' do
249+
context 'with a list of user IDs'
239250

240-
include_examples 'raises AuthorizationError'
251+
context 'with a list of email addresses' do
252+
subject do
253+
client.send_message(content: 'Hello, friend!',
254+
to: ['friend1@example.com', 'friend2@example.com'])
255+
end
256+
257+
context 'when the request succeeds' do
258+
before do
259+
response_body = {
260+
id: 2,
261+
msg: '',
262+
result: 'success'
263+
}
264+
265+
json_encoded_to = JSON.generate(['friend1@example.com', 'friend2@example.com'])
266+
267+
stub_request(:post, 'https://test.example.com/api/v1/messages').
268+
with(body: { content: 'Hello, friend!', to: json_encoded_to,
269+
topic: nil, type: 'private' }).
270+
to_return(body: JSON.generate(response_body))
271+
end
272+
273+
it 'returns a private message' do
274+
expect(subject).to be_private
275+
expect(subject.content).to eq('Hello, friend!')
276+
expect(subject.id).to eq(2)
277+
expect(subject.to).to match_array(['friend1@example.com', 'friend2@example.com'])
278+
expect(subject.topic).to be_nil
279+
end
280+
end
281+
282+
context 'when the Zulip server returns an error'
283+
284+
context 'when the API credentials are invalid'
285+
end
241286
end
242287
end
243288

0 commit comments

Comments
 (0)