Class: Konstant::Web

Inherits:
Object
  • Object
show all
Defined in:
lib/konstant/web.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(env) ⇒ Web

Returns a new instance of Web.



9
10
11
# File 'lib/konstant/web.rb', line 9

def initialize(env)
  @env = env
end

Class Method Details

.call(env) ⇒ Object



5
6
7
# File 'lib/konstant/web.rb', line 5

def self.call(env)
  new(env).route
end

Instance Method Details

#requestObject



13
14
15
# File 'lib/konstant/web.rb', line 13

def request
  @request ||= Rack::Request.new(@env)
end

#routeObject



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/konstant/web.rb', line 17

def route
  if request.path.match(/^\/projects$/)
    [200, {"Content-type" => "application/json"}, [Konstant::Project.all.to_json]]
  elsif m = request.path.match(/^\/projects\/([^\/]+)\/builds\/(\d+)$/)
    project = Konstant::Project.new m[1]
    build = Konstant::Build.new project, m[2]
    [200, {"Content-type" => "application/json"}, [build.to_json]]
  elsif request.path.match(/^\/config$/)
    [200, {"Content-type" => "application/json"}, [Konstant.config.to_json]]
  elsif m = request.path.match(/^\/projects\/([^\/]+)\/builds$/)
    project = Konstant::Project.new m[1]
    [200, {"Content-type" => "application/json"}, [project.builds.to_json]]
  elsif m = request.path.match(/^\/projects\/([^\/]+)\/build$/)
    project = Konstant::Project.new m[1]
    project.build!
    message = {"message" => "ok"}
    [200, {"Content-type" => "application/json"}, [message.to_json]]
  else
    [404, {"Content-type" => "text/plain"}, ["not found"]]
  end
end