Sha256: cfc67f57bb8fd07fea9fe3a40c126f5301fbc2b14a92777547fb81e9150ae35e

Contents?: true

Size: 1.75 KB

Versions: 1

Compression:

Stored size: 1.75 KB

Contents

require"byebug"
module Aocli
  class Cli
    def initialize
      @options = {cookie: Aocli::CookieRetriever.call}
      @prompt = TTY::Prompt.new
    end

    def run
      prompt_for_command
      get_all_command_options
      # Aocli::Commands::StartDay
      # Aocli::Commands::SaveCookie
      options[:command].constantize.run!(options)
    end

    private
    attr_accessor :options, :prompt
    attr_reader :cookie

    def prompt_for_command
      options[:command] = prompt.select("What would you like to do?", command_options)
    end

    def get_all_command_options
      case options[:command]
      when "Aocli::Commands::StartDay"
        set_start_day_options
      when "Aocli::Commands::SaveCookie"
        set_save_cookie_options
      when "Exit"
        exit
      else
        raise NotImplementedError, "Can't handle that command: #{command_klass}"
      end
    end

    def command_options
      @command_options ||= [
        {name: "Save Cookie", value: "Aocli::Commands::SaveCookie"},
        requires_cookie({name: "Start day", value: "Aocli::Commands::StartDay"}),
        {name: "Exit", value: "Exit"},
      ]
      # TODO:
      # requires_cookie({name: "Remove cookie file", value: "Aocli::Commands::RemoveCookieFile"}),
    end

    def requires_cookie(hash)
      hash.merge(options[:cookie] ? {} : {disabled: "(No cookie has been set yet)"})
    end

    def set_start_day_options
      year = prompt.select("Which Year?", Aocli::Values::START_DAY_YEARS)
      day = prompt.select("Which Day?", Aocli::Values::START_DAY_DAYS)
      options[:date] = Date.new(year, 12, day)
      options[:output_destination] = "./"
    end

    def set_save_cookie_options
      options[:cookie] = prompt.ask("Paste your cookie and it will be saved:")
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
aocli-1.3.0 lib/aocli/cli.rb