v3: add /org/:id endpoint

This commit is contained in:
Konstantin Haase 2015-02-18 10:07:14 +01:00
parent 32c2d9b0b9
commit b84a0a492b
10 changed files with 79 additions and 10 deletions

View File

@ -20,6 +20,10 @@ module Travis::API::V3
protected
def organization_visible?(organization)
unrestricted_api?
end
def repository_visible?(repository)
return true if unrestricted_api? and not repository.private?
private_repository_visible?(repository)

View File

@ -0,0 +1,10 @@
module Travis::API::V3
class Queries::Organization < Query
params :id
def find
return ::Organization.find_by_id(id) if id
raise WrongParams
end
end
end

View File

@ -3,8 +3,12 @@ module Travis::API::V3
DIRECT_ATTRIBUTES = %i[id login name github_id]
extend self
def render(organization, **)
{ :@type => 'organization'.freeze, **direct_attributes(organization) }
def render(organization, script_name: nil, **)
{
:@type => 'organization'.freeze,
:@href => Renderer.href(:organization, id: organization.id, script_name: script_name),
**direct_attributes(organization)
}
end
def direct_attributes(repository)

View File

@ -19,6 +19,11 @@ module Travis::API::V3
get :for_current_user
end
resource :organization do
route '/org/{organization.id}'
get :find
end
resource :organizations do
route '/orgs'
get :for_current_user

View File

@ -0,0 +1,13 @@
module Travis::API::V3
module ServiceHelpers::Organization
def organization
@organization ||= find_organization
end
def find_organization
not_found(true, :organization) unless org = query(:organization).find
not_found(false, :organization) unless access_control.visible? org
org
end
end
end

View File

@ -2,6 +2,7 @@ module Travis::API::V3
module Services
extend ConstantResolver
Organization = Module.new { extend Services }
Organizations = Module.new { extend Services }
Repositories = Module.new { extend Services }
Repository = Module.new { extend Services }

View File

@ -0,0 +1,9 @@
module Travis::API::V3
class Services::Organization::Find < Service
helpers :organization
def run!
organization
end
end
end

View File

@ -14,6 +14,8 @@ describe Travis::API::V3::ServiceIndex do
"for_current_user" => [{"request-method"=>"GET", "uri-template"=>"#{path}repos"}] },
"organizations" => {
"for_current_user" => [{"request-method"=>"GET", "uri-template"=>"#{path}orgs"}] },
"organization" => {
"find" => [{"request-method"=>"GET", "uri-template"=>"#{path}org/{organization.id}"}] },
"requests" => {
"find" => [{"request-method"=>"GET", "uri-template"=>"#{path}repo/{repository.id}/requests"}],
"create" => [{"request-method"=>"POST", "uri-template"=>"#{path}repo/{repository.id}/requests"}]}

View File

@ -0,0 +1,20 @@
require 'spec_helper'
describe Travis::API::V3::Services::Organization::Find do
let(:org) { Organization.new(login: 'example-org') }
before { org.save! }
after { org.delete }
describe 'existing org, public api' do
before { get("/v3/org/#{org.id}") }
example { expect(last_response).to be_ok }
example { expect(JSON.load(body)).to be == {
"@type" => "organization",
"@href" => "/v3/org/#{org.id}",
"id" => org.id,
"login" => "example-org",
"name" => nil,
"github_id" => nil
}}
end
end

View File

@ -18,14 +18,15 @@ describe Travis::API::V3::Services::Organizations::ForCurrentUser do
before { get("/v3/orgs", {}, headers) }
example { expect(last_response).to be_ok }
example { expect(JSON.load(body)).to be == {
"@type" => "organizations",
"@href" => "/v3/orgs",
"organizations" => [{
"@type" => "organization",
"id" => org.id,
"login" => "example-org",
"name" => nil,
"github_id" => nil
"@type" => "organizations",
"@href" => "/v3/orgs",
"organizations" => [{
"@type" => "organization",
"@href" => "/v3/org/#{org.id}",
"id" => org.id,
"login" => "example-org",
"name" => nil,
"github_id" => nil
}]
}}
end