Class: Applephile::CLI
- Inherits:
-
Object
- Object
- Applephile::CLI
- Defined in:
- lib/applephile/command_line_interface.rb
Constant Summary collapse
- INTRO_MESSAGE =
"WELCOME TO CRAIGSLIST SCRAPPER!"
Instance Method Summary collapse
- #convert_to_city(state, city_number) ⇒ Object
- #convert_to_state(state_number) ⇒ Object
- #create_items_from_array(phones_array) ⇒ Object
- #display_apple_products_info(apple_products) ⇒ Object
- #display_cities(state) ⇒ Object
- #display_main_menu ⇒ Object
- #display_states ⇒ Object
- #get_choice ⇒ Object
- #get_menu_input ⇒ Object
- #grab_apple_prodcuts ⇒ Object
- #run ⇒ Object
- #scrape_apple_prodcuts ⇒ Object
Instance Method Details
#convert_to_city(state, city_number) ⇒ Object
68 69 70 71 |
# File 'lib/applephile/command_line_interface.rb', line 68 def convert_to_city(state, city_number) #it takes in a number and returns the corresponding city's name @state_cities[city_number.to_i - 1] end |
#convert_to_state(state_number) ⇒ Object
63 64 65 66 |
# File 'lib/applephile/command_line_interface.rb', line 63 def convert_to_state(state_number) #it takes in a number and returns the corresponding state's name @scrape.get_states_names[state_number.to_i - 1] end |
#create_items_from_array(phones_array) ⇒ Object
83 84 85 86 87 88 89 90 91 92 93 94 95 |
# File 'lib/applephile/command_line_interface.rb', line 83 def create_items_from_array(phones_array) if phones_array.empty? puts "Your search return zero hits, please scrape again" create_items_from_array(scrape_apple_products()) else @city = Applephile::City.new({:name => @city_scraped, :state => @state_scraped, :city_url => @scraped_city_url}) phones_array.each do |phone_info| @city.add_item(Applephile::Item.new(phone_info)) end end end |
#display_apple_products_info(apple_products) ⇒ Object
97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 |
# File 'lib/applephile/command_line_interface.rb', line 97 def display_apple_products_info(apple_products) separator = "-+-+--+-+--+-+--+-+--+-+--+-+--+-+--+-+--+-+--+-+--+-+--+-+--+-+--+-+--+-+--+-+--+-+--+-+-+".colorize(:yellow) puts "The following phones were found in accordance to your search parameters." choice = "" while choice != "exit" apple_products.each_with_index do |product, index| puts "#{index + 1}: #{product.description}" puts "Price: $#{product.price}" puts "Phone's url: #{product.url}" puts "Phone's City: #{product.city.name}" puts separator if (index + 1) % 5 == 0 # puts "Enter number to see link phone on browser.".colorize(:cyan) # puts "next for more phones".colorize(:green) # puts "Or enter exit to re-scrape.".colorize(:blue) choice = get_choice if choice == "next" next elsif choice == 'exit' break else #make sure screen stays on all present create_items_from_array phone_url = apple_products[choice.to_i - 1].url system("open", phone_url) choice = get_choice end end end puts "There are no moh phones to show." end end |
#display_cities(state) ⇒ Object
50 51 52 53 54 55 56 57 58 59 60 61 |
# File 'lib/applephile/command_line_interface.rb', line 50 def display_cities(state) @state_cities = @scrape.get_state_cities(state) #find max length of city and use it left justify everything else for screen output max_length = @state_cities.map(&:length).max @state_cities.each_with_index do |city, index| print "#{index + 1}. #{city.capitalize.ljust(max_length)} " if (index + 1) % 3 == 0 print "\n" end end print "\n" end |
#display_main_menu ⇒ Object
20 21 22 23 24 25 26 27 |
# File 'lib/applephile/command_line_interface.rb', line 20 def stars = "************************".colorize(:yellow) puts "#{stars} MAIN MENU #{stars}" puts "Enter \"scrape\" to scrape.".colorize(:blue) puts "Enter \"exit\" to end program.".colorize(:yellow) puts "What would you like to do?".colorize(:blue) gets.chomp end |
#display_states ⇒ Object
40 41 42 43 44 45 46 47 48 |
# File 'lib/applephile/command_line_interface.rb', line 40 def display_states @scrape.get_states_names.each_with_index do |state, index| print "#{index + 1}. #{state} ".ljust(28) if (index + 1) % 5 == 0 print "\n" end end print "\n" end |
#get_choice ⇒ Object
131 132 133 134 135 136 |
# File 'lib/applephile/command_line_interface.rb', line 131 def get_choice puts "Enter number to see link phone on browser.".colorize(:cyan) puts "next for more phones".colorize(:green) puts "Or enter exit to re-scrape.".colorize(:blue) gets.chomp end |
#get_menu_input ⇒ Object
29 30 31 32 33 34 35 36 37 38 |
# File 'lib/applephile/command_line_interface.rb', line 29 def display_states puts "Please enter a number for the state you'd like to scrape.".colorize(:green) #convert number input from user @state_scraped = convert_to_state(gets.chomp) display_cities(@state_scraped) puts "Please enter a number for the city you'd like to scrape.".colorize(:green) @city_scraped = convert_to_city(@state_scraped, gets.chomp) puts "You have chosen the state of #{@state_scraped}, and the city of #{@city_scraped.capitalize}." end |
#grab_apple_prodcuts ⇒ Object
78 79 80 81 |
# File 'lib/applephile/command_line_interface.rb', line 78 def grab_apple_prodcuts puts "Price enter a price higher than 150 to see the list of phones, defualt price is 150.".colorize(:blue) display_apple_products_info(@city.get_apple_prods_by_price(gets.chomp)) end |
#run ⇒ Object
5 6 7 8 9 10 11 12 13 14 15 16 17 |
# File 'lib/applephile/command_line_interface.rb', line 5 def run @scrape = Applephile::CraigsList.new puts INTRO_MESSAGE choice = "" while choice != "exit" choice = if choice == "scrape" create_items_from_array(scrape_apple_prodcuts) grab_apple_prodcuts end end end |
#scrape_apple_prodcuts ⇒ Object
73 74 75 76 |
# File 'lib/applephile/command_line_interface.rb', line 73 def scrape_apple_prodcuts @scraped_city_url = @scrape.return_city_link(@state_scraped, @city_scraped) @phones = @scrape.scrape_by_city_url(@scraped_city_url) end |