Skip to content

Commit

Permalink
Merge #585
Browse files Browse the repository at this point in the history
585: Rename top level module r=curquiza a=ellnix

# Pull Request

## Related issue
Fixes #523

Co-authored-by: ellnix <[email protected]>
  • Loading branch information
meili-bors[bot] and ellnix authored Jan 10, 2025
2 parents 884f46d + 1ffa380 commit 4d7a593
Show file tree
Hide file tree
Showing 53 changed files with 238 additions and 185 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ gem 'meilisearch'
```ruby
require 'meilisearch'

client = MeiliSearch::Client.new('http://127.0.0.1:7700', 'masterKey')
client = Meilisearch::Client.new('http://127.0.0.1:7700', 'masterKey')

# An index is where the documents are stored.
index = client.index('movies')
Expand Down
35 changes: 35 additions & 0 deletions lib/meilisearch.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,40 @@
require 'meilisearch/client'
require 'meilisearch/index'

module Meilisearch
end

# Softly deprecate the old spelling of the top level module
# from MeiliSearch to Meilisearch
module MeiliSearch
class << self
def const_missing(const_name)
_warn_about_deprecation

Meilisearch.const_get(const_name)
end

def method_missing(method_name, *args, **kwargs)
_warn_about_deprecation

Meilisearch.send(method_name, *args, **kwargs)
end

def respond_to_missing?(method_name, *)
Meilisearch.respond_to?(method_name) || super
end

private

def _warn_about_deprecation
return if @warned

Meilisearch::Utils.logger.warn <<~RENAMED_MODULE_WARNING
[meilisearch-ruby] The top-level module of Meilisearch has been renamed.
[meilisearch-ruby] Please update "MeiliSearch" to "Meilisearch".
RENAMED_MODULE_WARNING

@warned = true
end
end
end
6 changes: 3 additions & 3 deletions lib/meilisearch/client.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
# frozen_string_literal: true

module MeiliSearch
module Meilisearch
class Client < HTTPRequest
include MeiliSearch::TenantToken
include MeiliSearch::MultiSearch
include Meilisearch::TenantToken
include Meilisearch::MultiSearch

### INDEXES

Expand Down
26 changes: 13 additions & 13 deletions lib/meilisearch/error.rb
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
# frozen_string_literal: true

module MeiliSearch
module Meilisearch
class Error < StandardError
end

class ApiError < Error
# :http_code # e.g. 400, 404...
# :http_message # e.g. Bad Request, Not Found...
# :http_body # The response body received from the MeiliSearch API
# :ms_code # The error code given by the MeiliSearch API
# :ms_type # The error type given by the MeiliSearch API
# :ms_link # The documentation link given by the MeiliSearch API
# :ms_message # The error message given by the MeiliSearch API
# :http_body # The response body received from the Meilisearch API
# :ms_code # The error code given by the Meilisearch API
# :ms_type # The error type given by the Meilisearch API
# :ms_link # The documentation link given by the Meilisearch API
# :ms_message # The error message given by the Meilisearch API
# :message # The detailed error message of this error class

attr_reader :http_code, :http_message, :http_body, :ms_code, :ms_type, :ms_link, :ms_message, :message
Expand All @@ -26,7 +26,7 @@ def initialize(http_code, http_message, http_body)
@http_body = parse_body(http_body)
@ms_code = @http_body['code']
@ms_type = @http_body['type']
@ms_message = @http_body.fetch('message', 'MeiliSearch API has not returned any error message')
@ms_message = @http_body.fetch('message', 'Meilisearch API has not returned any error message')
@ms_link = @http_body.fetch('link', '<no documentation link found>')
@message = "#{http_code} #{http_message} - #{@ms_message}. See #{ms_link}."
super(details)
Expand All @@ -41,22 +41,22 @@ def parse_body(http_body)
{}
end
rescue JSON::ParserError
# We might receive a JSON::ParserError when, for example, MeiliSearch is running behind
# We might receive a JSON::ParserError when, for example, Meilisearch is running behind
# some proxy (ELB or Nginx, for example), and the request timeouts, returning us
# a raw HTML body instead of a JSON as we were expecting
{ 'message' => "The server has not returned a valid JSON HTTP body: #{http_body}" }
end

def details
"MeiliSearch::ApiError - code: #{@ms_code} - type: #{ms_type} - message: #{@ms_message} - link: #{ms_link}"
"Meilisearch::ApiError - code: #{@ms_code} - type: #{ms_type} - message: #{@ms_message} - link: #{ms_link}"
end
end

class CommunicationError < Error
attr_reader :message

def initialize(message)
@message = "An error occurred while trying to connect to the MeiliSearch instance: #{message}"
@message = "An error occurred while trying to connect to the Meilisearch instance: #{message}"
super(@message)
end
end
Expand All @@ -80,8 +80,8 @@ def initialize(message = nil)
end

module TenantToken
class ExpireOrInvalidSignature < MeiliSearch::Error; end
class InvalidApiKey < MeiliSearch::Error; end
class InvalidSearchRules < MeiliSearch::Error; end
class ExpireOrInvalidSignature < Meilisearch::Error; end
class InvalidApiKey < Meilisearch::Error; end
class InvalidSearchRules < Meilisearch::Error; end
end
end
4 changes: 2 additions & 2 deletions lib/meilisearch/http_request.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
require 'httparty'
require 'meilisearch/error'

module MeiliSearch
module Meilisearch
class HTTPRequest
include HTTParty

Expand Down Expand Up @@ -93,7 +93,7 @@ def build_default_options_headers
'Authorization' => ("Bearer #{@api_key}" unless @api_key.nil?),
'User-Agent' => [
@options.fetch(:client_agents, []),
MeiliSearch.qualified_version
Meilisearch.qualified_version
].flatten.join(';')
}.compact
end
Expand Down
4 changes: 2 additions & 2 deletions lib/meilisearch/index.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

require 'meilisearch/http_request'

module MeiliSearch
module Meilisearch
class Index < HTTPRequest
attr_reader :uid, :primary_key, :created_at, :updated_at

Expand Down Expand Up @@ -233,7 +233,7 @@ def delete_documents!(documents_ids)

def delete_document(document_id)
if document_id.nil? || document_id.to_s.empty?
raise MeiliSearch::InvalidDocumentId, 'document_id cannot be empty or nil'
raise Meilisearch::InvalidDocumentId, 'document_id cannot be empty or nil'
end

encode_document = URI.encode_www_form_component(document_id)
Expand Down
4 changes: 2 additions & 2 deletions lib/meilisearch/models/task.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

require 'forwardable'

module MeiliSearch
module Meilisearch
module Models
class Task
extend Forwardable
Expand Down Expand Up @@ -88,7 +88,7 @@ def refresh(with: nil)
self.metadata = with || @task_endpoint.task(uid)

self
rescue MeiliSearch::ApiError => e
rescue Meilisearch::ApiError => e
raise e unless e.http_code == 404

@deleted = true
Expand Down
2 changes: 1 addition & 1 deletion lib/meilisearch/multi_search.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# frozen_string_literal: true

module MeiliSearch
module Meilisearch
module MultiSearch
def multi_search(data)
body = Utils.transform_attributes(data)
Expand Down
4 changes: 2 additions & 2 deletions lib/meilisearch/task.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
require 'meilisearch/http_request'
require 'timeout'

module MeiliSearch
module Meilisearch
class Task < HTTPRequest
ALLOWED_PARAMS = [
:limit, :from, :index_uids, :types, :statuses, :uids, :canceled_by,
Expand Down Expand Up @@ -46,7 +46,7 @@ def wait_for_task(task_uid, timeout_in_ms = 5000, interval_in_ms = 50)
end
end
rescue Timeout::Error
raise MeiliSearch::TimeoutError
raise Meilisearch::TimeoutError
end

private
Expand Down
2 changes: 1 addition & 1 deletion lib/meilisearch/tenant_token.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# frozen_string_literal: true

module MeiliSearch
module Meilisearch
module TenantToken
HEADER = {
typ: 'JWT',
Expand Down
6 changes: 3 additions & 3 deletions lib/meilisearch/utils.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

require 'logger'

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

Expand Down Expand Up @@ -57,10 +57,10 @@ def parse_query(original_options, allowed_params = [])

def version_error_handler(method_name)
yield if block_given?
rescue MeiliSearch::ApiError => e
rescue Meilisearch::ApiError => e
message = message_builder(e.http_message, method_name)

raise MeiliSearch::ApiError.new(e.http_code, message, e.http_body)
raise Meilisearch::ApiError.new(e.http_code, message, e.http_body)
rescue StandardError => e
raise e.class, message_builder(e.message, method_name)
end
Expand Down
2 changes: 1 addition & 1 deletion lib/meilisearch/version.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# frozen_string_literal: true

module MeiliSearch
module Meilisearch
VERSION = '0.29.0'

def self.qualified_version
Expand Down
2 changes: 1 addition & 1 deletion meilisearch.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ require File.join(File.dirname(__FILE__), 'lib', 'meilisearch', 'version')

Gem::Specification.new do |s|
s.name = 'meilisearch'
s.version = MeiliSearch::VERSION
s.version = Meilisearch::VERSION
s.authors = ['Meili']
s.email = '[email protected]'
s.summary = 'An easy-to-use ruby client for Meilisearch API'
Expand Down
2 changes: 1 addition & 1 deletion spec/meilisearch/client/dumps_spec.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# frozen_string_literal: true

RSpec.describe 'MeiliSearch::Client - Dumps' do
RSpec.describe 'Meilisearch::Client - Dumps' do
it 'creates a new dump' do
expect(client.create_dump.await).to be_succeeded
end
Expand Down
30 changes: 15 additions & 15 deletions spec/meilisearch/client/errors_spec.rb
Original file line number Diff line number Diff line change
@@ -1,44 +1,44 @@
# frozen_string_literal: true

RSpec.describe 'MeiliSearch::Client - Errors' do
describe 'MeiliSearch::Error' do
RSpec.describe 'Meilisearch::Client - Errors' do
describe 'Meilisearch::Error' do
it 'catches all other errors' do
expect(MeiliSearch::TimeoutError.ancestors).to include(MeiliSearch::Error)
expect(MeiliSearch::CommunicationError.ancestors).to include(MeiliSearch::Error)
expect(MeiliSearch::ApiError.ancestors).to include(MeiliSearch::Error)
expect(MeiliSearch::TenantToken::InvalidApiKey.ancestors).to include(MeiliSearch::Error)
expect(MeiliSearch::TenantToken::InvalidSearchRules.ancestors).to include(MeiliSearch::Error)
expect(MeiliSearch::TenantToken::ExpireOrInvalidSignature.ancestors).to include(MeiliSearch::Error)
expect(Meilisearch::TimeoutError.ancestors).to include(Meilisearch::Error)
expect(Meilisearch::CommunicationError.ancestors).to include(Meilisearch::Error)
expect(Meilisearch::ApiError.ancestors).to include(Meilisearch::Error)
expect(Meilisearch::TenantToken::InvalidApiKey.ancestors).to include(Meilisearch::Error)
expect(Meilisearch::TenantToken::InvalidSearchRules.ancestors).to include(Meilisearch::Error)
expect(Meilisearch::TenantToken::ExpireOrInvalidSignature.ancestors).to include(Meilisearch::Error)
end
end

context 'when request takes to long to answer' do
it 'raises MeiliSearch::TimeoutError' do
timed_client = MeiliSearch::Client.new(URL, MASTER_KEY, { timeout: 0.000001 })
it 'raises Meilisearch::TimeoutError' do
timed_client = Meilisearch::Client.new(URL, MASTER_KEY, { timeout: 0.000001 })

expect do
timed_client.version
end.to raise_error(MeiliSearch::TimeoutError)
end.to raise_error(Meilisearch::TimeoutError)
end
end

context 'when body is too large' do
let(:index) { client.index('movies') }

it 'raises MeiliSearch::CommunicationError' do
it 'raises Meilisearch::CommunicationError' do
allow(index.class).to receive(:post).and_raise(Errno::EPIPE)

expect do
index.add_documents([{ id: 1, text: 'my_text' }])
end.to raise_error(MeiliSearch::CommunicationError)
end.to raise_error(Meilisearch::CommunicationError)
end
end

context 'when document id is invalid' do
it 'raises MeiliSearch::InvalidDocumentId' do
it 'raises Meilisearch::InvalidDocumentId' do
expect do
client.index('movies').delete_document(nil)
end.to raise_error(MeiliSearch::InvalidDocumentId)
end.to raise_error(Meilisearch::InvalidDocumentId)
end
end
end
6 changes: 3 additions & 3 deletions spec/meilisearch/client/health_spec.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
# frozen_string_literal: true

RSpec.describe 'MeiliSearch::Client - Health' do
let(:client) { MeiliSearch::Client.new(URL, MASTER_KEY) }
let(:wrong_client) { MeiliSearch::Client.new('bad_url') }
RSpec.describe 'Meilisearch::Client - Health' do
let(:client) { Meilisearch::Client.new(URL, MASTER_KEY) }
let(:wrong_client) { Meilisearch::Client.new('bad_url') }

it 'is healthy when the url is valid' do
expect(client.healthy?).to be true
Expand Down
Loading

0 comments on commit 4d7a593

Please sign in to comment.