Skip to content

Commit 982dc58

Browse files
authored
Apply changes after the review (#285)
* Apply changes after the review * Fix linter errors
1 parent d0f45ff commit 982dc58

File tree

6 files changed

+108
-110
lines changed

6 files changed

+108
-110
lines changed

lib/meilisearch/client.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -113,11 +113,11 @@ def dump_status(dump_uid)
113113
### TASKS
114114

115115
def tasks
116-
task_endpoint.global_tasks
116+
task_endpoint.task_list
117117
end
118118

119119
def task(task_uid)
120-
task_endpoint.global_task(task_uid)
120+
task_endpoint.task(task_uid)
121121
end
122122

123123
def wait_for_task(task_uid, timeout_in_ms = 5000, interval_in_ms = 50)
@@ -131,7 +131,7 @@ def index_object(uid, primary_key = nil)
131131
end
132132

133133
def task_endpoint
134-
Task.new(@base_url, @api_key, @options)
134+
@task_endpoint ||= Task.new(@base_url, @api_key, @options)
135135
end
136136
end
137137
end

lib/meilisearch/index.rb

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ class Index < HTTPRequest
77
attr_reader :uid, :primary_key, :created_at, :updated_at
88

99
def initialize(index_uid, url, api_key = nil, primary_key = nil, options = {})
10-
@url = url
1110
@uid = index_uid
1211
@primary_key = primary_key
1312
super(url, api_key, options)
@@ -195,7 +194,7 @@ def search(query, options = {})
195194
### TASKS
196195

197196
def task_endpoint
198-
Task.new(@url, @api_key, @options)
197+
@task_endpoint ||= Task.new(@base_url, @api_key, @options)
199198
end
200199
private :task_endpoint
201200

lib/meilisearch/task.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,11 @@
55

66
module MeiliSearch
77
class Task < HTTPRequest
8-
def global_tasks
8+
def task_list
99
http_get '/tasks/'
1010
end
1111

12-
def global_task(task_uid)
12+
def task(task_uid)
1313
http_get "/tasks/#{task_uid}"
1414
end
1515

@@ -24,7 +24,7 @@ def index_task(index_uid, task_uid)
2424
def wait_for_task(task_uid, timeout_in_ms = 5000, interval_in_ms = 50)
2525
Timeout.timeout(timeout_in_ms.to_f / 1000) do
2626
loop do
27-
task = global_task(task_uid)
27+
task = task(task_uid)
2828
return task if achieved_task?(task)
2929

3030
sleep interval_in_ms.to_f / 1000

spec/meilisearch/client/indexes_spec.rb

Lines changed: 25 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,21 +5,25 @@
55
context 'without a primary key' do
66
it 'creates an index' do
77
task = client.create_index('new_index')
8+
89
expect(task['type']).to eq('indexCreation')
9-
client.wait_for_task(task['uid'])
1010

11+
client.wait_for_task(task['uid'])
1112
index = client.fetch_index('new_index')
13+
1214
expect(index).to be_a(MeiliSearch::Index)
1315
expect(index.uid).to eq('new_index')
1416
expect(index.primary_key).to be_nil
1517
end
1618

1719
it 'creates an index synchronously' do
1820
task = client.create_index!('new_index')
21+
1922
expect(task['type']).to eq('indexCreation')
2023
expect(task['status']).to eq('succeeded')
2124

2225
index = client.fetch_index('new_index')
26+
2327
expect(index).to be_a(MeiliSearch::Index)
2428
expect(index.uid).to eq('new_index')
2529
expect(index.primary_key).to be_nil
@@ -29,10 +33,12 @@
2933
context 'with a primary key' do
3034
it 'creates an index' do
3135
task = client.create_index('new_index', primaryKey: 'primary_key')
36+
3237
expect(task['type']).to eq('indexCreation')
33-
client.wait_for_task(task['uid'])
3438

39+
client.wait_for_task(task['uid'])
3540
index = client.fetch_index('new_index')
41+
3642
expect(index).to be_a(MeiliSearch::Index)
3743
expect(index.uid).to eq('new_index')
3844
expect(index.primary_key).to eq('primary_key')
@@ -41,10 +47,12 @@
4147

4248
it 'creates an index synchronously' do
4349
task = client.create_index!('new_index', primaryKey: 'primary_key')
50+
4451
expect(task['type']).to eq('indexCreation')
4552
expect(task['status']).to eq('succeeded')
4653

4754
index = client.fetch_index('new_index')
55+
4856
expect(index).to be_a(MeiliSearch::Index)
4957
expect(index.uid).to eq('new_index')
5058
expect(index.primary_key).to eq('primary_key')
@@ -72,10 +80,12 @@
7280
primaryKey: 'primary_key',
7381
uid: 'not_primary_key'
7482
)
83+
7584
expect(task['type']).to eq('indexCreation')
76-
client.wait_for_task(task['uid'])
7785

