Module: Pytty::Daemon::Components::Handler

Defined in:
lib/pytty/daemon/components/handler.rb

Class Method Summary collapse

Class Method Details

.handle(component:, params:) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/pytty/daemon/components/handler.rb', line 7

def self.handle(component:, params:)
  return case component
  when "ps"
    output = []
    Pytty::Daemon.yields.each do |id, process_yield|
      output << process_yield
    end
    [200, output]
  when "yield"
    cmd = params.dig "cmd"
    env = params.dig "env"
    id = params.dig "id"

    if Pytty::Daemon.yields[id]
      [500, "already exists"]
    else
      process_yield = Pytty::Daemon::ProcessYield.new cmd, id: id, env: env
      Pytty::Daemon.yields[process_yield.id] = process_yield
      Pytty::Daemon.dump
      [200, process_yield]
    end
  else
    raise "unknown: #{component}"
  end
end