Sha256: 15ccf7cd9d231e980a7a43d3928972e87b2ae56e1caea5793d0d84e5b22a3679

Contents?: true

Size: 1.93 KB

Versions: 2

Compression:

Stored size: 1.93 KB

Contents

require 'thor'
require 'search_kit/thor'

module SearchKit
  class CLI < Thor
    class Documents < Thor
      namespace :documents

      no_commands do
        def client
          @client ||= SearchKit::Clients::Documents.new
        end

        def messages
          @messages ||= SearchKit::Messages.new
        end
      end

      document :create
      def create(slug, document)
        document = JSON.parse(document, symbolize_names: true)
        response = client.create(slug, document)
        messages.info response.to_json
      rescue Errors::IndexNotFound
        messages.not_found
      rescue Errors::BadRequest
        messages.bad_request
      rescue Errors::Unprocessable
        messages.unprocessable
      rescue Faraday::ConnectionFailed
        messages.no_service
      rescue JSON::ParserError
        messages.json_parse_error
      end

      document :delete
      def delete(slug, id)
        response = client.delete(slug, id)
        messages.info response.to_json
      rescue Errors::IndexNotFound
        messages.not_found
      rescue Faraday::ConnectionFailed
        messages.no_service
      end

      document :show
      def show(slug, id)
        response = client.show(slug, id)
        messages.info response.to_json
      rescue Errors::IndexNotFound
        messages.not_found
      rescue Faraday::ConnectionFailed
        messages.no_service
      end

      document :update
      def update(slug, id, document)
        document = JSON.parse(document, symbolize_names: true)
        response = client.update(slug, id, document)
        messages.info response.to_json
      rescue JSON::ParserError
        messages.json_parse_error
      rescue Errors::BadRequest
        messages.bad_request
      rescue Errors::IndexNotFound
        messages.not_found
      rescue Errors::Unprocessable
        messages.unprocessable
      rescue Faraday::ConnectionFailed
        messages.no_service
      end

    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
search-kit-0.0.4 lib/search_kit/cli/documents.rb
search-kit-0.0.3 lib/search_kit/cli/documents.rb