86+
client.wait_for_task(task['uid'])
7887
index = client.fetch_index('new_index')
88+
7989
expect(index).to be_a(MeiliSearch::Index)
8090
expect(index.uid).to eq('new_index')
8191
expect(index.primary_key).to eq('primary_key')
@@ -86,13 +96,14 @@
8696

8797
context 'when an index with a given uid already exists' do
8898
it 'returns a failing task' do
89-
task1 = client.create_index!('existing_index')
90-
task2 = client.create_index!('existing_index')
91-
expect(task1['type']).to eq('indexCreation')
92-
expect(task2['type']).to eq('indexCreation')
93-
expect(task1['status']).to eq('succeeded')
94-
expect(task2['status']).to eq('failed')
95-
expect(task2['error']['code']).to eq('index_already_exists')
99+
initial_task = client.create_index!('existing_index')
100+
last_task = client.create_index!('existing_index')
101+
102+
expect(initial_task['type']).to eq('indexCreation')
103+
expect(last_task['type']).to eq('indexCreation')
104+
expect(initial_task['status']).to eq('succeeded')
105+
expect(last_task['status']).to eq('failed')
106+
expect(last_task['error']['code']).to eq('index_already_exists')
96107
end
97108
end
98109

@@ -108,7 +119,9 @@
108119
describe '#indexes' do
109120
it 'returns MeiliSearch::Index objects' do
110121
client.create_index!('index')
122+
111123
index = client.indexes.first
124+
112125
expect(index).to be_a(MeiliSearch::Index)
113126
end
114127

@@ -162,7 +175,6 @@
162175
describe '#fetch_raw_index' do
163176
it 'fetch a specific index raw Hash response based on uid' do
164177
client.create_index!('specific_index_fetch_raw', primaryKey: 'primary_key')
165-
166178
index = client.fetch_index('specific_index_fetch_raw')
167179
raw_response = index.fetch_raw_info
168180

@@ -179,7 +191,6 @@
179191
describe '#index' do
180192
it 'returns an index object with the provided uid' do
181193
client.create_index!('existing_index', primaryKey: 'primary_key')
182-
183194
# this index is in memory, without metadata from server
184195
index = client.index('existing_index')
185196

@@ -197,9 +208,10 @@
197208
context 'when the index exists' do
198209
it 'deletes the index' do
199210
client.create_index!('existing_index')
200-
201211
task = client.delete_index('existing_index')
212+
202213
expect(task['type']).to eq('indexDeletion')
214+
203215
achieved_task = client.wait_for_task(task['uid'])
204216

205217
expect(achieved_task['status']).to eq('succeeded')

spec/meilisearch/client/keys_spec.rb

Lines changed: 53 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,29 @@
11
# frozen_string_literal: true
22

33
RSpec.describe 'MeiliSearch::Client - Keys' do
4-
context 'Test the default key roles' do
5-
let(:public_key) { client.keys['results'].filter { |k| k['description'].start_with? 'Default Search API Key' }.first }
6-
let(:private_key) { client.keys['results'].filter { |k| k['description'].start_with? 'Default Admin API Key' }.first }
4+
context 'When a client uses default key roles' do
5+
let(:search_key) { client.keys['results'].find { |k| k['description'].start_with? 'Default Search' } }
6+
let(:admin_key) { client.keys['results'].find { |k| k['description'].start_with? 'Default Admin' } }
77

88
it 'fails to get settings if public key used' do
9-
new_client = MeiliSearch::Client.new(URL, public_key['key'])
9+
new_client = MeiliSearch::Client.new(URL, search_key['key'])
10+
1011
expect do
1112
new_client.index(random_uid).settings
1213
end.to raise_meilisearch_api_error_with(403, 'invalid_api_key', 'auth')
1314
end
1415

1516
it 'fails to get keys if private key used' do
16-
new_client = MeiliSearch::Client.new(URL, private_key['key'])
17+
new_client = MeiliSearch::Client.new(URL, admin_key['key'])
18+
1719
expect do
1820
new_client.keys
1921
end.to raise_meilisearch_api_error_with(403, 'invalid_api_key', 'auth')
2022
end
2123

22-
it 'fails to search if no key used' do
24+
it 'fails to get settings if no key is used' do
2325
new_client = MeiliSearch::Client.new(URL)
26+
2427
expect do
2528
new_client.index(random_uid).settings
2629
end.to raise_meilisearch_api_error_with(401, 'missing_authorization_header', 'auth')
@@ -30,36 +33,50 @@
3033
uid = random_uid
3134
index = client.index(uid)
3235
index.add_documents!(title: 'Test')
33-
34-
new_client = MeiliSearch::Client.new(URL, public_key['key'])
36+
new_client = MeiliSearch::Client.new(URL, search_key['key'])
3537
response = new_client.index(uid).search('test')
38+
3639
expect(response).to have_key('hits')
3740
end
3841

