# 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 new_line print "[#{line}] gyr(#{VERSION}) -> " 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 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 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 delegation.reps.each do |rep| @banner ||= Patriotic.banner rep.cli_display end end end end end