commit 5c29457066bbc44c0ea681462830bfb7ce96a402
Author: Sven Fuchs <svenfuchs@artweb-design.de>
Date:   Wed Jul 18 02:14:24 2012 +0200

    initial commit

diff --git a/Gemfile b/Gemfile
new file mode 100644
index 00000000..1e107130
--- /dev/null
+++ b/Gemfile
@@ -0,0 +1,23 @@
+source :rubygems
+
+ruby '1.9.3' rescue nil
+
+gem 'travis-core',    github: 'travis-ci/travis-core'
+gem 'travis-support', github: 'travis-ci/travis-support'
+
+gem 'sinatra'
+gem 'sinatra-contrib'
+# gem 'sinatra-cross_origin', github: 'britg/sinatra-cross_origin'
+gem 'rack-contrib',         github: 'rack/rack-contrib', require: 'rack/contrib'
+
+gem 'rake',           '~> 0.9.2.2'
+gem 'versionist',     '~> 0.2.0'
+
+# db
+gem 'pg',             '~> 0.13.2'
+gem 'newrelic_rpm',   '~> 3.3.0'
+gem 'hubble',         git: 'git://github.com/roidrage/hubble'
+
+# heroku
+gem 'unicorn',        '~> 4.1.1'
+
diff --git a/Gemfile.lock b/Gemfile.lock
new file mode 100644
index 00000000..b69e7ef7
--- /dev/null
+++ b/Gemfile.lock
@@ -0,0 +1,203 @@
+GIT
+  remote: git://github.com/rack/rack-contrib.git
+  revision: b7e7c38fd02c3b5da91aa57af78b3f571c6ebcd0
+  specs:
+    rack-contrib (1.1.0)
+      rack (>= 0.9.1)
+
+GIT
+  remote: git://github.com/roidrage/hubble
+  revision: 5220415d5542a2868d54f7be9f35fc1d66126b8e
+  specs:
+    hubble (0.1.2)
+      json (~> 1.6.5)
+
+GIT
+  remote: git://github.com/travis-ci/travis-core.git
+  revision: 2531cab2f6584faf311eae19dda0fe6c34fb7264
+  specs:
+    travis-core (0.0.1)
+      actionmailer (~> 3.2.3)
+      activerecord (~> 3.2.3)
+      data_migrations (~> 0.0.1)
+      gh
+      hashr (~> 0.0.19)
+      metriks (~> 0.9.7)
+      multi_json
+      postmark-rails (~> 0.4.1)
+      pusher (~> 0.9.2)
+      railties (~> 3.2.3)
+      rake (~> 0.9.2.2)
+      redis (~> 3.0)
+      rollout (~> 1.1.0)
+      simple_states (~> 0.1.1)
+      thor (~> 0.14.6)
+
+GIT
+  remote: git://github.com/travis-ci/travis-support.git
+  revision: 44a2e3cfa76b619962f14fe72c2cca9d683c684c
+  specs:
+    travis-support (0.0.1)
+
+GEM
+  remote: http://rubygems.org/
+  specs:
+    actionmailer (3.2.6)
+      actionpack (= 3.2.6)
+      mail (~> 2.4.4)
+    actionpack (3.2.6)
+      activemodel (= 3.2.6)
+      activesupport (= 3.2.6)
+      builder (~> 3.0.0)
+      erubis (~> 2.7.0)
+      journey (~> 1.0.1)
+      rack (~> 1.4.0)
+      rack-cache (~> 1.2)
+      rack-test (~> 0.6.1)
+      sprockets (~> 2.1.3)
+    activemodel (3.2.6)
+      activesupport (= 3.2.6)
+      builder (~> 3.0.0)
+    activerecord (3.2.6)
+      activemodel (= 3.2.6)
+      activesupport (= 3.2.6)
+      arel (~> 3.0.2)
+      tzinfo (~> 0.3.29)
+    activeresource (3.2.6)
+      activemodel (= 3.2.6)
+      activesupport (= 3.2.6)
+    activesupport (3.2.6)
+      i18n (~> 0.6)
+      multi_json (~> 1.0)
+    addressable (2.2.8)
+    arel (3.0.2)
+    atomic (1.0.1)
+    avl_tree (1.1.3)
+    backports (2.6.1)
+    builder (3.0.0)
+    data_migrations (0.0.1)
+      activerecord
+      rake
+    erubis (2.7.0)
+    eventmachine (0.12.10)
+    faraday (0.8.1)
+      multipart-post (~> 1.1)
+    gh (0.7.3)
+      addressable
+      backports (~> 2.3)
+      faraday (~> 0.8)
+      multi_json (~> 1.0)
+      net-http-persistent (>= 2.7)
+      net-http-pipeline
+    hashr (0.0.21)
+    hike (1.2.1)
+    hitimes (1.1.1)
+    i18n (0.6.0)
+    journey (1.0.4)
+    json (1.6.7)
+    kgio (2.7.4)
+    mail (2.4.4)
+      i18n (>= 0.4.0)
+      mime-types (~> 1.16)
+      treetop (~> 1.4.8)
+    metriks (0.9.9.1)
+      atomic (~> 1.0)
+      avl_tree (~> 1.1.2)
+      hitimes (~> 1.1)
+    mime-types (1.19)
+    multi_json (1.3.6)
+    multipart-post (1.1.5)
+    net-http-persistent (2.7)
+    net-http-pipeline (1.0.1)
+    newrelic_rpm (3.3.5)
+    pg (0.13.2)
+    polyglot (0.3.3)
+    postmark (0.9.12)
+      json
+      rake
+    postmark-rails (0.4.1)
+      actionmailer
+      postmark (>= 0.9.0)
+      rake
+    pusher (0.9.4)
+      multi_json (~> 1.0)
+      signature (~> 0.1.2)
+    rack (1.4.1)
+    rack-cache (1.2)
+      rack (>= 0.4)
+    rack-protection (1.2.0)
+      rack
+    rack-ssl (1.3.2)
+      rack
+    rack-test (0.6.1)
+      rack (>= 1.0)
+    rails (3.2.6)
+      actionmailer (= 3.2.6)
+      actionpack (= 3.2.6)
+      activerecord (= 3.2.6)
+      activeresource (= 3.2.6)
+      activesupport (= 3.2.6)
+      bundler (~> 1.0)
+      railties (= 3.2.6)
+    railties (3.2.6)
+      actionpack (= 3.2.6)
+      activesupport (= 3.2.6)
+      rack-ssl (~> 1.3.2)
+      rake (>= 0.8.7)
+      rdoc (~> 3.4)
+      thor (>= 0.14.6, < 2.0)
+    raindrops (0.10.0)
+    rake (0.9.2.2)
+    rdoc (3.12)
+      json (~> 1.4)
+    redis (3.0.1)
+    rollout (1.1.0)
+    signature (0.1.3)
+    simple_states (0.1.1)
+      activesupport
+      hashr (~> 0.0.10)
+    sinatra (1.3.2)
+      rack (~> 1.3, >= 1.3.6)
+      rack-protection (~> 1.2)
+      tilt (~> 1.3, >= 1.3.3)
+    sinatra-contrib (1.3.1)
+      backports (>= 2.0)
+      eventmachine
+      rack-protection
+      rack-test
+      sinatra (~> 1.3.0)
+      tilt (~> 1.3)
+    sprockets (2.1.3)
+      hike (~> 1.2)
+      rack (~> 1.0)
+      tilt (~> 1.1, != 1.3.0)
+    thor (0.14.6)
+    tilt (1.3.3)
+    treetop (1.4.10)
+      polyglot
+      polyglot (>= 0.3.1)
+    tzinfo (0.3.33)
+    unicorn (4.1.1)
+      kgio (~> 2.4)
+      rack
+      raindrops (~> 0.6)
+    versionist (0.2.3)
+      rails (~> 3.0)
+      yard (~> 0.7)
+    yard (0.8.2.1)
+
+PLATFORMS
+  ruby
+
+DEPENDENCIES
+  hubble!
+  newrelic_rpm (~> 3.3.0)
+  pg (~> 0.13.2)
+  rack-contrib!
+  rake (~> 0.9.2.2)
+  sinatra
+  sinatra-contrib
+  travis-core!
+  travis-support!
+  unicorn (~> 4.1.1)
+  versionist (~> 0.2.0)
diff --git a/config.ru b/config.ru
new file mode 100644
index 00000000..9d32d914
--- /dev/null
+++ b/config.ru
@@ -0,0 +1,5 @@
+$:.unshift 'lib'
+
+require 'travis/api/app'
+
+run Travis::Api::App
diff --git a/lib/travis/api/app.rb b/lib/travis/api/app.rb
new file mode 100644
index 00000000..09cad0cb
--- /dev/null
+++ b/lib/travis/api/app.rb
@@ -0,0 +1,92 @@
+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'
+
+      register Sinatra::Reloader
+      use ActiveRecord::ConnectionAdapters::ConnectionManagement
+
+      provides :json
+
+      get '/repositories' do
+        respond_with Service::Repos.new(params).collection
+      end
+
+      get '/repositories/:id' do
+        respond_with Service::Repos.new(params).element
+        # rescue ActiveRecord::RecordNotFound
+        #   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).element
+      end
+
+      get '/branches' do
+        respond_with Service::Repos.new(params).element, :type => :branches
+      end
+
+      get '/jobs' do
+        respond_with Service::Jobs.new(params).collection
+      end
+
+      get '/jobs/:id' do
+        respond_with Service::Jobs.new(params).element
+      end
+
+      get '/artifacts/:id' do
+        respond_with Service::Artifacts.new(params).element
+      end
+
+      get '/workers' do
+        respond_with Service::Workers.new(params).collection
+      end
+
+      get '/hooks' do
+        authenticate_user!
+        respond_with Service::Hooks.new(user, params).element
+        # 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, params = {})
+          Travis::Api.data(resource, :params => self.params.merge(params), :version => version).to_json
+        end
+
+        def version
+          'v2'
+        end
+    end
+  end
+end
diff --git a/lib/travis/api/app/service.rb b/lib/travis/api/app/service.rb
new file mode 100644
index 00000000..97479805
--- /dev/null
+++ b/lib/travis/api/app/service.rb
@@ -0,0 +1,20 @@
+module Travis
+  module Api
+    class App
+      class Service
+        autoload :Artifacts, 'travis/api/app/service/artifacts'
+        autoload :Builds,    'travis/api/app/service/builds'
+        autoload :Hooks,     'travis/api/app/service/hooks'
+        autoload :Jobs,      'travis/api/app/service/jobs'
+        autoload :Repos,     'travis/api/app/service/repos'
+        autoload :Workers,   'travis/api/app/service/workers'
+
+        attr_reader :params
+
+        def initialize(params)
+          @params = params
+        end
+      end
+    end
+  end
+end
diff --git a/lib/travis/api/app/service/artifacts.rb b/lib/travis/api/app/service/artifacts.rb
new file mode 100644
index 00000000..e9ada452
--- /dev/null
+++ b/lib/travis/api/app/service/artifacts.rb
@@ -0,0 +1,14 @@
+module Travis
+  module Api
+    class App
+      class Service
+        class Artifacts < Service
+          def element
+            Artifact.find(params[:id])
+          end
+        end
+      end
+    end
+  end
+end
+
diff --git a/lib/travis/api/app/service/builds.rb b/lib/travis/api/app/service/builds.rb
new file mode 100644
index 00000000..8638dd5a
--- /dev/null
+++ b/lib/travis/api/app/service/builds.rb
@@ -0,0 +1,27 @@
+module Travis
+  module Api
+    class App
+      class Service
+        class Builds < Service
+          def collection
+            scope = repository.builds.by_event_type(params[:event_type] || 'push')
+            scope = params[:after] ? scope.older_than(params[:after]) : scope.recent
+            scope
+          end
+
+          def element
+            one = params[:repository_id] ? repository.builds : Build
+            one.includes(:commit, :matrix => [:commit, :log]).find(params[:id])
+          end
+
+          private
+
+            def repository
+              Repository.find_by(params) || not_found # TODO needs to return nil if params are empty
+            end
+        end
+      end
+    end
+  end
+end
+
diff --git a/lib/travis/api/app/service/hooks.rb b/lib/travis/api/app/service/hooks.rb
new file mode 100644
index 00000000..446888b8
--- /dev/null
+++ b/lib/travis/api/app/service/hooks.rb
@@ -0,0 +1,39 @@
+module Travis
+  module Api
+    class App
+      class Service
+        class Hooks < Service
+          attr_reader :user
+
+          def initialize(user, params)
+            super(params)
+            @user = user
+          end
+
+          def collection
+            user.service_hooks
+          end
+
+          def update
+            hook.set(payload[:active], user)
+            hook
+          end
+
+          private
+
+            def hook
+              repository.service_hook
+            end
+
+            def repository
+              Repository.find_or_create_by_owner_name_and_name(params[:owner_name], params[:name])
+            end
+
+            def payload
+              params[:service_hook] || {}
+            end
+        end
+      end
+    end
+  end
+end
diff --git a/lib/travis/api/app/service/jobs.rb b/lib/travis/api/app/service/jobs.rb
new file mode 100644
index 00000000..84f0ae20
--- /dev/null
+++ b/lib/travis/api/app/service/jobs.rb
@@ -0,0 +1,23 @@
+module Travis
+  module Api
+    class App
+      class Service
+        class Jobs < Service
+          def collection
+            if params[:ids]
+              Job.where(:id => params[:ids]).includes(:commit, :log)
+            else
+              jobs = Job.queued.includes(:commit, :log)
+              jobs = jobs.where(:queue => params[:queue]) if params[:queue]
+              jobs
+            end
+          end
+
+          def element
+            Job.find(params[:id])
+          end
+        end
+      end
+    end
+  end
+end
diff --git a/lib/travis/api/app/service/profile.rb b/lib/travis/api/app/service/profile.rb
new file mode 100644
index 00000000..49d71d16
--- /dev/null
+++ b/lib/travis/api/app/service/profile.rb
@@ -0,0 +1,32 @@
+module Travis
+  module Api
+    class App
+      class Service
+        class Profile < Service
+          attr_reader :user
+
+          def initialize(user)
+            @user = user
+          end
+
+          def element
+            user
+          end
+
+          def sync
+            unless user.is_syncing?
+              publisher.publish({ user_id: user.id }, type: 'sync')
+              user.update_attribute(:is_syncing, true)
+            end
+          end
+
+          private
+
+            def publisher
+              Travis::Amqp::Publisher.new('sync.user')
+            end
+        end
+      end
+    end
+  end
+end
diff --git a/lib/travis/api/app/service/repos.rb b/lib/travis/api/app/service/repos.rb
new file mode 100644
index 00000000..1af2d875
--- /dev/null
+++ b/lib/travis/api/app/service/repos.rb
@@ -0,0 +1,27 @@
+module Travis
+  module Api
+    class App
+      class Service
+        class Repos
+          attr_reader :params
+
+          def initialize(params)
+            @params = params
+          end
+
+          def collection
+            scope = Repository.timeline.recent
+            scope = scope.by_owner_name(params[:owner_name]) if params[:owner_name]
+            scope = scope.by_slug(params[:slug])             if params[:slug]
+            scope = scope.search(params[:search])            if params[:search].present?
+            scope
+          end
+
+          def element
+            Repository.find_by(params)
+          end
+        end
+      end
+    end
+  end
+end
diff --git a/lib/travis/api/app/service/workers.rb b/lib/travis/api/app/service/workers.rb
new file mode 100644
index 00000000..ba23c943
--- /dev/null
+++ b/lib/travis/api/app/service/workers.rb
@@ -0,0 +1,14 @@
+module Travis
+  module Api
+    class App
+      class Service
+        class Workers < Service
+          def collection
+            Worker.order(:host, :name)
+          end
+        end
+      end
+    end
+  end
+end
+
diff --git a/travis-api.gemspec b/travis-api.gemspec
new file mode 100644
index 00000000..df9c7c12
--- /dev/null
+++ b/travis-api.gemspec
@@ -0,0 +1,8 @@
+# encoding: utf-8
+
+Gem::Specification.new do |s|
+  s.name         = 'travis-api'
+  s.version      = '0.0.1'
+  s.require_path = 'lib'
+end
+