Sha256: 741b3b0761e445cbc6129c2a68b35bdee4cce0509f271bc75c47764ef6cc3087

Contents?: true

Size: 1.87 KB

Versions: 8

Compression:

Stored size: 1.87 KB

Contents

require 'faraday'
require 'json'

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

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

      def archive(slug)
        response = connection.delete(slug, 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 create(name)
        options = {
          token: token,
          data: { type: 'indices', attributes: { name: name } }
        }

        response = connection.post('', options)
        body     = JSON.parse(response.body, symbolize_names: true)

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

        body
      end

      def show(slug)
        response = connection.get(slug, 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, options)
        options  = {
          token: token,
          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::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

8 entries across 8 versions & 1 rubygems

Version Path
search-kit-0.0.10 lib/search_kit/clients/indices.rb
search-kit-0.0.9 lib/search_kit/clients/indices.rb
search-kit-0.0.8 lib/search_kit/clients/indices.rb
search-kit-0.0.7 lib/search_kit/clients/indices.rb
search-kit-0.0.6 lib/search_kit/clients/indices.rb
search-kit-0.0.5 lib/search_kit/clients/indices.rb
search-kit-0.0.4 lib/search_kit/clients/indices.rb
search-kit-0.0.3 lib/search_kit/clients/indices.rb