meilisearch 0.11.0 → 0.13.3

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: 9a36df5bca77a8d8487a6cfdf12ef055db52aec77d25d0a522e4fecb455d8714
4
- data.tar.gz: e6a5b012fa2d4099be7ce43e5790ee032d612d68ce7fbcf697a6928458a23e51
3
+ metadata.gz: 2540e65f9756b00265d39045cc557a7a70b95379efb9e853a19e2d310b08b641
4
+ data.tar.gz: b6b2ced9220eab1b917fd4711343e10b50755de3b51e2eb6eb53292a105f0105
5
5
  SHA512:
6
- metadata.gz: 37cba34617de2798ff77d3f5bd711862a84a14cbea9dca44349897dacff37f004184e6b71fad314ddefc3ce6a1cbcb124c5c9ed85781d7ee2028385bc20d3418
7
- data.tar.gz: ce410e17029be8a779f10921aef8b28200dd3ae2fc1cb3924bbb5085596a1adc70c2a5e4aed5492751dee3d6f1ab4dbbdc7186673fe1041d2b4875819ab3ab65
6
+ metadata.gz: 5827e1e243154d2b431c3a4f678d7db404140f6d19d5bf07a4f402b874de90918ce84fea0793a178527f158931a8de3f1a24f517d391f1aaedf14d09ef272a14
7
+ data.tar.gz: d084c29d0094e52ab63036439cfaa1415d38fe3148a9591f2ba801f89aa343704bb7e3253560cea0b69172ddf5f267d6b6ad9cff4d1f8f77521dae6d72f5e7af
@@ -18,11 +18,20 @@ module MeiliSearch
18
18
  # client.create_index('indexUID')
19
19
  # client.create_index('indexUID', primaryKey: 'id')
20
20
  def create_index(index_uid, options = {})
21
- body = { uid: index_uid }.merge(options)
21
+ body = options.merge(uid: index_uid)
22
22
  res = http_post '/indexes', body
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
@@ -66,14 +73,6 @@ module MeiliSearch
66
73
  http_get '/version'
67
74
  end
68
75
 
69
- def sysinfo
70
- http_get '/sys-info'
71
- end
72
-
73
- def pretty_sysinfo
74
- http_get '/sys-info/pretty'
75
- end
76
-
77
76
  def stats
78
77
  http_get '/stats'
79
78
  end
@@ -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}. 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
@@ -7,66 +7,74 @@ module MeiliSearch
7
7
  class HTTPRequest
8
8
  include HTTParty
9
9
 
10
- def initialize(url, api_key = nil)
10
+ def initialize(url, api_key = nil, options = {})
11
11
  @base_url = url
12
12
  @api_key = api_key
13
+ @options = options
13
14
  @headers = {
14
15
  'Content-Type' => 'application/json',
15
16
  'X-Meili-API-Key' => api_key
16
17
  }.compact
17
18
  end
18
19
 
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
20
+ def http_get(relative_path = '', query_params = {})
21
+ send_request(
22
+ proc { |path, config| self.class.get(path, config) },
23
+ relative_path,
24
+ query_params
25
25
  )
26
- validate(response)
27
26
  end
28
27
 
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
28
+ def http_post(relative_path = '', body = nil, query_params = nil)
29
+ send_request(
30
+ proc { |path, config| self.class.post(path, config) },
31
+ relative_path,
32
+ query_params,
33
+ body
39
34
  )
40
- validate(response)
41
35
  end
42
36
 
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
37
+ def http_put(relative_path = '', body = nil, query_params = nil)
38
+ send_request(
39
+ proc { |path, config| self.class.put(path, config) },
40
+ relative_path,
41
+ query_params,
42
+ body
53
43
  )
54
- validate(response)
55
44
  end
56
45
 
57
- def http_delete(path = '')
58
- response = self.class.delete(
59
- @base_url + path,
60
- headers: @headers,
61
- timeout: 1
46
+ def http_delete(relative_path = '')
47
+ send_request(
48
+ proc { |path, config| self.class.delete(path, config) },
49
+ relative_path
62
50
  )
63
- validate(response)
64
51
  end
65
52
 
66
53
  private
67
54
 
55
+ def send_request(http_method, relative_path, query_params = nil, body = nil)
56
+ config = http_config(query_params, body)
57
+ begin
58
+ response = http_method.call(@base_url + relative_path, config)
59
+ rescue Errno::ECONNREFUSED => e
60
+ raise CommunicationError, e.message
61
+ end
62
+ validate(response)
63
+ end
64
+
65
+ def http_config(query_params, body)
66
+ body = body.to_json unless body.nil?
67
+ {
68
+ headers: @headers,
69
+ query: query_params,
70
+ body: body,
71
+ timeout: @options[:timeout] || 1,
72
+ max_retries: @options[:max_retries] || 0
73
+ }.compact
74
+ end
75
+
68
76
  def validate(response)
69
- raise HTTPError.new(response.code, response.message, response.body) unless response.success?
77
+ raise ApiError.new(response.code, response.message, response.body) unless response.success?
70
78
 
71
79
  response.parsed_response
72
80
  end
@@ -81,16 +81,8 @@ module MeiliSearch
81
81
  ### SEARCH
82
82
 
83
83
  def search(query, options = {})
84
- parsed_options = options.transform_keys(&:to_sym).map do |k, v|
85
- if [:facetFilters, :facetsDistribution].include?(k)
86
- [k, v.inspect]
87
- elsif v.is_a?(Array)
88
- [k, v.join(',')]
89
- else
90
- [k, v]
91
- end
92
- end.to_h
93
- http_get "/indexes/#{@uid}/search", { q: query }.merge(parsed_options)
84
+ parsed_options = options.compact
85
+ http_post "/indexes/#{@uid}/search", { q: query }.merge(parsed_options)
94
86
  end
95
87
 
96
88
  ### UPDATES
@@ -113,7 +105,7 @@ module MeiliSearch
113
105
  end
114
106
  end
115
107
  rescue Timeout::Error
116
- raise MeiliSearch::MeiliSearchTimeoutError
108
+ raise MeiliSearch::TimeoutError
117
109
  end
118
110
 
119
111
  ### STATS
@@ -244,17 +236,6 @@ module MeiliSearch
244
236
  http_delete "/indexes/#{@uid}/settings/displayed-attributes"
245
237
  end
246
238
 
247
- ### SETTINGS - ACCEPT NEW FIELS VALUE
248
-
249
- def accept_new_fields
250
- http_get "/indexes/#{@uid}/settings/accept-new-fields"
251
- end
252
- alias get_accept_new_fields accept_new_fields
253
-
254
- def update_accept_new_fields(accept_new_fields)
255
- http_post "/indexes/#{@uid}/settings/accept-new-fields", accept_new_fields
256
- end
257
-
258
239
  ### SETTINGS - ATTRIBUTES FOR FACETING
259
240
 
260
241
  def attributes_for_faceting
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module MeiliSearch
4
- VERSION = '0.11.0'
4
+ VERSION = '0.13.3'
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.0
4
+ version: 0.13.3
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-15 00:00:00.000000000 Z
11
+ date: 2020-09-24 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: httparty