Skip to content

Commit c8144ac

Browse files
committed
Move out params parsing of the HttpRequest class
This way we would gain more control about when parse something or not that even could be a query_params situation or a body situation. Fixes #265
1 parent a6a53d6 commit c8144ac

File tree

8 files changed

+79
-29
lines changed

8 files changed

+79
-29
lines changed

lib/meilisearch.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# frozen_string_literal: true
22

33
require 'meilisearch/version'
4+
require 'meilisearch/utils'
45
require 'meilisearch/client'
56
require 'meilisearch/index'
67

lib/meilisearch/client.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@ def indexes
2020
# client.create_index('indexUID')
2121
# client.create_index('indexUID', primaryKey: 'id')
2222
def create_index(index_uid, options = {})
23-
body = options.merge(uid: index_uid)
23+
body = Utils.transform_attributes(options.merge(uid: index_uid))
24+
2425
index_hash = http_post '/indexes', body
2526
index_object(index_hash['uid'], index_hash['primaryKey'])
2627
end

lib/meilisearch/http_request.rb

Lines changed: 1 addition & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,6 @@ def http_delete(relative_path = '')
6161

6262
private
6363

64-
SNAKE_CASE = /[^a-zA-Z0-9]+(.)/.freeze
65-
6664
def send_request(http_method, relative_path, query_params: nil, body: nil, headers: nil)
6765
config = http_config(query_params, body, headers)
6866
begin
@@ -74,35 +72,15 @@ def send_request(http_method, relative_path, query_params: nil, body: nil, heade
7472
end
7573

7674
def http_config(query_params, body, headers)
77-
body = transform_attributes(body).to_json
7875
{
7976
headers: headers,
8077
query: query_params,
81-
body: body,
78+
body: body.to_json,
8279
timeout: @options[:timeout] || 1,
8380
max_retries: @options[:max_retries] || 0
8481
}.compact
8582
end
8683

87-
def transform_attributes(body)
88-
case body
89-
when Array
90-
body.map { |item| transform_attributes(item) }
91-
when Hash
92-
parse(body)
93-
else
94-
body
95-
end
96-
end
97-
98-
def parse(body)
99-
body
100-
.transform_keys(&:to_s)
101-
.transform_keys do |key|
102-
key.include?('_') ? key.downcase.gsub(SNAKE_CASE, &:upcase).gsub('_', '') : key
103-
end
104-
end
105-
10684
def validate(response)
10785
raise ApiError.new(response.code, response.message, response.body) unless response.success?
10886

lib/meilisearch/index.rb

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,9 @@ def fetch_raw_info
3131
end
3232

3333
def update(body)
34-
index_hash = http_put indexes_path(id: @uid), body
34+
index_hash = http_put indexes_path(id: @uid), Utils.transform_attributes(body)
3535
set_base_properties index_hash
36+
3637
self
3738
end
3839

@@ -65,7 +66,7 @@ def document(document_id)
6566
alias get_one_document document
6667

6768
def documents(options = {})
68-
http_get "/indexes/#{@uid}/documents", options
69+
http_get "/indexes/#{@uid}/documents", Utils.transform_attributes(options)
6970
end
7071
alias get_documents documents
7172

@@ -168,8 +169,9 @@ def delete_all_documents!
168169
### SEARCH
169170

170171
def search(query, options = {})
171-
parsed_options = options.compact
172-
http_post "/indexes/#{@uid}/search", { q: query.to_s }.merge(parsed_options)
172+
parsed_options = Utils.transform_attributes({ q: query.to_s }.merge(options.compact))
173+
174+
http_post "/indexes/#{@uid}/search", parsed_options
173175
end
174176

175177
### UPDATES
@@ -229,7 +231,7 @@ def settings
229231
alias get_settings settings
230232

231233
def update_settings(settings)
232-
http_post "/indexes/#{@uid}/settings", settings
234+
http_post "/indexes/#{@uid}/settings", Utils.transform_attributes(settings)
233235
end
234236
alias settings= update_settings
235237

lib/meilisearch/utils.rb

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# frozen_string_literal: true
2+
3+
module MeiliSearch
4+
module Utils
5+
SNAKE_CASE = /[^a-zA-Z0-9]+(.)/.freeze
6+
7+
def self.transform_attributes(body)
8+
case body
9+
when Array
10+
body.map { |item| transform_attributes(item) }
11+
when Hash
12+
parse(body)
13+
else
14+
body
15+
end
16+
end
17+
18+
def self.parse(body)
19+
body
20+
.transform_keys(&:to_s)
21+
.transform_keys do |key|
22+
key.include?('_') ? key.downcase.gsub(SNAKE_CASE, &:upcase).gsub('_', '') : key
23+
end
24+
end
25+
26+
private_class_method :parse
27+
end
28+
end

spec/meilisearch/index/base_spec.rb

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,4 +126,16 @@
126126
expect(index.method(:update) == index.method(:update_index)).to be_truthy
127127
expect(index.method(:delete) == index.method(:delete_index)).to be_truthy
128128
end
129+
130+
context 'with snake_case options' do
131+
it 'does the request with camelCase attributes' do
132+
index = client.create_index('uid')
133+
index.update(primary_key: 'new_primary_key')
134+
135+
expect(index).to be_a(MeiliSearch::Index)
136+
expect(index.uid).to eq('uid')
137+
expect(index.primary_key).to eq('new_primary_key')
138+
expect(index.fetch_primary_key).to eq('new_primary_key')
139+
end
140+
end
129141
end

spec/meilisearch/index/documents_spec.rb

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,17 @@
2525
expect(index.documents.count).to eq(documents.count)
2626
end
2727

28+
it 'keeps the structure of the original documents' do
29+
docs = [
30+
{ object_id: 123, my_title: 'Pride and Prejudice', 'my-comment': 'A great book' }
31+
]
32+
33+
response = index.add_documents(docs)
34+
index.wait_for_pending_update(response['updateId'])
35+
36+
expect(index.documents.first.keys).to eq(docs.first.keys.map(&:to_s))
37+
end
38+
2839
it 'adds documents in a batch (as a array of documents)' do
2940
response = index.add_documents_in_batches(documents, 5)
3041
expect(response).to be_a(Array)

spec/meilisearch/index/settings_spec.rb

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,23 @@
8989
expect(settings['stopWords']).to be_empty
9090
expect(settings['synonyms']).to be_empty
9191
end
92+
93+
context 'with snake_case options' do
94+
it 'does the request with camelCase attributes' do
95+
response = index.update_settings(
96+
ranking_rules: ['typo'],
97+
distinct_ATTribute: 'title',
98+
stopWords: ['a']
99+
)
100+
101+
index.wait_for_pending_update(response['updateId'])
102+
settings = index.settings
103+
104+
expect(settings['rankingRules']).to eq(['typo'])
105+
expect(settings['distinctAttribute']).to eq('title')
106+
expect(settings['stopWords']).to eq(['a'])
107+
end
108+
end
92109
end
93110

94111
context 'On ranking-rules sub-routes' do

0 commit comments

Comments
 (0)