Skip to content

Commit dbef0d2

Browse files
committed
Combine /repos/(:id|:owner_name) routes into one
With Sinatra 2.x, these two routes cannot coexist. So, use a regexp type to handle both cases. See https://github.com/sinatra/mustermann/blob/master/mustermann/README.md#-regexp-pattern
1 parent 674d9c8 commit dbef0d2

File tree

1 file changed

+15
-11
lines changed

1 file changed

+15
-11
lines changed

lib/travis/api/app/endpoint/repos.rb

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -52,21 +52,25 @@ class Repos < Endpoint
5252
end
5353
end
5454

55-
# Gets the repository with the given id.
56-
#
55+
# Given a single string, consider two cases:
56+
# 1. string consisting entirely of digits
5757
# ### Response
5858
#
5959
# json(:repository)
60-
get '/:id' do
61-
prefer_follower do
62-
respond_with service(:find_repo, params)
63-
end
64-
end
65-
66-
# Retrieves repositories for a given owner.
67-
get '/:owner_name' do
60+
#
61+
# 2. All other cases
62+
# ### Response
63+
#
64+
# array of repositories owned by that owner
65+
get '/((?<id>\d+)|(?<owner_name>[^\/]+))', mustermann_opts: { type: :regexp } do
6866
prefer_follower do
69-
respond_with service(:find_repos, params).run
67+
if params[:id]
68+
params.delete :owner_name
69+
respond_with service(:find_repo, params)
70+
elsif params[:owner_name]
71+
params.delete :id
72+
respond_with service(:find_repos, params).run
73+
end
7074
end
7175
end
7276

0 commit comments

Comments
 (0)