# frozen_string_literal: true module GetYourRep # Command Line Interface module CLI # :nodoc: class << self include GetYourRep attr_reader :line, :address, :delegation def call Patriotic.stars_and_bars Patriotic.welcome @line = 0 start end private def start puts '', "Type 'help' for help".blue loop do command_line input = gets.strip.downcase case input when 'help' help when 'exit' break when 'set address' set_address when 'address' find_or_set_address when 'all reps' all_reps when 'google all reps' google_all_reps when 'google congress only' google_congress_only when 'open states' open_states else command_not_found_error end end end def command_line new_line print "[#{line}] gyr(#{VERSION}) -> " end def find_or_set_address puts address || set_address end def new_line @line += 1 end def help puts "'help'...................list of commands".bold.blue puts "'address'................displays address or sets it if there is none".bold.blue puts "'set address'............set a new address".bold.blue puts "'all reps'...............get federal legislators from Google".bold.blue puts ' and state legislators from Open States'.bold.blue puts "'google all reps'........get all federal and state executives and".bold.blue puts ' legislators from Google'.bold.blue puts "'google congress only'...get only congress from Google".bold.blue puts "'open states'............get only state legislators from Open States".bold.blue puts "'exit'...................exit".bold.blue end def set_address puts "\nThe most accurate results will always be achieved with a full address, \n\ but you can try a zip code if you insist.".yellow puts 'Hint: '.bold.blue + 'GYR will geocode your zip to an address anyway.'.yellow print "\nPlease enter your address -> " self.address = gets.strip end def address=(value) @address = address_is_a_zip?(value) ? convert_zip_to_address(value) : value end def all_reps address || set_address super(address) display_reps end def google_all_reps address || set_address @delegation = Google.all_reps(address) display_reps end def google_congress_only address || set_address @delegation = Google.all_reps(address, congress_only: true) display_reps end def open_states address || set_address @delegation = OpenStates.all_reps(address) display_reps end def display_reps rep_list puts 'Select a rep number for more detail. '\ "Type 'list' for the list or 'exit' for a new query.".yellow rep_loop end def rep_list delegation.each_with_index do |rep, index| puts "#{index + 1}. #{rep.name}".bold.blue end end def rep_loop loop do command_line input = gets.strip.downcase num = input.to_i if input == 'exit' puts "Enter a new query. Type 'help' for help.".yellow break elsif input == 'list' rep_list elsif (1..delegation.count).cover?(num) delegation.each_with_index do |rep, index| next if index + 1 != num rep.cli_display end else puts "Submit a valid entry or type 'exit'.".bold.red end end end end end end