Module: MeiliSearch::Utils

Defined in:
lib/meilisearch/utils.rb

Constant Summary collapse

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

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.loggerObject



12
13
14
# File 'lib/meilisearch/utils.rb', line 12

def logger
  @logger ||= Logger.new($stdout)
end

Class Method Details

.filter(original_options, allowed_params = []) ⇒ Object



44
45
46
# File 'lib/meilisearch/utils.rb', line 44

def filter(original_options, allowed_params = [])
  original_options.transform_keys(&:to_sym).slice(*allowed_params)
end

.parse_query(original_options, allowed_params = []) ⇒ Object



48
49
50
51
52
53
54
55
56
# File 'lib/meilisearch/utils.rb', line 48

def parse_query(original_options, allowed_params = [])
  only_allowed_params = filter(original_options, allowed_params)

  Utils.transform_attributes(only_allowed_params).then do |body|
    body.transform_values do |v|
      v.respond_to?(:join) ? v.join(',') : v.to_s
    end
  end
end

.soft_deprecate(subject, replacement) ⇒ Object



16
17
18
# File 'lib/meilisearch/utils.rb', line 16

def soft_deprecate(subject, replacement)
  logger.warn("[meilisearch-ruby] #{subject} is DEPRECATED, please use #{replacement} instead.")
end

.transform_attributes(body) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
# File 'lib/meilisearch/utils.rb', line 32

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

.version_error_handler(method_name) ⇒ Object



58
59
60
61
62
63
64
65
66
# File 'lib/meilisearch/utils.rb', line 58

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

  raise MeiliSearch::ApiError.new(e.http_code, message, e.http_body)
rescue StandardError => e
  raise e.class, message_builder(e.message, method_name)
end

.warn_on_non_conforming_attribute_names(body) ⇒ Object



68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/meilisearch/utils.rb', line 68

def warn_on_non_conforming_attribute_names(body)
  return if body.nil?

  non_snake_case = body.keys.grep_v(/^[a-z0-9_]+$/)
  return if non_snake_case.empty?

  message = <<~MSG
    [meilisearch-ruby] Attributes will be expected to be snake_case in future versions.
    [meilisearch-ruby] Non-conforming attributes: #{non_snake_case.join(', ')}
  MSG

  logger.warn(message)
end

.warn_on_unfinished_task(task_uid) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
# File 'lib/meilisearch/utils.rb', line 20

def warn_on_unfinished_task(task_uid)
  message = <<~UNFINISHED_TASK_WARNING
    [meilisearch-ruby] Task #{task_uid}'s finished state (succeeded?/failed?/cancelled?) is being checked before finishing.
    [meilisearch-ruby] Tasks in meilisearch are processed in the background asynchronously.
    [meilisearch-ruby] Please use the #finished? method to check if the task is finished or the #await method to wait for the task to finish.
  UNFINISHED_TASK_WARNING

  message.lines.each do |line|
    logger.warn(line)
  end
end