Sha256: adc624a2d1cf2a90aa90a6a44a880d2e95163346467759b492672b38d79ccdab

Contents?: true

Size: 2 KB

Versions: 3

Compression:

Stored size: 2 KB

Contents

require 'faraday'
require 'json'

module SearchKit
  module Clients
    class Documents
      attr_reader :connection, :token

      def initialize
        uri = [SearchKit.config.app_uri, "documents"].join("/")
        @connection = Faraday.new(uri)
        @token      = SearchKit.config.app_token
      end

      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

      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

      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

      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

    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
search-kit-0.0.10 lib/search_kit/clients/documents.rb
search-kit-0.0.9 lib/search_kit/clients/documents.rb
search-kit-0.0.8 lib/search_kit/clients/documents.rb