Sha256: 3507c9622dbff3a5b113c26af068d096d16c6e50d9d312879fe266e3b5658683

Contents?: true

Size: 1.69 KB

Versions: 3

Compression:

Stored size: 1.69 KB

Contents

# frozen_string_literal: true

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

module Boilercode
  module Commands
    class Drive
      class Search < Boilercode::Command
        # attr_reader :query, :download_file
        def initialize(options)
          @options = options
          # @download_file = options[:download]
          # @query = options[:query]
        end

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

        private

        def handle_search_response(res)
          headers = res.headers
          response = parse_json(res)
          if response.empty?
            pastel.red "No uploads found, try updating your query."
          else
            choice = prompt.select("Choose your upload:", choices(response, headers))
            if choice == "Next Page"
              return execute(page: headers["Current-Page"].to_i + 1)
            end
            upload = response.find { |upload| upload["filename"] == choice }

            download(upload["url"]) if download_file?
            pastel.green(upload["url"])
          end
        end

        def choices(response, headers)
          choices = response.map { |upload| upload["filename"] }

          choices << "Next Page" if headers["Total-Pages"].to_i > 1
          choices
        end

        def download_file?
          ask("Do you want to download the file? (y/n)") == "y"
        end

        def query
          ask("Enter your search query:")
        end
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

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