Skip to content

Commit a940ef6

Browse files
Merge pull request #32 from jer-k/rejection
Add handler for rejections from ActionCable
2 parents b37da19 + ee3069d commit a940ef6

File tree

4 files changed

+41
-2
lines changed

4 files changed

+41
-2
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ The available hooks to tie in to are:
3737
- `disconnected {}`
3838
- `connected {}`
3939
- `subscribed {}`
40+
- `rejected {}`
4041
- `errored { |msg| }`
4142
- `received { |msg }`
4243
- `pinged { |msg| }`

lib/action_cable_client.rb

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ class Commands
2323
# The queue should store entries in the format:
2424
# [ action, data ]
2525
attr_accessor :message_queue, :_subscribed
26-
attr_accessor :_subscribed_callback, :_pinged_callback, :_connected_callback, :_disconnected_callback
26+
attr_accessor :_subscribed_callback, :_rejected_callback, :_pinged_callback, :_connected_callback, :_disconnected_callback
2727

2828
def_delegator :_websocket_client, :onerror, :errored
2929
def_delegator :_websocket_client, :send, :send_msg
@@ -109,6 +109,19 @@ def connected
109109
end
110110
end
111111

112+
# callback when the server rejects the subscription
113+
#
114+
# @example
115+
# client = ActionCableClient.new(uri, 'RoomChannel')
116+
# client.rejected do
117+
# # do things after the server rejects the subscription
118+
# end
119+
def rejected
120+
self._rejected_callback = proc do |json|
121+
yield(json)
122+
end
123+
end
124+
112125
# callback when the client receives a confirm_subscription message
113126
# from the action_cable server.
114127
# This is only called once, and signifies that you can now send
@@ -162,6 +175,8 @@ def handle_received_message(message)
162175
elsif is_welcome?(json)
163176
subscribe
164177
_connected_callback&.call(json)
178+
elsif is_rejection?(json)
179+
_rejected_callback&.call(json)
165180
elsif !subscribed?
166181
check_for_subscribe_confirmation(json)
167182
else
@@ -174,7 +189,7 @@ def handle_received_message(message)
174189
# {"identifier" => "_ping","type" => "confirm_subscription"}
175190
def check_for_subscribe_confirmation(message)
176191
message_type = message[Message::TYPE_KEY]
177-
return unless Message::TYPE_CONFIRM_SUBSCRIPTION == message_type
192+
return unless Message::TYPE_CONFIRM_SUBSCRIPTION == message_type
178193

179194
self._subscribed = true
180195
_subscribed_callback&.call
@@ -193,6 +208,11 @@ def is_welcome?(message)
193208
Message::IDENTIFIER_WELCOME == message_identifier
194209
end
195210

211+
def is_rejection?(message)
212+
message_type = message[Message::TYPE_KEY]
213+
Message::TYPE_REJECT_SUBSCRIPTION == message_type
214+
end
215+
196216
def subscribe
197217
msg = _message_factory.create(Commands::SUBSCRIBE)
198218
send_msg(msg.to_json)

lib/action_cable_client/message.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ class Message
99
# TODO: find a better place for this constant
1010
TYPE_KEY = 'type'
1111
TYPE_CONFIRM_SUBSCRIPTION = 'confirm_subscription'
12+
TYPE_REJECT_SUBSCRIPTION = 'reject_subscription'
1213

1314
attr_reader :_command, :_identifier, :_data
1415

spec/unit/action_cable_client_spec.rb

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,23 @@
8989
end
9090
end
9191

92+
context 'is a rejection' do
93+
let(:hash) { { 'type' => 'reject_subscription' } }
94+
let(:message) { hash.to_json }
95+
96+
it 'calls _rejected_callback' do
97+
result = nil
98+
99+
@client.rejected do |data|
100+
result = data
101+
end
102+
103+
@client.send(:handle_received_message, message)
104+
105+
expect(result).to eq(hash)
106+
end
107+
end
108+
92109
context 'empty messages are ignored' do
93110
let(:message) { '' }
94111

0 commit comments

Comments
 (0)