Skip to content

Commit 170bbe9

Browse files
authored
Merge pull request #470 from meilisearch/jmks-deprecate-camel-case-attributes
Jmks deprecate camel case attributes
2 parents 5bf72a4 + 9147580 commit 170bbe9

File tree

15 files changed

+140
-121
lines changed

15 files changed

+140
-121
lines changed

.rubocop_todo.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ Metrics/BlockLength:
2323
# Offense count: 2
2424
# Configuration parameters: CountComments, CountAsOne.
2525
Metrics/ClassLength:
26-
Max: 327
26+
Max: 328
2727

2828
# Offense count: 1
2929
# Configuration parameters: Max, CountKeywordArgs.

docker-compose.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ services:
2222
- bundle:/vendor/bundle
2323

2424
meilisearch:
25-
image: getmeili/meilisearch:v1.3.0
25+
image: getmeili/meilisearch:latest
2626
ports:
2727
- "7700"
2828
environment:

lib/meilisearch/client.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ def indexes(options = {})
3131

3232
# Usage:
3333
# client.create_index('indexUID')
34-
# client.create_index('indexUID', primaryKey: 'id')
34+
# client.create_index('indexUID', primary_key: 'id')
3535
def create_index(index_uid, options = {})
3636
body = Utils.transform_attributes(options.merge(uid: index_uid))
3737

lib/meilisearch/index.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -221,8 +221,9 @@ def delete_all_documents!
221221
### SEARCH
222222

223223
def search(query, options = {})
224-
parsed_options = Utils.transform_attributes({ q: query.to_s }.merge(options.compact))
224+
attributes = { q: query.to_s }.merge(options.compact)
225225

226+
parsed_options = Utils.transform_attributes(attributes)
226227
response = http_post "/indexes/#{@uid}/search", parsed_options
227228

228229
response['nbHits'] ||= response['estimatedTotalHits'] unless response.key?('totalPages')

lib/meilisearch/utils.rb

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ def self.transform_attributes(body)
99
when Array
1010
body.map { |item| transform_attributes(item) }
1111
when Hash
12+
warn_on_non_conforming_attribute_names(body)
1213
parse(body)
1314
else
1415
body
@@ -52,6 +53,21 @@ def self.version_error_handler(method_name)
5253
raise e.class, message_builder(e.message, method_name)
5354
end
5455

56+
def self.warn_on_non_conforming_attribute_names(body)
57+
return if body.nil?
58+
59+
non_snake_case = body.keys.grep_v(/^[a-z0-9_]+$/)
60+
return if non_snake_case.empty?
61+
62+
message = <<~MSG
63+
Attributes will be expected to be snake_case in future versions of Meilisearch Ruby.
64+
65+
Non-conforming attributes: #{non_snake_case.join(', ')}
66+
MSG
67+
68+
warn(message)
69+
end
70+
5571
private_class_method :parse, :message_builder
5672
end
5773
end

spec/meilisearch/client/indexes_spec.rb

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232

3333
context 'with a primary key' do
3434
it 'creates an index' do
35-
task = client.create_index('books', primaryKey: 'reference_code')
35+
task = client.create_index('books', primary_key: 'reference_code')
3636

3737
expect(task['type']).to eq('indexCreation')
3838

@@ -46,7 +46,7 @@
4646
end
4747

4848
it 'creates an index synchronously' do
49-
task = client.create_index!('books', primaryKey: 'reference_code')
49+
task = client.create_index!('books', primary_key: 'reference_code')
5050

5151
expect(task['type']).to eq('indexCreation')
5252
expect(task['status']).to eq('succeeded')
@@ -77,7 +77,7 @@
7777
it 'ignores the uid option' do
7878
task = client.create_index(
7979
'books',
80-
primaryKey: 'reference_code',
80+
primary_key: 'reference_code',
8181
uid: 'publications'
8282
)
8383

@@ -173,7 +173,7 @@
173173

174174
describe '#fetch_index' do
175175
it 'fetches index by uid' do
176-
client.create_index!('books', primaryKey: 'reference_code')
176+
client.create_index!('books', primary_key: 'reference_code')
177177

178178
fetched_index = client.fetch_index('books')
179179

@@ -186,7 +186,7 @@
186186

187187
describe '#fetch_raw_index' do
188188
it 'fetch a specific index raw Hash response based on uid' do
189-
client.create_index!('books', primaryKey: 'reference_code')
189+
client.create_index!('books', primary_key: 'reference_code')
190190
index = client.fetch_index('books')
191191
raw_response = index.fetch_raw_info
192192

@@ -202,7 +202,7 @@
202202

203203
describe '#index' do
204204
it 'returns an index object with the provided uid' do
205-
client.create_index!('books', primaryKey: 'reference_code')
205+
client.create_index!('books', primary_key: 'reference_code')
206206
# this index is in memory, without metadata from server
207207
index = client.index('books')
208208

spec/meilisearch/client/keys_spec.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,15 @@
88
description: 'A new key to delete docs',
99
actions: ['documents.delete'],
1010
indexes: ['*'],
11-
expiresAt: nil
11+
expires_at: nil
1212
}
1313
end
1414
let(:add_docs_key_options) do
1515
{
1616
description: 'A new key to add docs',
1717
actions: ['documents.add'],
1818
indexes: ['*'],
19-
expiresAt: nil
19+
expires_at: nil
2020
}
2121
end
2222

spec/meilisearch/client/requests_spec.rb

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,4 @@
1616
expect(index.uid).to eq(key)
1717
expect(index.primary_key).to eq(key)
1818
end
19-
20-
it 'parses options when they are in a different shape' do
21-
client.create_index!(key, priMARy_kEy: key)
22-
23-
index = client.fetch_index(key)
24-
expect(index.uid).to eq(key)
25-
expect(index.primary_key).to eq(key)
26-
end
2719
end

