Skip to content

Instantly share code, notes, and snippets.

@RachelSa
Created September 19, 2016 02:28
Show Gist options
  • Save RachelSa/f146c895be9fd10f3d0537028695dd11 to your computer and use it in GitHub Desktop.
Save RachelSa/f146c895be9fd10f3d0537028695dd11 to your computer and use it in GitHub Desktop.
My first command-line app, which suggests fresh foods based on user's location and the current month.
#A command-line app that suggests fresh foods
#based on user's location and the current month.
#The app automatically retrieves date and asks
#user for their region in the continental US.
#Ideas for expansion and improvements:
#allow user to choose alternate date,
#sort results by fruit and veg
class Local_Fresh
Months = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"] #for converting month number to word
def current_month #gets current month and converts to word
current = Time.new.month - 1
Months[current]
end
def welcome #welcomes user
puts "Welcome to Local Fresh!"
end
def loc_ask #Asks user for location and gets location
sleep 1
puts "Let's find out where you are in the US!"
sleep 2
puts "Please write NW for northwest, NE for northeast, SW for southwest, or SE for southeast."
@loc = gets.strip
end
def invalid_loc #re-asks if user enters invalid location
puts "Woops, I don't know that place. Please enter NW, NE, SW, or SE."
@loc = gets.strip
loc_converter
end
def loc_converter
if @loc == "NW"
return "northwest"
elsif @loc == "NE"
return "northeast"
elsif @loc == "SW"
return "southwest"
elsif @loc == "SE"
return "southeast"
else return invalid_loc
end
end
def confirmation #confirms location
puts "Great! You are in the US #{loc_converter} in #{current_month}, correct? y/n"
@confirm = gets.strip
end
def invalid_response #re-asks if user enters invalid response
puts "Not sure what that means. Please write 'y' for yes or 'n' for no."
@confirm = gets.strip
response
end
def response #gives user foods that match their time and location
if @confirm == "y" || @confirm == "Y"
puts "Nice! Here are the foods fresh in your area:"
elsif @confirm == "n" || @confirm == "N"
loc_ask
confirmation
response
else invalid_response
end
end
def intro #compiles intro methods
welcome
loc_ask
confirmation
response
end
def food_finder(list, month, region) #makes raw list of food matches
month_matches = []
region_matches = []
result = []
list.each do |food,info|
info[:inSeason].each do|el|
if el == month
month_matches.push(food)
end
end
end
list.each do |food, info|
info[:region].each do |el|
if el == region
region_matches.push(food)
end
end
end
month_matches.each do |food|
if region_matches.include?(food)
result.push(food)
end
end
return result
end
def readable(list, matches) #makes raw list readable
if matches.count == 0
puts "Darn! No suggestions available."
end
output = []
matches.each do |food|
symbol = food.to_sym
output.push(list[symbol][:name])
end
puts output
end
def full_run(foods) #complies all methods
intro
readable(foods, food_finder(foods, current_month, loc_converter))
end
end
#food database
foods = {
:watermelon => {
:name => "watermelons",
:inSeason => ["May", "June", "July", "August", "September"],
:region => ["northeast", "northwest", "southeast", "southwest"]
},
:bartlettpears => {
:name => "bartlett pears",
:inSeason => ["August", "September", "October", "November", "December", "January"],
:region => ["northwest"]
},
:cranberries => {
:name => "cranberries",
:inSeason => ["September", "October", "November"],
:region => ["northeast", "northwest"]
},
:broccoli => {
:name => "broccoli",
:inSeason => ["September", "October", "November", "March", "April", "May"],
:region => ["northeast", "southwest", "northwest"]
},
:pineapple => {
:name => "pineapples",
:inSeason => ["March", "April", "May", "June", "July"],
:region => ["southeast", "southwest"]
},
:kale => {
:name => "kale",
:inSeason => ["August", "September", "October", "November", "December"],
:region => ["northeast", "northwest", "southeast", "southwest"]
},
:pomegranate => {
:name => "pomegranates",
:inSeason => ["September", "October", "November", "December", "January", "February"],
:region => ["southwest"]
},
:garlic => {
:name => "garlic",
:inSeason => ["July", "August", "September", "October", "November"],
:region => ["northeast", "southwest", "northwest"]
},
:artichoke => {
:name => "artichokes",
:inSeason => ["February", "March", "April", "May"],
:region => ["southwest"]
},
:fiddleheads => {
:name => "fiddlehead ferns",
:inSeason => ["April", "May"],
:region => ["northeast"]
},
:snowpeas => {
:name => "snow peas",
:inSeason => ["February", "March", "April", "May", "September", "October", "November"],
:region => ["northwest", "northeast"]
},
:rhubarb => {
:name => "rhubarb",
:inSeason => ["April", "May", "June", "July", "August", "September"],
:region => ["northeast", "northwest"]
},
:mango => {
:name => "mangoes",
:inSeason => ["March", "April", "May", "June", "July", "August"],
:region => ["southeast", "southwest"]
},
:cucumbers => {
:name => "cuccumbers",
:inSeason => ["May", "June", "July", "August"],
:region => ["northeast", "northwest", "southeast", "southwest"]
},
:chiliPeppers => {
:name => "chili peppers",
:inSeason => ["August", "September", "October"],
:region => ["northeast", "southeast", "southwest", "northwest"]
},
:clementines => {
:name => "clementines",
:inSeason => ["October", "November", "December", "January", "February"],
:region => ["southeast", "southwest"]
},
:corn => {
:name => "corn",
:inSeason => ["May", "June", "July", "August", "September", "October"],
:region => ["southwest", "southeast", "northeast", "northwest"]
},
:tomato => {
:name => "tomatoes",
:inSeason => ["June", "July", "August", "September"],
:region => ["southwest", "southeast", "northeast", "northwest"]
},
:limes => {
:name => "limes",
:inSeason => ["May", "June", "July", "August", "September", "October"],
:region => ["southeast"]
},
:eggplant => {
:name => "eggplants",
:inSeason => ["July", "August", "September", "October"],
:region => ["northeast", "southwest"]
}
}
#calls object
Local_Fresh.new.full_run(foods)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment