class MeiliSearch::HTTPRequest

Attributes

options[R]

Public Class Methods

new(url, api_key = nil, options = {}) click to toggle source
# File lib/meilisearch/http_request.rb, line 12
def initialize(url, api_key = nil, options = {})
  @base_url = url
  @api_key = api_key
  @options = options
  @headers = {
    'Content-Type' => 'application/json',
    'X-Meili-API-Key' => api_key
  }.compact
end

Public Instance Methods

http_delete(relative_path = '') click to toggle source
# File lib/meilisearch/http_request.rb, line 48
def http_delete(relative_path = '')
  send_request(
    proc { |path, config| self.class.delete(path, config) },
    relative_path
  )
end
http_get(relative_path = '', query_params = {}) click to toggle source
# File lib/meilisearch/http_request.rb, line 22
def http_get(relative_path = '', query_params = {})
  send_request(
    proc { |path, config| self.class.get(path, config) },
    relative_path,
    query_params
  )
end
http_post(relative_path = '', body = nil, query_params = nil) click to toggle source
# File lib/meilisearch/http_request.rb, line 30
def http_post(relative_path = '', body = nil, query_params = nil)
  send_request(
    proc { |path, config| self.class.post(path, config) },
    relative_path,
    query_params,
    body
  )
end
http_put(relative_path = '', body = nil, query_params = nil) click to toggle source
# File lib/meilisearch/http_request.rb, line 39
def http_put(relative_path = '', body = nil, query_params = nil)
  send_request(
    proc { |path, config| self.class.put(path, config) },
    relative_path,
    query_params,
    body
  )
end

Private Instance Methods

http_config(query_params, body) click to toggle source
# File lib/meilisearch/http_request.rb, line 67
def http_config(query_params, body)
  body = body.to_json
  {
    headers: @headers,
    query: query_params,
    body: body,
    timeout: @options[:timeout] || 1,
    max_retries: @options[:max_retries] || 0
  }.compact
end
send_request(http_method, relative_path, query_params = nil, body = nil) click to toggle source
# File lib/meilisearch/http_request.rb, line 57
def send_request(http_method, relative_path, query_params = nil, body = nil)
  config = http_config(query_params, body)
  begin
    response = http_method.call(@base_url + relative_path, config)
  rescue Errno::ECONNREFUSED => e
    raise CommunicationError, e.message
  end
  validate(response)
end
validate(response) click to toggle source
# File lib/meilisearch/http_request.rb, line 78
def validate(response)
  raise ApiError.new(response.code, response.message, response.body) unless response.success?

  response.parsed_response
end