spec/meilisearch/index/base_spec.rb

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
end
1616

1717
it 'fetch the raw Hash info of the index' do
18-
client.create_index!('books', primaryKey: 'reference_number')
18+
client.create_index!('books', primary_key: 'reference_number')
1919

2020
raw_index = client.fetch_raw_index('books')
2121

@@ -37,7 +37,7 @@
3737
end
3838

3939
it 'get primary-key of index if it exists' do
40-
client.create_index!('index_with_prirmary_key', primaryKey: 'primary_key')
40+
client.create_index!('index_with_prirmary_key', primary_key: 'primary_key')
4141

4242
index = client.fetch_index('index_with_prirmary_key')
4343
expect(index.primary_key).to eq('primary_key')
@@ -54,7 +54,7 @@
5454
it 'updates primary-key of index if not defined before' do
5555
client.create_index!('uid')
5656

57-
task = client.index('uid').update(primaryKey: 'new_primary_key')
57+
task = client.index('uid').update(primary_key: 'new_primary_key')
5858
expect(task['type']).to eq('indexUpdate')
5959
client.wait_for_task(task['taskUid'])
6060

@@ -70,9 +70,9 @@
7070
end
7171

7272
it 'updates primary-key of index if has been defined before but there is not docs' do
73-
client.create_index!('books', primaryKey: 'reference_number')
73+
client.create_index!('books', primary_key: 'reference_number')
7474

75-
task = client.index('books').update(primaryKey: 'international_standard_book_number')
75+
task = client.index('books').update(primary_key: 'international_standard_book_number')
7676
expect(task['type']).to eq('indexUpdate')
7777
client.wait_for_task(task['taskUid'])
7878

@@ -91,7 +91,7 @@
9191
index = client.index('uid')
9292
index.add_documents!({ id: 1, title: 'My Title' })
9393

94-
task = index.update(primaryKey: 'new_primary_key')
94+
task = index.update(primary_key: 'new_primary_key')
9595
expect(task['type']).to eq('indexUpdate')
9696
achieved_task = client.wait_for_task(task['taskUid'])
9797

@@ -177,7 +177,7 @@
177177
end
178178

179179
it 'works with method aliases' do
180-
client.create_index!('uid', primaryKey: 'primary_key')
180+
client.create_index!('uid', primary_key: 'primary_key')
181181

182182
index = client.fetch_index('uid')
183183
expect(index.method(:fetch_primary_key) == index.method(:get_primary_key)).to be_truthy

spec/meilisearch/index/search/attributes_to_crop_spec.rb

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,23 +13,23 @@
1313
before { index.add_documents!(document) }
1414

1515
it 'searches with default cropping params' do
16-
response = index.search('galaxy', attributesToCrop: ['*'], cropLength: 6)
16+
response = index.search('galaxy', attributes_to_crop: ['*'], crop_length: 6)
1717

1818
expect(response.dig('hits', 0, '_formatted', 'description')).to eq('…Guide to the Galaxy is a…')
1919
end
2020

2121
it 'searches with custom crop markers' do
22-
response = index.search('galaxy', attributesToCrop: ['*'], cropLength: 6, cropMarker: '(ꈍᴗꈍ)')
22+
response = index.search('galaxy', attributes_to_crop: ['*'], crop_length: 6, crop_marker: '(ꈍᴗꈍ)')
2323

2424
expect(response.dig('hits', 0, '_formatted', 'description')).to eq('(ꈍᴗꈍ)Guide to the Galaxy is a(ꈍᴗꈍ)')
2525
end
2626

2727
it 'searches with mixed highlight and crop config' do
2828
response = index.search(
2929
'galaxy',
30-
attributesToHighlight: ['*'],
31-
attributesToCrop: ['*'],
32-
highlightPreTag: '<span class="bold">'
30+
attributes_to_highlight: ['*'],
31+
attributes_to_crop: ['*'],
32+
highlight_pre_tag: '<span class="bold">'
3333
)
3434

3535
expect(response.dig('hits', 0, '_formatted', 'description')).to \
@@ -39,22 +39,22 @@
3939
it 'searches with highlight tags' do
4040
response = index.search(
4141
'galaxy',
42-
attributesToHighlight: ['*'],
43-
highlightPreTag: '<span>',
44-
highlightPostTag: '</span>'
42+
attributes_to_highlight: ['*'],
43+
highlight_pre_tag: '<span>',
44+
highlight_post_tag: '</span>'
4545
)
4646

4747
expect(response.dig('hits', 0, '_formatted', 'description')).to include('<span>Galaxy</span>')
4848
end
4949

5050
it 'does a custom search with attributes to crop' do
51-
response = index.search('galaxy', { attributesToCrop: ['description'], cropLength: 6 })
51+
response = index.search('galaxy', { attributes_to_crop: ['description'], crop_length: 6 })
5252
expect(response['hits'].first).to have_key('_formatted')
5353
expect(response['hits'].first['_formatted']['description']).to eq('…Guide to the Galaxy is a…')
5454
end
5555

5656
it 'does a placehodler search with attributes to crop' do
57-
response = index.search('', { attributesToCrop: ['description'], cropLength: 5 })
57+
response = index.search('', { attributes_to_crop: ['description'], crop_length: 5 })
5858
expect(response['hits'].first).to have_key('_formatted')
5959
expect(response['hits'].first['description']).to eq(document[:description])
6060
expect(response['hits'].first['_formatted']['description']).to eq("The Hitchhiker's Guide to…")

0 commit comments

Comments
 (0)