Sha256: ccc3ffbb5408a9ec6c11a8cb18375599eeeeb4adef72273839cd40ed640206a3

Contents?: true

Size: 1.55 KB

Versions: 1

Compression:

Stored size: 1.55 KB

Contents

require 'faraday'
require 'json'
require 'uri'

module SearchKit
  class Documents
    autoload :CLI, 'search_kit/documents/cli'

    attr_reader :connection

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

    def create(slug, options)
      document = { data: { type: "documents", attributes: options } }
      response = connection.post(slug, document)
      body     = JSON.parse(response.body, symbolize_names: true)

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

      body
    end

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

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

      body
    end

    def update(slug, id, options)
      document = { data: { type: "documents", id: id, attributes: options } }
      response = connection.patch("#{slug}/#{id}", document)
      body     = JSON.parse(response.body, symbolize_names: true)

      fail Errors::BadRequest    if response.status == 400
      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}")
      body     = JSON.parse(response.body, symbolize_names: true)

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

      body
    end

  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
search-kit-0.0.2 lib/search_kit/documents.rb