Skip to content

Commit

Permalink
Move out params parsing of the HttpRequest class
Browse files Browse the repository at this point in the history
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
  • Loading branch information
brunoocasali committed Nov 18, 2021
1 parent 1ddbc92 commit d9f7ad4
Show file tree
Hide file tree
Showing 7 changed files with 65 additions and 28 deletions.
1 change: 1 addition & 0 deletions lib/meilisearch.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# frozen_string_literal: true

require 'meilisearch/version'
require 'meilisearch/utils'
require 'meilisearch/client'
require 'meilisearch/index'

Expand Down
3 changes: 2 additions & 1 deletion lib/meilisearch/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ def indexes
# client.create_index('indexUID')
# client.create_index('indexUID', primaryKey: 'id')
def create_index(index_uid, options = {})
body = options.merge(uid: index_uid)
body = Utils.transform_attributes(options.merge(uid: index_uid))

index_hash = http_post '/indexes', body
index_object(index_hash['uid'], index_hash['primaryKey'])
end
Expand Down
24 changes: 1 addition & 23 deletions lib/meilisearch/http_request.rb
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,6 @@ def http_delete(relative_path = '')

private

SNAKE_CASE = /[^a-zA-Z0-9]+(.)/.freeze

def send_request(http_method, relative_path, query_params: nil, body: nil, headers: nil)
config = http_config(query_params, body, headers)
begin
Expand All @@ -74,35 +72,15 @@ def send_request(http_method, relative_path, query_params: nil, body: nil, heade
end

def http_config(query_params, body, headers)
body = transform_attributes(body).to_json
{
headers: headers,
query: query_params,
body: body,
body: body.to_json,
timeout: @options[:timeout] || 1,
max_retries: @options[:max_retries] || 0
}.compact
end

def transform_attributes(body)
case body
when Array
body.map { |item| transform_attributes(item) }
when Hash
parse(body)
else
body
end
end

def parse(body)
body
.transform_keys(&:to_s)
.transform_keys do |key|
key.include?('_') ? key.downcase.gsub(SNAKE_CASE, &:upcase).gsub('_', '') : key
end
end

def validate(response)
raise ApiError.new(response.code, response.message, response.body) unless response.success?

Expand Down
9 changes: 5 additions & 4 deletions lib/meilisearch/index.rb
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ def document(document_id)
alias get_one_document document

def documents(options = {})
http_get "/indexes/#{@uid}/documents", options
http_get "/indexes/#{@uid}/documents", Utils.transform_attributes(options)
end
alias get_documents documents

Expand Down Expand Up @@ -168,8 +168,9 @@ def delete_all_documents!
### SEARCH

def search(query, options = {})
parsed_options = options.compact
http_post "/indexes/#{@uid}/search", { q: query.to_s }.merge(parsed_options)
parsed_options = Utils.transform_attributes({ q: query.to_s }.merge(options.compact))

http_post "/indexes/#{@uid}/search", parsed_options
end

### UPDATES
Expand Down Expand Up @@ -229,7 +230,7 @@ def settings
alias get_settings settings

def update_settings(settings)
http_post "/indexes/#{@uid}/settings", settings
http_post "/indexes/#{@uid}/settings", Utils.transform_attributes(settings)
end
alias settings= update_settings

Expand Down
28 changes: 28 additions & 0 deletions lib/meilisearch/utils.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# frozen_string_literal: true

module MeiliSearch
module Utils
SNAKE_CASE = /[^a-zA-Z0-9]+(.)/.freeze

def self.transform_attributes(body)
case body
when Array
body.map { |item| transform_attributes(item) }
when Hash
parse(body)
else
body
end
end

def self.parse(body)
body
.transform_keys(&:to_s)
.transform_keys do |key|
key.include?('_') ? key.downcase.gsub(SNAKE_CASE, &:upcase).gsub('_', '') : key
end
end

private_class_method :parse
end
end
11 changes: 11 additions & 0 deletions spec/meilisearch/index/documents_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,17 @@
expect(index.documents.count).to eq(documents.count)
end

it 'keeps the structure of the original documents' do
docs = [
{ object_id: 123, my_title: 'Pride and Prejudice', 'my-comment': 'A great book' }
]

response = index.add_documents(docs)
index.wait_for_pending_update(response['updateId'])

expect(index.documents.first.keys).to eq(docs.first.keys.map(&:to_s))
end

it 'adds documents in a batch (as a array of documents)' do
response = index.add_documents_in_batches(documents, 5)
expect(response).to be_a(Array)
Expand Down
17 changes: 17 additions & 0 deletions spec/meilisearch/index/settings_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,23 @@
expect(settings['stopWords']).to be_empty
expect(settings['synonyms']).to be_empty
end

context 'with snake_case options' do
it 'does the request with camelCase attributes' do
response = index.update_settings(
ranking_rules: ['typo'],
distinct_ATTribute: 'title',
stopWords: ['a']
)

index.wait_for_pending_update(response['updateId'])
settings = index.settings

expect(settings['rankingRules']).to eq(['typo'])
expect(settings['distinctAttribute']).to eq('title')
expect(settings['stopWords']).to eq(['a'])
end
end
end

context 'On ranking-rules sub-routes' do
Expand Down

0 comments on commit d9f7ad4

Please sign in to comment.