v3: add /user and /user/:id

This commit is contained in:
Konstantin Haase 2015-03-24 17:25:57 +01:00
parent 4ae3f5e171
commit 3a058339be
10 changed files with 88 additions and 2 deletions

View File

@ -44,6 +44,10 @@ module Travis::API::V3
unrestricted_api?
end
def user_visible?(user)
unrestricted_api?
end
def repository_visible?(repository)
return true if unrestricted_api? and not repository.private?
private_repository_visible?(repository)

View File

@ -18,7 +18,9 @@ module Travis::API::V3
expander = EXPANDER_CACHE[[type, script_name, args.keys]] ||= begin
resource = Routes.resources.detect { |r| r.identifier == type }
route = resource.route if resource
verb, sub = resource.services.key(:find) if resource
route = resource.route if verb
route += sub if sub
route &&= Mustermann.new(script_name, type: :identity) + route if script_name and not script_name.empty?
key_mapping = {}
args.keys.each do |key|

View File

@ -32,6 +32,12 @@ module Travis::API::V3
get :find
end
resource :user do
route '/user'
get :current
get :find, '/{user.id}'
end
resource :organization do
route '/org/{organization.id}'
get :find

View File

@ -9,6 +9,7 @@ module Travis::API::V3
Repositories = Module.new { extend Services }
Repository = Module.new { extend Services }
Requests = Module.new { extend Services }
User = Module.new { extend Services }
def result_type
@resul_type ||= name[/[^:]+$/].underscore.to_sym

View File

@ -0,0 +1,8 @@
module Travis::API::V3
class Services::User::Current < Service
def run!
raise LoginRequired unless access_control.logged_in?
access_control.user || not_found(false)
end
end
end

View File

@ -0,0 +1,7 @@
module Travis::API::V3
class Services::User::Find < Service
def run!
find
end
end
end

View File

@ -93,7 +93,19 @@ describe Travis::API::V3::ServiceIndex do
[{"@type"=>"template",
"request_method"=>"GET",
"uri_template"=>"#{path}orgs"}]},
"attributes"=>["organizations"]}}
"attributes"=>["organizations"]},
"user"=>
{"@type"=>"resource",
"actions"=>
{"current"=>
[{"@type"=>"template",
"request_method"=>"GET",
"uri_template"=>"#{path}user"}],
"find"=>
[{"@type"=>"template",
"request_method"=>"GET",
"uri_template"=>"#{path}user/{user.id}"}]},
"attributes"=>["id", "login", "name", "github_id", "is_syncing", "synced_at"]}}
}
describe 'with /v3 prefix' do

View File

@ -0,0 +1,23 @@
require 'spec_helper'
describe Travis::API::V3::Services::User::Current do
let(:user) { User.find_by_login('svenfuchs') }
let(:token) { Travis::Api::App::AccessToken.create(user: user, app_id: 1) }
let(:headers) {{ 'HTTP_AUTHORIZATION' => "token #{token}" }}
describe "authenticated as user with access" do
before { get("/v3/user", {}, headers) }
example { expect(last_response).to be_ok }
example { expect(JSON.load(body)).to be == {
"@type" => "user",
"@href" => "/v3/user/#{user.id}",
"id" => user.id,
"login" => "svenfuchs",
"name" =>"Sven Fuchs",
"github_id" => user.github_id,
"is_syncing" => user.is_syncing,
"synced_at" => user.synced_at
}}
end
end

View File

@ -0,0 +1,23 @@
require 'spec_helper'
describe Travis::API::V3::Services::User::Find do
let(:user) { User.find_by_login('svenfuchs') }
let(:token) { Travis::Api::App::AccessToken.create(user: user, app_id: 1) }
let(:headers) {{ 'HTTP_AUTHORIZATION' => "token #{token}" }}
describe "authenticated as user with access" do
before { get("/v3/user/#{user.id}", {}, headers) }
example { expect(last_response).to be_ok }
example { expect(JSON.load(body)).to be == {
"@type" => "user",
"@href" => "/v3/user/#{user.id}",
"id" => user.id,
"login" => "svenfuchs",
"name" =>"Sven Fuchs",
"github_id" => user.github_id,
"is_syncing" => user.is_syncing,
"synced_at" => user.synced_at
}}
end
end