Class: RailsSpotlight::Channels::Handlers::LiveConsoleHandler

Inherits:
Object
  • Object
show all
Defined in:
lib/rails_spotlight/channels/handlers/live_console_handler.rb

Constant Summary collapse

TYPE =
'console'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data) ⇒ LiveConsoleHandler

Returns a new instance of LiveConsoleHandler.



11
12
13
# File 'lib/rails_spotlight/channels/handlers/live_console_handler.rb', line 11

def initialize(data)
  @data = data
end

Instance Attribute Details

#dataObject (readonly)

Returns the value of attribute data.



15
16
17
# File 'lib/rails_spotlight/channels/handlers/live_console_handler.rb', line 15

def data
  @data
end

Instance Method Details

#callObject



17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/rails_spotlight/channels/handlers/live_console_handler.rb', line 17

def call
  return unless ::RailsSpotlight.config.live_logs_enabled?
  return unless data['type'] == TYPE

  command = data['command']
  inspect_types = data['inspect_types']
  for_project = Array(data['project'])

  raise_project_mismatch_error!(for_project) if for_project.present? && !for_project.include?(project)

  execute_command(command, inspect_types)
end

#execute_command(command, inspect_types) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/rails_spotlight/channels/handlers/live_console_handler.rb', line 41

def execute_command(command, inspect_types)
  RailsSpotlight.config.logger && RailsSpotlight.config.logger.info("Executing command: #{command}") # rubocop:disable Style/SafeNavigation

  executor.execute(command)
  if executor.execution_successful?
    {
      payload: { result: executor.result_as_json(inspect_types: inspect_types) }
    }
  else
    {
      payload: { failed: executor.result_as_json }
    }
  end
rescue => e # rubocop:disable Style/RescueStandardError
  { error: e.message }
end

#executorObject



30
31
32
# File 'lib/rails_spotlight/channels/handlers/live_console_handler.rb', line 30

def executor
  @executor ||= ::RailsSpotlight::RailsCommandExecutor.new
end

#projectObject



58
59
60
# File 'lib/rails_spotlight/channels/handlers/live_console_handler.rb', line 58

def project
  ::RailsSpotlight.config.project_name
end

#raise_project_mismatch_error!(for_project) ⇒ Object



34
35
36
37
38
39
# File 'lib/rails_spotlight/channels/handlers/live_console_handler.rb', line 34

def raise_project_mismatch_error!(for_project)
  raise ::RailsSpotlight::Channels::Handlers::ResponseError.new(
    "Project mismatch, The command was intended for the #{for_project} project. This is #{project} project",
    code: :project_mismatch
  )
end