Class: CodingChallenge::Commands::Start

Inherits:
CodingChallenge::Command show all
Includes:
Animation
Defined in:
lib/coding_challenge/commands/start.rb

Constant Summary collapse

@@BASE_NAVIGATION_STATES =
%w[HOME_MENU SET_INVENTORY_FILE INIT_QUERY VIEW_QUERIES EXITED]
@@DEFAULT_PRODUCTS_LIST_URL =
'https://gist.githubusercontent.com/michaelporter/b2743e0cdad0664fa9517c0a6b82cdda/raw/67e4606007391f678c9330ee3a77a9024fce4e64/products.json'

Instance Method Summary collapse

Methods included from Animation

#intro_loading_bars_animation, #intro_title_animation, #loading_animation, #type_effect

Methods inherited from CodingChallenge::Command

#command, #cursor, #editor, #exec_exist?, #generator, #pager, #platform, #prompt, #screen, #which

Constructor Details

#initialize(options) ⇒ Start

Returns a new instance of Start.



22
23
24
25
26
27
28
# File 'lib/coding_challenge/commands/start.rb', line 22

def initialize(options)
  @options = options
  @inventory = nil
  @base_navigation_state_index = 0
  @prev_base_navigation_state_index = nil
  @queries = []
end

Instance Method Details

#execute(input: $stdin, output: $stdout) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/coding_challenge/commands/start.rb', line 30

def execute(input: $stdin, output: $stdout)
  cli_args = ARGV.slice(1, ARGV.length).reject { |arg| arg.slice(0, 2) == '--' }

  perform_intro_animation if @options['skip_intro_animation'].nil?

  if cli_args.empty?
    loading_animation('Loading menu...', 1)
  else
    loading_animation('Calculating results...', 2)

    new_inventory = Inventory.new

    begin
      new_inventory.load_products_list_from_default
      @inventory = new_inventory
      new_query = Query.new(cli_args)
      query_with_results = @inventory.handle_query(new_query)

      puts 'Results:'.colorize(:yellow)
      puts query_with_results.results

      @queries.push(query_with_results.formatted_results)
    rescue StandardError => e
      puts "#{e.class.name}: #{e.message}".colorize(:red)
    end
  end

  exited = false
  until exited
    base_navigation_state = @@BASE_NAVIGATION_STATES[@base_navigation_state_index]

    if base_navigation_state == 'HOME_MENU'
      new_base_navigation_state_index = execute_home_menu
    elsif base_navigation_state == 'SET_INVENTORY_FILE'
      new_base_navigation_state_index = execute_set_inventory_file
    elsif base_navigation_state == 'INIT_QUERY'
      new_base_navigation_state_index = execute_init_query
    elsif base_navigation_state == 'VIEW_QUERIES'
      new_base_navigation_state_index = execute_view_queries
    elsif base_navigation_state == 'EXITED'
      exited = true
    end

    if new_base_navigation_state_index == 'BACK'
      @base_navigation_state_index = @prev_base_navigation_state_index
    elsif @base_navigation_state_index != @prev_base_navigation_state
      @prev_base_navigation_state_index = @base_navigation_state_index
      @base_navigation_state_index = new_base_navigation_state_index
    end
  end

  exit(0)
end