From f2e8ccafc1efb0da8e14c2e03021d27dd1b40431 Mon Sep 17 00:00:00 2001 From: Sven Fuchs Date: Thu, 27 Sep 2012 15:38:56 +0200 Subject: [PATCH] remove /profile, add /user and /accounts --- lib/travis/api/app/endpoint/accounts.rb | 11 ++++ lib/travis/api/app/endpoint/authorization.rb | 4 +- lib/travis/api/app/endpoint/profile.rb | 63 -------------------- lib/travis/api/app/endpoint/user.rb | 43 +++++++++++++ 4 files changed, 56 insertions(+), 65 deletions(-) create mode 100644 lib/travis/api/app/endpoint/accounts.rb delete mode 100644 lib/travis/api/app/endpoint/profile.rb create mode 100644 lib/travis/api/app/endpoint/user.rb diff --git a/lib/travis/api/app/endpoint/accounts.rb b/lib/travis/api/app/endpoint/accounts.rb new file mode 100644 index 00000000..0626e94f --- /dev/null +++ b/lib/travis/api/app/endpoint/accounts.rb @@ -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: :account + end + end + end +end diff --git a/lib/travis/api/app/endpoint/authorization.rb b/lib/travis/api/app/endpoint/authorization.rb index f9be8f6a..940237fd 100644 --- a/lib/travis/api/app/endpoint/authorization.rb +++ b/lib/travis/api/app/endpoint/authorization.rb @@ -180,8 +180,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 diff --git a/lib/travis/api/app/endpoint/profile.rb b/lib/travis/api/app/endpoint/profile.rb deleted file mode 100644 index 74d45f9a..00000000 --- a/lib/travis/api/app/endpoint/profile.rb +++ /dev/null @@ -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 - diff --git a/lib/travis/api/app/endpoint/user.rb b/lib/travis/api/app/endpoint/user.rb new file mode 100644 index 00000000..2d012c45 --- /dev/null +++ b/lib/travis/api/app/endpoint/user.rb @@ -0,0 +1,43 @@ +require 'travis/api/app' + +class Travis::Api::App + class Endpoint + class User < 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 + services(:user).update_locale(locale) + 204 + end + + # TODO: Add implementation and documentation. + post '/sync', scope: :private do + services(:user).sync + 204 + end + + private + + def locale + params[:profile][:locale] + end + end + end +end