From 882921fb5622ae92ef660d8b4a07c5cb31c3b832 Mon Sep 17 00:00:00 2001 From: Bruno Casali Date: Thu, 7 Jul 2022 16:12:45 -0300 Subject: [PATCH 1/2] Add pagination and filtering to the tasks resources --- lib/meilisearch/client.rb | 4 ++-- lib/meilisearch/task.rb | 9 +++++++-- spec/meilisearch/client/keys_spec.rb | 6 +++--- spec/meilisearch/client/tasks_spec.rb | 22 ++++++++++++++++++++-- 4 files changed, 32 insertions(+), 9 deletions(-) diff --git a/lib/meilisearch/client.rb b/lib/meilisearch/client.rb index e120a2f6..2dfefef1 100644 --- a/lib/meilisearch/client.rb +++ b/lib/meilisearch/client.rb @@ -110,8 +110,8 @@ def create_dump ### TASKS - def tasks - task_endpoint.task_list + def tasks(options = {}) + task_endpoint.task_list(options) end def task(task_uid) diff --git a/lib/meilisearch/task.rb b/lib/meilisearch/task.rb index a43bf9f2..306c965f 100644 --- a/lib/meilisearch/task.rb +++ b/lib/meilisearch/task.rb @@ -5,8 +5,13 @@ module MeiliSearch class Task < HTTPRequest - def task_list - http_get '/tasks/' + ALLOWED_PARAMS = [:limit, :from, :next, :index_uid, :type, :status].freeze + + def task_list(options = {}) + body = Utils.transform_attributes(options.transform_keys(&:to_sym).slice(*ALLOWED_PARAMS)) + body = body.transform_values { |v| v.respond_to?(:join) ? v.join(',') : v } + + http_get '/tasks/', body end def task(task_uid) diff --git a/spec/meilisearch/client/keys_spec.rb b/spec/meilisearch/client/keys_spec.rb index 030e6486..4a83274c 100644 --- a/spec/meilisearch/client/keys_spec.rb +++ b/spec/meilisearch/client/keys_spec.rb @@ -95,7 +95,7 @@ end it 'updates a key with their uid data' do - new_key = client.create_key(delete_docs_key_options.merge(uid: uuid_v4)) + client.create_key(delete_docs_key_options.merge(uid: uuid_v4)) new_updated_key = client.update_key(uuid_v4, name: 'coco') expect(new_updated_key['name']).to eq('coco') @@ -105,9 +105,9 @@ new_key = client.create_key(add_docs_key_options) client.delete_key(new_key['key']) - expect { + expect do client.key(new_key['key']) - }.to raise_error(MeiliSearch::ApiError) + end.to raise_error(MeiliSearch::ApiError) end end end diff --git a/spec/meilisearch/client/tasks_spec.rb b/spec/meilisearch/client/tasks_spec.rb index 917c66cc..55c75a1f 100644 --- a/spec/meilisearch/client/tasks_spec.rb +++ b/spec/meilisearch/client/tasks_spec.rb @@ -24,7 +24,7 @@ expect(last_task.keys).to include(*succeeded_task_keys) end - it 'gets a task of the MeiliSearch instance' do + it 'gets a task of the Meilisearch instance' do task = client.task(0) expect(task).to be_a(Hash) @@ -32,7 +32,7 @@ expect(task.keys).to include(*succeeded_task_keys) end - it 'gets all the tasks of the MeiliSearch instance' do + it 'gets tasks of the Meilisearch instance' do tasks = client.tasks expect(tasks['results']).to be_a(Array) @@ -42,6 +42,24 @@ expect(last_task.keys).to include(*succeeded_task_keys) end + it 'paginates tasks with limit/from/next' do + tasks = client.tasks(limit: 2) + + expect(tasks['results'].count).to be <= 2 + expect(tasks['from']).to be_a(Integer) + expect(tasks['next']).to be_a(Integer) + end + + it 'filters tasks with index_uid/type/status' do + tasks = client.tasks(index_uid: ['a-cool-index-name']) + + expect(tasks['results'].count).to eq(0) + + tasks = client.tasks(index_uid: ['books'], type: ['documentAdditionOrUpdate'], status: ['succeeded']) + + expect(tasks['results'].count).to be > 1 + end + describe '#index.wait_for_task' do it 'waits for task with default values' do task = index.add_documents(documents) From 1a9308d50d8c56db174713920be51aa6df78846d Mon Sep 17 00:00:00 2001 From: Bruno Casali Date: Mon, 11 Jul 2022 09:43:12 -0300 Subject: [PATCH 2/2] Remove :next from ALLOWED_PARAMS --- lib/meilisearch/task.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/meilisearch/task.rb b/lib/meilisearch/task.rb index 306c965f..5dd24df9 100644 --- a/lib/meilisearch/task.rb +++ b/lib/meilisearch/task.rb @@ -5,7 +5,7 @@ module MeiliSearch class Task < HTTPRequest - ALLOWED_PARAMS = [:limit, :from, :next, :index_uid, :type, :status].freeze + ALLOWED_PARAMS = [:limit, :from, :index_uid, :type, :status].freeze def task_list(options = {}) body = Utils.transform_attributes(options.transform_keys(&:to_sym).slice(*ALLOWED_PARAMS))