Class: RailsSpotlight::Channels::SpotlightChannel
- Inherits:
-
ActionCable::Channel::Base
- Object
- ActionCable::Channel::Base
- RailsSpotlight::Channels::SpotlightChannel
- Defined in:
- lib/rails_spotlight/channels/spotlight_channel.rb
Class Method Summary collapse
- .broadcast(attrs = {}) ⇒ Object
-
.broadcast_log(message, level, callsite = {}, name = nil) ⇒ Object
rubocop:disable Metrics/CyclomaticComplexity.
Instance Method Summary collapse
Class Method Details
.broadcast(attrs = {}) ⇒ Object
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 |
# File 'lib/rails_spotlight/channels/spotlight_channel.rb', line 9 def self.broadcast(attrs = {}) broadcasting = ::RailsSpotlight::Channels::SPOTLIGHT_CHANNEL = { type: attrs[:type], code: attrs[:code] || 'ok', project: ::RailsSpotlight.config.project_name, version: ::RailsSpotlight::VERSION, payload: attrs[:payload] || {} } coder = ::ActiveSupport::JSON encoded = coder ? coder.encode() : ActionCable.server.pubsub.broadcast(broadcasting, encoded) # We do not use the following code because it is triggering logs and can cause an infinite loop # ActionCable.server.broadcast( # ::RailsSpotlight::Channels::SPOTLIGHT_CHANNEL, # { # type: attrs[:type], # code: attrs[:code] || 'ok', # project: ::RailsSpotlight.config.project_name, # version: ::RailsSpotlight::VERSION, # payload: attrs[:payload] || {} # } # ) rescue StandardError => e RailsSpotlight.config.logger.fatal("#{e.}\n #{e.backtrace.join("\n ")}") end |
.broadcast_log(message, level, callsite = {}, name = nil) ⇒ Object
rubocop:disable Metrics/CyclomaticComplexity
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
# File 'lib/rails_spotlight/channels/spotlight_channel.rb', line 36 def self.broadcast_log(, level, callsite = {}, name = nil) # rubocop:disable Metrics/CyclomaticComplexity return unless ::RailsSpotlight.config.use_cable? return if .blank? broadcast( type: 'logs', payload: (callsite || {}).merge( msg: , src: ENV['RS_SRC'] || RailsSpotlight.config.default_rs_src, l: level, dt: Time.now.to_f, id: AppRequest.current&.id, pg: name ) ) rescue StandardError => e RailsSpotlight.config.logger.fatal("#{e.}\n #{e.backtrace&.join("\n ")}") end |
Instance Method Details
#receive(data) ⇒ Object
64 65 66 67 68 69 70 71 |
# File 'lib/rails_spotlight/channels/spotlight_channel.rb', line 64 def receive(data) return publish({ message: 'Unknown type of request', code: :unknown_type, type: :error }) unless Handlers::TYPES.include?(data['type']) result = Handlers.handle(data) publish({ payload: result[:payload], code: result[:code] || :ok, type: data['type'] }) if result[:payload] rescue ::RailsSpotlight::Channels::Handlers::ResponseError => e publish({ message: e., code: e.code, type: :error }) end |
#subscribed ⇒ Object
55 56 57 58 |
# File 'lib/rails_spotlight/channels/spotlight_channel.rb', line 55 def subscribed stream_from ::RailsSpotlight::Channels::SPOTLIGHT_CHANNEL publish({ message: "Your #{project} project is now connected to the spotlight channel.", code: :connected, type: :info }) end |
#unsubscribed ⇒ Object
60 61 62 |
# File 'lib/rails_spotlight/channels/spotlight_channel.rb', line 60 def unsubscribed # Any cleanup needed when channel is unsubscribed end |