lib/congress.rb in my-congress-0.2.2 vs lib/congress.rb in my-congress-0.2.3

- old
+ new

@@ -1,137 +1,43 @@ -#require "my/congress/version" -# require 'require_all' -# require_all "lib" -# require 'nokogiri' -# require 'open-uri' -# require 'pry' -# require 'selenium-webdriver' +class MyCongress - -class MCongress - - def call Header.new HeaderQuote.new - puts "Welcome to My Congress. Please make a selection from the menu: ".colorize(:red) + puts "Welcome to My Congress!" puts " " - puts "Enter '1' to find U.S. Senators and the Congressional Representative for your location.".colorize(:blue) - puts "Enter '2' to see upcoming elections for your location.".colorize(:blue) - puts "Type 'exit' to leave My Congress.".colorize(:blue) - choice = gets.chomp - if choice != "1" && choice != "2" && choice.upcase != "EXIT" - puts "Invalid input. Press 'Enter to return to Main Menu.".colorize(:blue) - nothing = gets.chomp - MCongress.new.call - elsif choice.upcase == "EXIT" + prompt = TTY::Prompt.new(active_color: :red) + choice = prompt.select("Please make a selection from the menu:".colorize(:blue)) do |menu| + menu.choice 'Find U.S. Senators and the Congressional Representative for my location', "1" + menu.choice 'See upcoming elections in my area', "2" + menu.choice 'See current headlines in U.S. Politcs', "3" + menu.choice 'See list of upcoming bills for House and Senate', "4" + menu.choice 'Exit My Congress', "EXIT" + end + + if choice == "EXIT" ProgramEnd.new + elsif choice == "3" + Headlines.new + elsif choice == "4" + Bills.new + end + + address = prompt.collect do + key(:city).ask("Enter the name of your city: ") + key(:state).ask("Enter the name of your state: ") + key(:zip).ask("Enter your zip code :") + key(:street).ask("Enter your street number and street name :") end - puts "Enter the name of your city: ".colorize(:blue) - @city_input = gets.chomp - puts "Enter the name of your state: ".colorize(:blue) - @state_input = gets.chomp - StateCheck.new(@state_input) - puts "Enter your 5 digit zip code: ".colorize(:blue) - @@zip_input = gets.chomp - ZipCheck.new(@@zip_input) - puts "Enter your street number and name: ".colorize(:blue) - @@street_input = gets.chomp + + StateCheck.new(address) + ZipCheck.new(address) + if choice == "1" puts " " puts "Please wait while we locate your congressional district...".colorize(:red) - GetURL.new(@@street_input, @@zip_input, @@state_input) + GetURL.new(address) else - UpcomingElections.new(@@state_input) + UpcomingElections.new(address) end end - - #Sends user to Senators/Representative method from Upcoming Elections method - def self.send_to_congress - puts " " - puts "Please wait while we locate your congressional district...".colorize(:red) - GetURL.new(@@street_input, @@zip_input, @@state_input) - end - - def self.state_input - @@state_input - end - - def self.state_input=(input) - @@state_input = input - end - - def self.get_url(street, zip) - address_array = street.scan(/\w+/) - address_string = "" - x = 0 - - while x < address_array.length - 1 - address_string += address_array[x] + "%20" - x += 1 - end - - address_string += address_array[address_array.length-1] - - site = "https://secure.everyaction.com/p/VRl-GNlm_0mCSWaMc4d6_w2?pc=#{zip}&add1=#{address_string}&results=True" - MCongress.selenium(site) - - end - - def self.selenium(site) - options = Selenium::WebDriver::Chrome::Options.new(args: ['headless']) - driver = Selenium::WebDriver.for(:chrome, options: options) - driver.get(site) - - - info = driver.find_elements(css: 'div.row.oa-elected-official-group-list-item') - - #Error for not locating address / invalid address - if info[3] == nil - puts " " - puts "Could not locate address. Press 'Enter' to return to Main Menu.".colorize(:blue) - nothing = gets.chomp - MCongress.new.call - end - elements = Nokogiri::HTML(open(driver.current_url)) - - sen1 = info[3].text.split("\n") - sen2 = info[4].text.split("\n") - rep = info[5].text.split("\n") - - driver.quit - - MCongress.find_representatives(sen1, sen2, rep, elements) - - end - - - def self.find_representatives(sen1, sen2, rep, elements) - @district = rep[2].scan(/\d/)[0] - senator1 = {} - senator2 = {} - representative = {} - congress_members = [senator1, senator2, representative] - info_array = [sen1, sen2, rep] - - x = 0 - congress_members.each do |member| - member[:name] = info_array[x][0].strip - member[:party] = info_array[x][1].scan(/.[RD]\W/)[0] - if member[:party] == "(R)" - member[:party] = "Republican" - else - member[:party] = "Democrat" - end - member[:phone] = info_array[x][3].strip - member[:twitter] = elements.css("div.oa-eo-target-col-links")[x+3].children[1].attributes["href"].value - member[:facebook] = elements.css("div.oa-eo-target-col-links")[x+3].children[3].attributes["href"].value - member[:website] = elements.css("div.oa-eo-target-col-links")[x+3].children[5].attributes["href"].value - x += 1 - end - - CongressOutput.new(senator1, senator2, representative, @district, @@state_input) - - - end - end \ No newline at end of file