Sha256: bfc383f964ad12da824e34817995d059def0be0eab8ec47de5d0d53fd223ba04

Contents?: true

Size: 1.24 KB

Versions: 3

Compression:

Stored size: 1.24 KB

Contents

# frozen_string_literal: true

require_relative "../../../boilercode"

module Boilercode
  module Commands
    class Bookmarks
      class Search < Boilercode::Command
        def initialize(options)
          @options = options
        end

        def execute(input: $stdin, output: $stdout)
          res = client.get("/bookmarks?q[title_or_url_cont]=#{query}")
          if res.success?
            output.puts handle_search_response(parse_json(res))
          else
            output.puts pastel.red res.inspect
          end
        end

        private

        def handle_search_response(response)
          if response.empty?
            pastel.red "No bookmarks found, try updating your query."
          else
            choice = prompt.select("Choose your bookmark:", response.map { |bookmark| bookmark["title"] })
            bookmark = response.find do |bm|
              bm.title == choice
            end
            puts pastel.green(link_to(bookmark["title"], bookmark["url"]))
            Clipboard.copy(bookmark["short_url"])
            open_in_browser(bookmark["url"])
            pastel.green("Bookmark copied to clipboard.")
          end
        end

        def query
          ask("Search for a bookmark:")
        end
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
boilercode-0.1.2 lib/boilercode/commands/bookmarks/search.rb
boilercode-0.1.1 lib/boilercode/commands/bookmarks/search.rb
boilercode-0.1.0 lib/boilercode/commands/bookmarks/search.rb