summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorNathan L Smith <nlloyds@gmail.com>2016-12-05 15:42:54 -0600
committerGitHub <noreply@github.com>2016-12-05 15:42:54 -0600
commit28ec5154589de42a83f2d4e056a3ed8b15ad0a7c (patch)
tree0874fd8ca991bfc3a5aa2584b4d9be0667761337
parent3b969c992fcb7b4be58d165b700ae6994ed7d116 (diff)
parente6c52bc7f88c2dfdcf78a9e8a0e720103a5c92ef (diff)
downloadchef-zero-28ec5154589de42a83f2d4e056a3ed8b15ad0a7c.tar.gz
Merge pull request #248 from chef/nls/fix-start-pagination
Fix pagination with start parameters
-rw-r--r--lib/chef_zero/endpoints/search_endpoint.rb34
1 files changed, 13 insertions, 21 deletions
diff --git a/lib/chef_zero/endpoints/search_endpoint.rb b/lib/chef_zero/endpoints/search_endpoint.rb
index 16da462..b808ce9 100644
--- a/lib/chef_zero/endpoints/search_endpoint.rb
+++ b/lib/chef_zero/endpoints/search_endpoint.rb
@@ -15,16 +15,6 @@ module ChefZero
results["rows"] = results["rows"].map { |name, uri, value, search_value| value }
- # Slice the array based on the value of the "rows" query parameter. If
- # this is a positive integer, we'll get the number of rows asked for.
- # If it's nil, we'll get -1, which gives us all of the elements.
- #
- # Do the same for "start", which will start at 0 if that value is not
- # given.
- results["rows"] =
- results["rows"][request.query_params["start"].to_i..
- (request.query_params["rows"].to_i - 1)]
-
json_response(200, results)
rescue ChefZero::Solr::ParseError
bad_search_request(request)
@@ -124,10 +114,8 @@ module ChefZero
query_string = request.query_params["q"] || "*:*"
solr_query = ChefZero::Solr::SolrParser.new(query_string).parse
sort_string = request.query_params["sort"]
- start = request.query_params["start"]
- start = start.to_i if start
- rows = request.query_params["rows"]
- rows = rows.to_i if rows
+ start = request.query_params["start"].to_i
+ rows = request.query_params["rows"].to_i
# Get the search container
container, expander = search_container(request, index, orgname)
@@ -151,13 +139,18 @@ module ChefZero
result = result.reverse if sort_order == "DESC"
end
- # Paginate
- if start
- result = result[start..start + (rows || -1)]
- end
{
- "rows" => result,
- "start" => start || 0,
+ # Paginate
+ #
+ # Slice the array based on the value of the "rows" query parameter. If
+ # this is a positive integer, we'll get the number of rows asked for.
+ # If it's nil, we'll get -1, which gives us all of the elements.
+ #
+ # Do the same for "start", which will start at 0 if that value is not
+ # given.
+ "rows" => result[start..(rows - 1)],
+ # Also return start and total in the object
+ "start" => start,
"total" => total,
}
end
@@ -212,7 +205,6 @@ module ChefZero
end
dest
end # deep_merge!
-
end
end
end