travis-api/lib/travis/api/app.rb
2012-07-23 14:18:42 +02:00

103 lines
2.4 KiB
Ruby

require 'sinatra'
require 'sinatra/reloader'
require 'json'
require 'travis'
Travis::Database.connect
module Travis
module Api
class App < Sinatra::Application
autoload :Service, 'travis/api/app/service'
use ActiveRecord::ConnectionAdapters::ConnectionManagement
error ActiveRecord::RecordNotFound do
not_found
end
configure :development do
register Sinatra::Reloader
set :show_exceptions, :after_handler
end
before do
content_type :json
end
get '/repositories' do
respond_with Service::Repos.new(params).collection
end
get '/repositories/:id' do
respond_with Service::Repos.new(params).item
# raise if not params[:format] == 'png'
end
get '/builds' do
respond_with Service::Builds.new(params).collection
end
get '/builds/:id' do
respond_with Service::Builds.new(params).item
end
get '/branches' do
# respond_with Service::Repos.new(params).item, :type => :branches
{ branches: [] }.to_json
end
get '/jobs' do
respond_with Service::Jobs.new(params).collection, :type => 'jobs'
end
get '/jobs/:id' do
respond_with Service::Jobs.new(params).item, :type => 'job'
end
get '/artifacts/:id' do
respond_with Service::Artifacts.new(params).item
end
get '/workers' do
respond_with Service::Workers.new(params).collection
end
get '/hooks' do
authenticate_user!
respond_with Service::Hooks.new(user, params).item
# rescue_from ActiveRecord::RecordInvalid, :with => Proc.new { head :not_acceptable }
end
put '/hooks/:id' do
authenticate_user!
respond_with Service::Hooks.new(user, params).update
end
get '/profile' do
authenticate_user!
respond_with Service::Profile.new(user).update
end
post '/profile/sync' do
authenticate_user!
respond_with Service::Profile.new(user).sync
end
private
def authenticate_user!
@user = User.find_by_login('svenfuchs')
end
def respond_with(resource, options = {})
Travis::Api.data(resource, { :params => params, :version => version }.merge(options)).to_json
end
def version
'v2'
end
end
end
end