Merge branch 'master' of github.com:travis-ci/travis-api

This commit is contained in:
Konstantin Haase 2012-09-27 22:28:39 +02:00
commit 6d1b6c0a82
8 changed files with 86 additions and 83 deletions

View File

@ -40,7 +40,7 @@ GIT
GIT
remote: git://github.com/travis-ci/travis-core.git
revision: a4b1446e3746a5ff59a5438a34f7b2471e33a731
revision: 6d8de39fa098965aad52b51f8f9a3f5490e7def5
branch: sf-more-services
specs:
travis-core (0.0.1)

View File

@ -0,0 +1,11 @@
require 'travis/api/app'
class Travis::Api::App
class Endpoint
class Accounts < Endpoint
get '/', scope: :private do
body service(:account).find_all, type: :accounts
end
end
end
end

View File

@ -115,7 +115,7 @@ class Travis::Api::App
get '/post_message' do
handshake do |user, token, target_origin|
halt 403, invalid_target(target_origin) unless target_ok? target_origin
rendered_user = Travis::Api.data(service(:user, user).find_one, type: :user, version: :v2)
rendered_user = Travis::Api.data(user, version: :v2)
post_message(token: token, user: rendered_user, target_origin: target_origin)
end
end
@ -185,8 +185,8 @@ class Travis::Api::App
def user_for_github_token(token)
data = GH.with(token: token.to_s) { GH['user'] }
scopes = parse_scopes data.headers['x-oauth-scopes']
user = User.find_by_github_id(data['id'])
user ||= User.create! user_info(data, github_oauth_token: token)
user = ::User.find_by_github_id(data['id'])
user ||= ::User.create! user_info(data, github_oauth_token: token)
halt 403, 'not a Travis user' if user.nil?
halt 403, 'insufficient access' unless acceptable? scopes

View File

@ -5,8 +5,7 @@ class Travis::Api::App
# TODO: Add documentation.
class Hooks < Endpoint
# TODO: Add implementation and documentation.
# TODO scope: :private
get('/') do
get('/', scope: :private) do
body service(:hooks).find_all(params), type: :hooks
end

View File

@ -1,63 +0,0 @@
require 'travis/api/app'
class Travis::Api::App
class Endpoint
class Profile < Endpoint
LOCALES = %w(en es fr ja eb nl pl pt-Br ru) # TODO how to figure these out
# Gives information about the currently logged in user.
#
# Example:
#
# {
# "user": {
# "email": "svenfuchs@artweb-design.de",
# "gravatar_id": "402602a60e500e85f2f5dc1ff3648ecb",
# "is_syncing": false,
# "locale": "de",
# "login": "svenfuchs",
# "name": "Sven Fuchs",
# "synced_at": "2012-08-14T22:11:21Z"
# }
# }
get '/', scope: :private do
body service(:user).find_one, type: :user
end
put '/:id?', scope: :private do
update_locale if valid_locale?
'ok'
end
# TODO: Add implementation and documentation.
post '/sync', scope: :private do
sync_user(current_user)
204
end
private
def sync_user(user)
unless user.is_syncing?
publisher = Travis::Amqp::Publisher.new('sync.user')
publisher.publish({ user_id: user.id }, type: 'sync')
user.update_column(:is_syncing, true)
end
end
def locale
params[:profile][:locale].to_s
end
def valid_locale?
LOCALES.include?(locale)
end
def update_locale
current_user.update_attribute(:locale, locale.to_s)
# session[:locale] = locale # ???
end
end
end
end

View File

@ -0,0 +1,43 @@
require 'travis/api/app'
class Travis::Api::App
class Endpoint
class Users < Endpoint
# Gives information about the currently logged in user.
#
# Example:
#
# {
# "user": {
# "name": "Sven Fuchs",
# "login": "svenfuchs",
# "email": "svenfuchs@artweb-design.de",
# "gravatar_id": "402602a60e500e85f2f5dc1ff3648ecb",
# "locale": "de",
# "is_syncing": false,
# "synced_at": "2012-08-14T22:11:21Z"
# }
# }
get '/:id?', scope: :private do
body current_user
end
put '/:id?', scope: :private do
service(:user).update_locale(locale)
204
end
# TODO: Add implementation and documentation.
post '/sync', scope: :private do
service(:user).sync
204
end
private
def locale
params[:profile][:locale]
end
end
end
end

View File

@ -0,0 +1,24 @@
require 'spec_helper'
describe Travis::Api::App::Endpoint::Accounts do
include Travis::Testing::Stubs
let(:access_token) { Travis::Api::App::AccessToken.create(user: user, app_id: -1) }
before do
User.stubs(:find_by_github_id).returns(user)
User.stubs(:find).returns(user)
user.stubs(:repositories).returns(stub(administratable: stub(select: [repository])))
user.stubs(:attributes).returns(:id => user.id, :login => user.login, :name => user.name)
end
it 'includes accounts' do
get('/accounts', access_token: access_token.to_s).should be_ok
parsed_body['accounts'].should == [{
'id' => user.id,
'login' => user.login,
'name' => user.name,
'type' => 'user',
'reposCount' => nil
}]
end
end

View File

@ -1,6 +1,6 @@
require 'spec_helper'
describe Travis::Api::App::Endpoint::Profile do
describe Travis::Api::App::Endpoint::Users do
include Travis::Testing::Stubs
let(:access_token) { Travis::Api::App::AccessToken.create(user: user, app_id: -1) }
@ -11,11 +11,11 @@ describe Travis::Api::App::Endpoint::Profile do
end
it 'needs to be authenticated' do
get('/profile').should_not be_ok
get('/users').should_not be_ok
end
it 'replies with the current user' do
get('/profile', access_token: access_token.to_s).should be_ok
get('/users', access_token: access_token.to_s).should be_ok
parsed_body['user'].should == {
'id' => user.id,
'login' => user.login,
@ -27,15 +27,4 @@ describe Travis::Api::App::Endpoint::Profile do
'synced_at' => user.synced_at.strftime('%Y-%m-%dT%H:%M:%SZ')
}
end
it 'includes accounts' do
get('/profile', access_token: access_token.to_s).should be_ok
parsed_body['accounts'].should == [{
'id' => user.id,
'login' => user.login,
'name' => user.name,
'type' => 'user',
'reposCount' => nil
}]
end
end