Sha256: 134c8e0f12c5a85c19324a8cce48d88b762456e9bbdb1da3948d32c9ca9d6008

Contents?: true

Size: 1.43 KB

Versions: 1

Compression:

Stored size: 1.43 KB

Contents

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

module SearchKit
  class Indices
    autoload :CLI, 'search_kit/indices/cli'

    attr_reader :connection

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

    def show(slug)
      response = connection.get(slug)
      body     = JSON.parse(response.body, symbolize_names: true)

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

      body
    end

    def create(name)
      options  = { data: { type: 'indices', attributes: { name: name } } }
      response = connection.post('/', options)
      body     = JSON.parse(response.body, symbolize_names: true)

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

      body
    end

    def update(slug, options)
      options  = { data: { type: 'indices', attributes: options } }
      response = connection.patch(slug, options)
      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)
      response = connection.delete(slug)
      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/indices.rb