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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 53ba8f68eca0a4b76da0d0a967c2ee67757496072a13acb79d82de18f29c2fc4
4
- data.tar.gz: 0ec5a423d738a7274cf85362d87919994fe137aac50768e0fe6efc22d3d4a9f7
3
+ metadata.gz: 17248185a6d2ba10e2e5fac61b6d9d07a0cac218a2accc4cd5b3bd130517f837
4
+ data.tar.gz: dd20f35451242677ec05ac52d95cbc14915dea6676562ef23c58a58655db24ad
5
5
  SHA512:
6
- metadata.gz: 5ac3a7446b8b86e952337866228149ca7ab2767c56bcba623e86e53c25ae934bf39bd238640945799d2ec9a6ea4928fa672da9f69b7e3ecb9a3c12ebb603e485
7
- data.tar.gz: fc6d734865d2e23827143e15acf9738091753be549899c3e10230adff950fedba1881e1915b30ac8de78d910a243156fb19b2852f21d3a5e93480337af188f37
6
+ metadata.gz: f0c0c7a6726257c62e14108f33df551cd5ec3d7c5f4b6f8eb3fa975988da3915d7b3167e0074c3e21134f49d1a40d84a55e4145ae7b4ffac45cd1daa8954e00d
7
+ data.tar.gz: 3cb711d0577b92fe428b90ec16d2cbc7fb593aba63f014206c17a88bca2ba4de0fdd58b51e2b014640ae7118996142b9edc4c4d4d00250fc8a6893df117e3527
@@ -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
@@ -1,45 +1,58 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module MeiliSearch
4
- class MeiliSearchError < StandardError; end
5
- class IndexUidError < MeiliSearchError; end
6
- class MeiliSearchTimeoutError < MeiliSearchError
7
- attr_reader :message
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
- def initialize
10
- @message = "MeiliSearchTimeoutError: update wasn't processed in the expected time"
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 to_s
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 HTTPError < MeiliSearchError
19
- attr_reader :status
41
+ class CommunicationError < StandardError
20
42
  attr_reader :message
21
- attr_reader :http_body
22
- attr_reader :http_body_message
23
- attr_reader :details
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
- def to_s
41
- final_message = @details.nil? ? @message : "#{@message}. #{@details}"
42
- "#{@status}: #{final_message}."
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(path = '', query_params = {})
20
- response = self.class.get(
21
- @base_url + path,
22
- query: query_params,
23
- headers: @headers,
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(path = '', body = nil, query_params = nil)
30
- body = body.to_json unless body.nil?
31
- response = self.class.post(
32
- @base_url + path,
33
- {
34
- body: body,
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(path = '', body = nil, query_params = nil)
44
- body = body.to_json unless body.nil?
45
- response = self.class.put(
46
- @base_url + path,
47
- {
48
- body: body,
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(path = '')
58
- response = self.class.delete(
59
- @base_url + path,
60
- headers: @headers,
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 HTTPError.new(response.code, response.message, response.body) unless response.success?
75
+ raise ApiError.new(response.code, response.message, response.body) unless response.success?
70
76
 
71
77
  response.parsed_response
72
78
  end
@@ -113,7 +113,7 @@ module MeiliSearch
113
113
  end
114
114
  end
115
115
  rescue Timeout::Error
116
- raise MeiliSearch::MeiliSearchTimeoutError
116
+ raise MeiliSearch::TimeoutError
117
117
  end
118
118
 
119
119
  ### STATS
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module MeiliSearch
4
- VERSION = '0.11.1'
4
+ VERSION = '0.12.0'
5
5
  end
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.11.1
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-06-23 00:00:00.000000000 Z
11
+ date: 2020-07-09 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: httparty