Merge pull request #204 from travis-ci/rkh-more-pagination

v3: More pagination and sorting
This commit is contained in:
Konstantin Haase 2015-10-14 14:41:49 +02:00
commit 099d10f466
11 changed files with 71 additions and 7 deletions

View File

@ -3,8 +3,10 @@ module Travis::API::V3
params :state, :event_type, :previous_state, prefix: :build
params :name, prefix: :branch, method_name: :branch_name
sortable_by :id, :started_at, :finished_at
def find(repository)
filter(repository.builds)
sort filter(repository.builds)
end
def filter(list)

View File

@ -1,7 +1,9 @@
module Travis::API::V3
class Queries::Organizations < Query
sortable_by :id, :login, :name, :github_id
def for_member(user)
Models::Organization.joins(:users).where(users: user_condition(user))
sort Models::Organization.joins(:users).where(users: user_condition(user))
end
end
end

View File

@ -1,6 +1,7 @@
module Travis::API::V3
class Queries::Repositories < Query
params :active, :private, prefix: :repository
sortable_by :id, :github_id, :owner_name, :name, active: sort_condition(:active)
def for_member(user)
all.joins(:users).where(users: user_condition(user), invalidated_at: nil)
@ -27,7 +28,7 @@ module Travis::API::V3
list = list.includes(default_branch: :last_build)
list = list.includes(default_branch: { last_build: :commit }) if includes? 'build.commit'.freeze
list
sort list
end
end
end

View File

@ -96,6 +96,13 @@ module Travis::API::V3
pattern = sub_route ? resource.route + sub_route : resource.route
factory = Services[resource.identifier][service]
if factory.params and factory.params.include? "sort_by".freeze
query = Queries[resource.identifier]
if query and query.sortable?
resources[resource.identifier][:sortable_by] = query.sort_by.keys
end
end
pattern.to_templates.each do |template|
params = factory.params if request_method == 'GET'.freeze
params &&= params.reject { |p| p.start_with? ?@.freeze }

View File

@ -1,5 +1,7 @@
module Travis::API::V3
class Services::Organizations::ForCurrentUser < Service
paginate(default_limit: 100)
def run!
raise LoginRequired unless access_control.logged_in?
query.for_member(access_control.user)

View File

@ -2,6 +2,7 @@ module Travis::API::V3
class Services::Owner::Repositories < Service
params :active, :private, prefix: :repository
result_type :repositories
paginate(default_limit: 100)
def run!
unfiltered = query(:repositories).for_owner(find(:owner))

View File

@ -1,6 +1,7 @@
module Travis::API::V3
class Services::Repositories::ForCurrentUser < Service
params :active, :private, prefix: :repository
paginate(default_limit: 100)
def run!
raise LoginRequired unless access_control.logged_in?

View File

@ -64,7 +64,7 @@ describe Travis::API::V3::ServiceIndex do
describe "for_current_user action" do
let(:action) { resource.fetch("actions").fetch("for_current_user") }
specify { expect(action).to include("@type"=>"template", "request_method"=>"GET", "uri_template"=>"#{path}repos{?active,include,private,repository.active,repository.private}") }
specify { expect(action).to include("@type"=>"template", "request_method"=>"GET", "uri_template"=>"#{path}repos{?active,include,limit,offset,private,repository.active,repository.private,sort_by}") }
end
end
@ -110,7 +110,7 @@ describe Travis::API::V3::ServiceIndex do
describe "for_current_user action" do
let(:action) { resource.fetch("actions").fetch("for_current_user") }
specify { expect(action).to include("@type"=>"template", "request_method"=>"GET", "uri_template"=>"#{path}orgs{?include}") }
specify { expect(action).to include("@type"=>"template", "request_method"=>"GET", "uri_template"=>"#{path}orgs{?include,limit,offset,sort_by}") }
end
end

View File

@ -20,7 +20,23 @@ describe Travis::API::V3::Services::Organizations::ForCurrentUser do
example { expect(JSON.load(body)).to be == {
"@type" => "organizations",
"@href" => "/v3/orgs",
"@representation" => "standard",
"@representation" => "standard",
"@pagination" => {
"limit" => 100,
"offset" => 0,
"count" => 1,
"is_first" => true,
"is_last" => true,
"next" => nil,
"prev" => nil,
"first" => {
"@href" => "/v3/orgs",
"offset" => 0,
"limit" => 100},
"last" => {
"@href" => "/v3/orgs",
"offset" => 0,
"limit" => 100}},
"organizations" => [{
"@type" => "organization",
"@href" => "/v3/org/#{org.id}",

View File

@ -18,6 +18,22 @@ describe Travis::API::V3::Services::Owner::Repositories do
"@type" => "repositories",
"@href" => "/v3/owner/svenfuchs/repos",
"@representation" => "standard",
"@pagination" => {
"limit" => 100,
"offset" => 0,
"count" => 1,
"is_first" => true,
"is_last" => true,
"next" => nil,
"prev" => nil,
"first" => {
"@href" => "/v3/owner/svenfuchs/repos",
"offset" => 0,
"limit" => 100},
"last" => {
"@href" => "/v3/owner/svenfuchs/repos",
"offset" => 0,
"limit" => 100}},
"repositories" => [{
"@type" => "repository",
"@href" => "/v3/repo/#{repo.id}",

View File

@ -17,7 +17,23 @@ describe Travis::API::V3::Services::Repositories::ForCurrentUser do
example { expect(JSON.load(body)).to be == {
"@type" => "repositories",
"@href" => "/v3/repos",
"@representation" => "standard",
"@representation" => "standard",
"@pagination" => {
"limit" => 100,
"offset" => 0,
"count" => 1,
"is_first" => true,
"is_last" => true,
"next" => nil,
"prev" => nil,
"first" => {
"@href" => "/v3/repos",
"offset" => 0,
"limit" => 100},
"last" => {
"@href" => "/v3/repos",
"offset" => 0,
"limit" => 100}},
"repositories" => [{
"@type" => "repository",
"@href" => "/v3/repo/#{repo.id}",