meilisearch 0.11.1 → 0.12.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/meilisearch/client.rb +9 -2
- data/lib/meilisearch/error.rb +43 -30
- data/lib/meilisearch/http_request.rb +42 -36
- data/lib/meilisearch/index.rb +1 -1
- data/lib/meilisearch/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 17248185a6d2ba10e2e5fac61b6d9d07a0cac218a2accc4cd5b3bd130517f837
|
4
|
+
data.tar.gz: dd20f35451242677ec05ac52d95cbc14915dea6676562ef23c58a58655db24ad
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: f0c0c7a6726257c62e14108f33df551cd5ec3d7c5f4b6f8eb3fa975988da3915d7b3167e0074c3e21134f49d1a40d84a55e4145ae7b4ffac45cd1daa8954e00d
|
7
|
+
data.tar.gz: 3cb711d0577b92fe428b90ec16d2cbc7fb593aba63f014206c17a88bca2ba4de0fdd58b51e2b014640ae7118996142b9edc4c4d4d00250fc8a6893df117e3527
|
data/lib/meilisearch/client.rb
CHANGED
@@ -23,6 +23,15 @@ module MeiliSearch
|
|
23
23
|
index_object(res['uid'])
|
24
24
|
end
|
25
25
|
|
26
|
+
def get_or_create_index(index_uid, options = {})
|
27
|
+
begin
|
28
|
+
create_index(index_uid, options)
|
29
|
+
rescue ApiError => e
|
30
|
+
raise e unless e.code == 'index_already_exists'
|
31
|
+
end
|
32
|
+
index_object(index_uid)
|
33
|
+
end
|
34
|
+
|
26
35
|
def delete_index(index_uid)
|
27
36
|
index_object(index_uid).delete
|
28
37
|
end
|
@@ -30,8 +39,6 @@ module MeiliSearch
|
|
30
39
|
# Usage:
|
31
40
|
# client.index('indexUID')
|
32
41
|
def index(index_uid)
|
33
|
-
raise IndexUidError if index_uid.nil?
|
34
|
-
|
35
42
|
index_object(index_uid)
|
36
43
|
end
|
37
44
|
alias get_index index
|
data/lib/meilisearch/error.rb
CHANGED
@@ -1,45 +1,58 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
3
|
module MeiliSearch
|
4
|
-
class
|
5
|
-
|
6
|
-
|
7
|
-
attr_reader :
|
4
|
+
class ApiError < StandardError
|
5
|
+
attr_reader :http_code # e.g. 400, 404...
|
6
|
+
attr_reader :http_message # e.g. Bad Request, Not Found...
|
7
|
+
attr_reader :http_body # The response body received from the MeiliSearch API
|
8
|
+
attr_reader :ms_code # The error code given by the MeiliSearch API
|
9
|
+
attr_reader :ms_type # The error type given by the MeiliSearch API
|
10
|
+
attr_reader :ms_link # The documentation link given by the MeiliSearch API
|
11
|
+
attr_reader :ms_message # The error message given by the MeiliSearch API
|
12
|
+
attr_reader :message # The detailed error message of this error class
|
8
13
|
|
9
|
-
|
10
|
-
|
14
|
+
alias code ms_code
|
15
|
+
alias type ms_type
|
16
|
+
alias link ms_link
|
17
|
+
|
18
|
+
def initialize(http_code, http_message, http_body)
|
19
|
+
get_meilisearch_error_info(http_body) unless http_body.nil? || http_body.empty?
|
20
|
+
@http_code = http_code
|
21
|
+
@http_message = http_message
|
22
|
+
@ms_message ||= 'MeiliSearch API has not returned any error message'
|
23
|
+
@ms_link ||= '<no documentation link found>'
|
24
|
+
@message = "#{http_code} #{http_message} - #{@ms_message.capitalize}. See #{ms_link}."
|
25
|
+
super(details)
|
26
|
+
end
|
27
|
+
|
28
|
+
def get_meilisearch_error_info(http_body)
|
29
|
+
@http_body = JSON.parse(http_body)
|
30
|
+
@ms_code = @http_body['errorCode']
|
31
|
+
@ms_message = @http_body['message']
|
32
|
+
@ms_type = @http_body['errorType']
|
33
|
+
@ms_link = @http_body['errorLink']
|
11
34
|
end
|
12
35
|
|
13
|
-
def
|
14
|
-
"#{@message}
|
36
|
+
def details
|
37
|
+
"MeiliSearch::ApiError - code: #{@ms_code} - type: #{ms_type} - message: #{@ms_message} - link: #{ms_link}"
|
15
38
|
end
|
16
39
|
end
|
17
40
|
|
18
|
-
class
|
19
|
-
attr_reader :status
|
41
|
+
class CommunicationError < StandardError
|
20
42
|
attr_reader :message
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
alias code status
|
26
|
-
alias body http_body
|
27
|
-
alias body_message http_body_message
|
28
|
-
|
29
|
-
def initialize(status, message, http_body, details = nil)
|
30
|
-
@status = status
|
31
|
-
unless http_body.nil? || http_body.empty?
|
32
|
-
@http_body = JSON.parse(http_body)
|
33
|
-
@http_body_message = @http_body['message']
|
34
|
-
end
|
35
|
-
@message = message.capitalize
|
36
|
-
@message = "#{@message} - #{@http_body_message.capitalize}" unless @http_body_message.nil?
|
37
|
-
@details = details
|
43
|
+
|
44
|
+
def initialize(message)
|
45
|
+
@message = "An error occurred while trying to connect to the MeiliSearch instance: #{message}"
|
46
|
+
super(@message)
|
38
47
|
end
|
48
|
+
end
|
39
49
|
|
40
|
-
|
41
|
-
|
42
|
-
|
50
|
+
class TimeoutError < StandardError
|
51
|
+
attr_reader :message
|
52
|
+
|
53
|
+
def initialize
|
54
|
+
@message = 'The update was not processed in the expected time'
|
55
|
+
super(@message)
|
43
56
|
end
|
44
57
|
end
|
45
58
|
end
|
@@ -16,57 +16,63 @@ module MeiliSearch
|
|
16
16
|
}.compact
|
17
17
|
end
|
18
18
|
|
19
|
-
def http_get(
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
timeout: 1
|
19
|
+
def http_get(relative_path = '', query_params = {})
|
20
|
+
send_request(
|
21
|
+
proc { |path, config| self.class.get(path, config) },
|
22
|
+
relative_path,
|
23
|
+
query_params
|
25
24
|
)
|
26
|
-
validate(response)
|
27
25
|
end
|
28
26
|
|
29
|
-
def http_post(
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
query: query_params,
|
36
|
-
headers: @headers,
|
37
|
-
timeout: 1
|
38
|
-
}.compact
|
27
|
+
def http_post(relative_path = '', body = nil, query_params = nil)
|
28
|
+
send_request(
|
29
|
+
proc { |path, config| self.class.post(path, config) },
|
30
|
+
relative_path,
|
31
|
+
query_params,
|
32
|
+
body
|
39
33
|
)
|
40
|
-
validate(response)
|
41
34
|
end
|
42
35
|
|
43
|
-
def http_put(
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
query: query_params,
|
50
|
-
headers: @headers,
|
51
|
-
timeout: 1
|
52
|
-
}.compact
|
36
|
+
def http_put(relative_path = '', body = nil, query_params = nil)
|
37
|
+
send_request(
|
38
|
+
proc { |path, config| self.class.put(path, config) },
|
39
|
+
relative_path,
|
40
|
+
query_params,
|
41
|
+
body
|
53
42
|
)
|
54
|
-
validate(response)
|
55
43
|
end
|
56
44
|
|
57
|
-
def http_delete(
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
timeout: 1
|
45
|
+
def http_delete(relative_path = '')
|
46
|
+
send_request(
|
47
|
+
proc { |path, config| self.class.delete(path, config) },
|
48
|
+
relative_path
|
62
49
|
)
|
63
|
-
validate(response)
|
64
50
|
end
|
65
51
|
|
66
52
|
private
|
67
53
|
|
54
|
+
def send_request(http_method, relative_path, query_params = nil, body = nil)
|
55
|
+
config = http_config(query_params, body)
|
56
|
+
begin
|
57
|
+
response = http_method.call(@base_url + relative_path, config)
|
58
|
+
rescue Errno::ECONNREFUSED => e
|
59
|
+
raise CommunicationError, e.message
|
60
|
+
end
|
61
|
+
validate(response)
|
62
|
+
end
|
63
|
+
|
64
|
+
def http_config(query_params, body)
|
65
|
+
body = body.to_json unless body.nil?
|
66
|
+
{
|
67
|
+
headers: @headers,
|
68
|
+
query: query_params,
|
69
|
+
body: body,
|
70
|
+
timeout: 1
|
71
|
+
}.compact
|
72
|
+
end
|
73
|
+
|
68
74
|
def validate(response)
|
69
|
-
raise
|
75
|
+
raise ApiError.new(response.code, response.message, response.body) unless response.success?
|
70
76
|
|
71
77
|
response.parsed_response
|
72
78
|
end
|
data/lib/meilisearch/index.rb
CHANGED
data/lib/meilisearch/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: meilisearch
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.12.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Meili
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2020-
|
11
|
+
date: 2020-07-09 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: httparty
|