class SearchKit::Clients::Documents

Attributes

connection[R]
token[R]

Public Class Methods

new() click to toggle source
# File lib/search_kit/clients/documents.rb, line 9
def initialize
  uri = [SearchKit.config.app_uri, "documents"].join("/")
  @connection = Faraday.new(uri)
  @token      = SearchKit.config.app_token
end

Public Instance Methods

create(slug, document) click to toggle source
# File lib/search_kit/clients/documents.rb, line 15
def create(slug, document)
  document = {
    token: token,
    data: { type: "documents", attributes: document }
  }

  response = connection.post(slug, document)
  body     = JSON.parse(response.body, symbolize_names: true)

  fail Errors::BadRequest    if response.status == 400
  fail Errors::Unauthorized  if response.status == 401
  fail Errors::IndexNotFound if response.status == 404
  fail Errors::Unprocessable if response.status == 422

  body
end
delete(slug, id) click to toggle source
# File lib/search_kit/clients/documents.rb, line 32
def delete(slug, id)
  response = connection.delete("#{slug}/#{id}", token: token)
  body     = JSON.parse(response.body, symbolize_names: true)

  fail Errors::Unauthorized  if response.status == 401
  fail Errors::IndexNotFound if response.status == 404

  body
end
show(slug, id) click to toggle source
# File lib/search_kit/clients/documents.rb, line 42
def show(slug, id)
  response = connection.get("#{slug}/#{id}", token: token)
  body     = JSON.parse(response.body, symbolize_names: true)

  fail Errors::Unauthorized  if response.status == 401
  fail Errors::IndexNotFound if response.status == 404

  body
end
update(slug, id, document) click to toggle source
# File lib/search_kit/clients/documents.rb, line 52
def update(slug, id, document)
  document = {
    token: token,
    data: { type: "documents", id: id, attributes: document }
  }

  response = connection.patch("#{slug}/#{id}", document)
  body     = JSON.parse(response.body, symbolize_names: true)

  fail Errors::BadRequest    if response.status == 400
  fail Errors::Unauthorized  if response.status == 401
  fail Errors::IndexNotFound if response.status == 404
  fail Errors::Unprocessable if response.status == 422

  body
end