Class: Pandemonium::API

Inherits:
Goliath::API
  • Object
show all
Defined in:
lib/pandemonium/api.rb

Instance Method Summary collapse

Instance Method Details

#response(env) ⇒ Object



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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/pandemonium/api.rb', line 10

def response(env)
  case env["PATH_INFO"]
  when "/"
    puts env.repos
    [200, {}, "Pandemonium"]
  when "/attach"
    project_name = env.params["project_name"]
    boss         = env.jobs[project_name]
    channel      = boss.channel

    env.stream_close if !boss || !boss.running?

    env["subscription"] = channel.subscribe do |message|
      case message.type
      when :data
        env.stream_send(message.content)
      when :exit_status
        env.stream_send("Exit status: #{message.content}")
      when :action
        if message.content == :finish
          channel.unsubscribe(env["subscription"])
          env.stream_close
        end
      end
    end

    streaming_response(202, {"X-Stream" => "Pandemonium"})
  when "/deploy"
    project_name = env.params["project_name"]
    project = env.repos[project_name]

    boss = env.jobs[project_name]

    if boss.running?
      [200, {}, "Already deploying"]
    else
      boss.run_command project
      [201, {}, "Deploying"]
    end
  end
end