Skip to content

Commit 2282213

Browse files
authored
Add update channel partial (#39)
1 parent 74739e5 commit 2282213

File tree

3 files changed

+29
-0
lines changed

3 files changed

+29
-0
lines changed

README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,14 @@ chan.unban_user('bob-1')
131131
# Query channel state
132132
chan.query({'messages' => { 'limit' => 10, 'id_lte' => m1['id']}})
133133

134+
# Update metadata (overwrite)
135+
chan.update({'motd' => 'one apple a day....'})
136+
137+
# Update partial
138+
# 1. key-value pairs to set
139+
# 2. keys to unset (remove)
140+
chan.update_partial({color: 'blue', age: 30}, ['motd'])
141+
134142
# Query channel members
135143
chan.query_members({name: {'$autocomplete': 'test'}}, sort: {last_created_at: -1}, offset: 5, limit: 5)
136144
```

lib/stream-chat/channel.rb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,13 @@ def update(channel_data, update_message = nil)
8484
@client.post(url, data: payload)
8585
end
8686

87+
def update_partial(set = nil, unset = nil)
88+
raise StreamChannelException 'set or unset is needed' if set.nil? && unset.nil?
89+
90+
payload = { set: set, unset: unset }
91+
@client.patch(url, data: payload)
92+
end
93+
8794
def delete
8895
@client.delete(url)
8996
end

spec/channel_spec.rb

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,20 @@
5757
expect(response['channel']['motd']).to eq 'one apple a day...'
5858
end
5959

60+
it 'can update metadata partial' do
61+
@channel.update_partial({ color: 'blue', age: 30 }, ['motd'])
62+
response = @channel.query
63+
expect(response['channel']['color']).to eq 'blue'
64+
expect(response['channel']['age']).to eq 30
65+
expect(response['channel']).not_to include 'motd'
66+
67+
@channel.update_partial({ color: 'red' }, ['age'])
68+
response = @channel.query
69+
expect(response['channel']['color']).to eq 'red'
70+
expect(response['channel']).not_to include 'age'
71+
expect(response['channel']).not_to include 'motd'
72+
end
73+
6074
it 'can delete' do
6175
response = @channel.delete
6276
expect(response).to include 'channel'

0 commit comments

Comments
 (0)