diff options
author | Nihad Abbasov <narkoz.2008@gmail.com> | 2013-10-29 13:41:20 +0400 |
---|---|---|
committer | Nihad Abbasov <narkoz.2008@gmail.com> | 2013-10-29 13:41:20 +0400 |
commit | 0678b8a426553110e63dde3c75aaf719e74771e1 (patch) | |
tree | 0a6d1e5f03e841f798eb7bfc225fd5f3d7b010df /lib/api | |
parent | ff7073ac23afdf5873f0b5eaee54b9c735e7e04d (diff) | |
download | gitlab-ce-0678b8a426553110e63dde3c75aaf719e74771e1.tar.gz |
add 'Link' header for API response
Diffstat (limited to 'lib/api')
-rw-r--r-- | lib/api/helpers.rb | 20 |
1 files changed, 18 insertions, 2 deletions
diff --git a/lib/api/helpers.rb b/lib/api/helpers.rb index edc662eaaab..227081d4c10 100644 --- a/lib/api/helpers.rb +++ b/lib/api/helpers.rb @@ -51,8 +51,12 @@ module API end end - def paginate(object) - object.page(params[:page]).per(params[:per_page].to_i) + def paginate(relation) + per_page = params[:per_page].to_i + paginated = relation.page(params[:page]).per(per_page) + add_pagination_headers(paginated, per_page) + + paginated end def authenticate! @@ -129,6 +133,18 @@ module API private + def add_pagination_headers(paginated, per_page) + request_url = request.url.split('?').first + + links = [] + links << %(<#{request_url}?page=#{paginated.current_page - 1}&per_page=#{per_page}>; rel="prev") unless paginated.first_page? + links << %(<#{request_url}?page=#{paginated.current_page + 1}&per_page=#{per_page}>; rel="next") unless paginated.last_page? + links << %(<#{request_url}?page=1&per_page=#{per_page}>; rel="first") + links << %(<#{request_url}?page=#{paginated.total_pages}&per_page=#{per_page}>; rel="last") + + header 'Link', links.join(', ') + end + def abilities @abilities ||= begin abilities = Six.new |