Class: UsersController
- Inherits:
-
Rubee::BaseController
- Object
- Rubee::BaseController
- UsersController
- Defined in:
- lib/app/controllers/users_controller.rb
Instance Method Summary collapse
-
#create ⇒ Object
Endpoint to find or create user.
- #publish ⇒ Object
- #subscribe ⇒ Object
- #unsubscribe ⇒ Object
Methods inherited from Rubee::BaseController
attach_websocket!, #css, #extract_params, #handle_websocket, #headers, #image, #initialize, #js, #params, #render_template, #response_with, #websocket, #websocket_connections
Methods included from Rubee::Hookable
Constructor Details
This class inherits a constructor from Rubee::BaseController
Instance Method Details
#create ⇒ Object
Endpoint to find or create user
6 7 8 9 10 11 12 13 |
# File 'lib/app/controllers/users_controller.rb', line 6 def create user = User.where(**params).last user ||= User.create(**params) response_with(object: user, type: :json) rescue StandardError => e response_with(object: { error: e. }, type: :json) end |
#publish ⇒ Object
43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/app/controllers/users_controller.rb', line 43 def publish args = {} User.pub(params[:channel], message: params[:message]) do |channel| user = User.find(params[:options][:id]) args[:message] = params[:message] args[:sender] = params[:options][:id] args[:sender_name] = user.email websocket_connections.stream(channel, args) end response_with(object: { type: 'system', message: params[:message], status: :published }, type: :websocket) rescue StandardError => e response_with(object: { type: 'system', error: e. }, type: :websocket) end |
#subscribe ⇒ Object
15 16 17 18 19 20 21 22 23 24 25 26 27 |
# File 'lib/app/controllers/users_controller.rb', line 15 def subscribe channel = params[:channel] sender_id = params[:options][:id] io = params[:options][:io] User.sub(channel, sender_id, io) do |channel, args| websocket_connections.register(channel, args[:io]) end response_with(object: { type: 'system', channel: params[:channel], status: :subscribed }, type: :websocket) rescue StandardError => e response_with(object: { type: 'system', error: e. }, type: :websocket) end |
#unsubscribe ⇒ Object
29 30 31 32 33 34 35 36 37 38 39 40 41 |
# File 'lib/app/controllers/users_controller.rb', line 29 def unsubscribe channel = params[:channel] sender_id = params[:options][:id] io = params[:options][:io] User.unsub(channel, sender_id, io) do |channel, args| websocket_connections.remove(channel, args[:io]) end response_with(object: params.merge(type: 'system', status: :unsubscribed), type: :websocket) rescue StandardError => e response_with(object: { type: 'system', error: e. }, type: :websocket) end |