Sha256: e2966d649334b27209276ce6365cf219b9793c1a50d72b934427ee8e67a1fdc5

Contents?: true

Size: 1.62 KB

Versions: 2

Compression:

Stored size: 1.62 KB

Contents

require 'thor'
require 'search_kit/thor'

module SearchKit
  class CLI < Thor
    class Search < Thor
      namespace :search

      no_commands do
        def client
          @client ||= Search.new
        end

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

      document :create
      option :display, aliases: ['-d'], type: :array, required: false
      def create(slug, phrase)
        search    = client.search(slug, phrase: phrase)
        head_path = 'cli.search.create.success.headline'
        info_path = 'cli.search.create.success.info'
        headline  = I18n.t(head_path, slug: slug, phrase: phrase)
        info      = I18n.t(info_path, count: search.results, time: search.time)
        lines     = [ headline, info ]
        display   = options.fetch('display', [])

        lines += search.documents.map do |document|
          if display.any?
            fields = display.map { |field| document.get(field) }
            " -- #{fields.join(' | ')} | score: #{document.score}"
          else
            " -- #{document.id} | score: #{document.score}"
          end
        end

        lines.each(&messages.method(:info))
      rescue Errors::Unauthorized
        messages.unauthorized
      rescue Errors::IndexNotFound
        messages.not_found
      rescue Errors::BadRequest
        messages.bad_request
      rescue Errors::Unprocessable
        messages.unprocessable
      rescue Faraday::ConnectionFailed
        messages.no_service
      end

      no_commands do
        alias_method :search, :create
        alias_method :c, :create
        alias_method :s, :search
      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/search.rb
search-kit-0.0.3 lib/search_kit/cli/search.rb