3942
it 'succeeds to get settings when using private key' do
4043
uid = random_uid
4144
client.create_index!(uid)
42-
new_client = MeiliSearch::Client.new(URL, private_key['key'])
45+
new_client = MeiliSearch::Client.new(URL, admin_key['key'])
4346
response = new_client.index(uid).settings
47+
4448
expect(response).to have_key('rankingRules')
4549
end
4650
end
4751

48-
context 'Test the key managements' do
52+
context 'When managing keys' do
53+
let(:delete_docs_key_options) do
54+
{
55+
description: 'A new key to delete docs',
56+
actions: ['documents.delete'],
57+
indexes: ['*'],
58+
expiresAt: nil
59+
}
60+
end
61+
let(:add_docs_key_options) do
62+
{
63+
description: 'A new key to add docs',
64+
actions: ['documents.add'],
65+
indexes: ['*'],
66+
expiresAt: nil
67+
}
68+
end
69+
4970
it 'gets the list of the default keys' do
5071
results = client.keys['results']
72+
5173
expect(results).to be_a(Array)
5274
expect(results.count).to be >= 2
5375
end
5476

5577
it 'creates a key' do
56-
key_options = {
57-
description: 'A new key to add docs',
58-
actions: ['documents.add'],
59-
indexes: ['*'],
60-
expiresAt: nil
61-
}
62-
new_key = client.create_key(key_options)
78+
new_key = client.create_key(add_docs_key_options)
79+
6380
expect(new_key['expiresAt']).to be_nil
6481
expect(new_key['key']).to be_a(String)
6582
expect(new_key['createdAt']).to be_a(String)
@@ -69,13 +86,8 @@
6986
end
7087

7188
it 'creates a key using snake_case' do
72-
key_options = {
73-
description: 'A new key to add docs',
74-
actions: ['documents.add'],
75-
indexes: ['*'],
76-
expires_at: nil
77-
}
78-
new_key = client.create_key(key_options)
89+
new_key = client.create_key(add_docs_key_options)
90+
7991
expect(new_key['expiresAt']).to be_nil
8092
expect(new_key['key']).to be_a(String)
8193
expect(new_key['createdAt']).to be_a(String)
@@ -85,41 +97,31 @@
8597
end
8698

8799
it 'gets a key' do
88-
key_options = {
89-
description: 'A new key to delete docs',
90-
actions: ['documents.delete'],
91-
indexes: ['*'],
92-
expiresAt: nil
93-
}
94-
new_key = client.create_key(key_options)
100+
new_key = client.create_key(delete_docs_key_options)
101+
95102
expect(client.key(new_key['key'])['description']).to eq('A new key to delete docs')
96-
end
97103

98-
it 'update a key' do
99-
key_options = {
100-
description: 'A new key to delete docs',
101-
actions: ['documents.delete'],
102-
indexes: ['*'],
103-
expiresAt: nil
104-
}
105-
new_key = client.create_key(key_options)
104+
key = client.key(new_key['key'])
106105

106+
expect(key['expiresAt']).to be_nil
107+
expect(key['key']).to be_a(String)
108+
expect(key['createdAt']).to be_a(String)
109+
expect(key['updatedAt']).to be_a(String)
110+
expect(key['indexes']).to eq(['*'])
111+
expect(key['description']).to eq('A new key to delete docs')
112+
end
113+
114+
it 'updates a key' do
115+
new_key = client.create_key(delete_docs_key_options)
107116
new_updated_key = client.update_key(new_key['key'], indexes: ['coco'])
108117

109118
expect(new_updated_key['key']).to eq(new_key['key'])
110119
expect(new_updated_key['description']).to eq(new_key['description'])
111120
expect(new_updated_key['indexes']).to eq(['coco'])
112121
end
113122

114-
it 'update a key using snake_case' do
115-
key_options = {
116-
description: 'A new key to delete docs',
117-
actions: ['documents.delete'],
118-
indexes: ['*'],
119-
expires_at: nil
120-
}
121-
new_key = client.create_key(key_options)
122-
123+
it 'updates a key using snake_case' do
124+
new_key = client.create_key(delete_docs_key_options)
123125
new_updated_key = client.update_key(new_key['key'], indexes: ['coco'])
124126

125127
expect(new_updated_key['key']).to eq(new_key['key'])
@@ -128,15 +130,9 @@
128130
end
129131

130132
it 'deletes a key' do
131-
key_options = {
132-
description: 'A new key to add docs',
133-
actions: ['documents.add'],
134-
indexes: ['*'],
135-
expiresAt: nil
136-
}
137-
new_key = client.create_key(key_options)
138-
133+
new_key = client.create_key(add_docs_key_options)
139134
client.delete_key(new_key['key'])
135+
140136
expect(client.keys.filter { |k| k['key'] == new_key['key'] }).to be_empty
141137
end
142138
end

0 commit comments

Comments
 (0)