lib/kamerling/http_api.rb in kamerling-0.0.2 vs lib/kamerling/http_api.rb in kamerling-0.0.3

- old
+ new

@@ -1,38 +1,54 @@ require 'forwardable' require 'sinatra/base' require 'slim' +require_relative 'project' +require_relative 'repos' +require_relative 'task_dispatcher' +require_relative 'uuid' -module Kamerling class HTTPAPI < Sinatra::Base - extend Forwardable +module Kamerling + class HTTPAPI < Sinatra::Base + extend Forwardable - configure { set repos: Repos } + delegate %i(task_dispatcher repos) => :settings - get '/' do - slim :root - end + configure { set task_dispatcher: TaskDispatcher.new, repos: Repos } - get '/clients' do - warn_off { slim :clients, locals: { clients: repos.clients } } - end + get '/' do + render_template :root + end - get '/projects' do - warn_off { slim :projects, locals: { projects: repos.projects } } - end + get '/clients' do + render_template :clients, locals: { clients: repos.clients } + end - get '/projects/:project_uuid' do - project = repos.project params['project_uuid'] - clients = repos.clients_for project - tasks = repos.tasks_for project - warn_off { slim :project, locals: { clients: clients, tasks: tasks } } - end + get '/projects' do + render_template :projects, locals: { projects: repos.projects } + end - post '/projects' do - uuid = params.fetch('uuid') { UUID.new } - repos << Project.new(name: params['name'], uuid: uuid) - redirect '/projects' - end + get '/projects/:project_uuid' do + project = repos.project(params['project_uuid']) + clients = repos.clients_for(project) + tasks = repos.tasks_for(project) + render_template :project, locals: { clients: clients, tasks: tasks } + end - private + post '/projects' do + name = params.fetch('name') + uuid = params.fetch('uuid') + repos << Project.new(name: name, uuid: uuid) + redirect '/projects' + end - delegate repos: :settings -end end + post '/projects/dispatch' do + task_dispatcher.dispatch_all + redirect '/projects' + end + + private + + def render_template(template, locals: {}) + warn_off { slim template, locals: locals } + end + end +end