class FindForecast
Public Class Methods
new(command)
click to toggle source
# File lib/find_forecast.rb, line 5 def initialize(command) @first_six = Nokogiri::HTML(open("http://www.weather.com/weather/hourbyhour/graph/New+York+NY+USNY0996:1:US")) @next_eighteen = Nokogiri::HTML(open("http://www.weather.com/weather/hourbyhour/graph/New+York+NY+USNY0996:1:US?pagenum=2&nextbeginIndex=6")) @five_day = Nokogiri::HTML(open("http://www.weather.com/weather/5-day/New+York+NY+USNY0996:1:US")) intro(command) scrape_forecast(command) end
Public Instance Methods
intro(command)
click to toggle source
# File lib/find_forecast.rb, line 13 def intro(command) introduction = "\nToday's date: #{Date.today}.\nThe weather for the next #{command} is as follows:\n\n" puts introduction end
scrape_five_days()
click to toggle source
# File lib/find_forecast.rb, line 47 def scrape_five_days forecasts = @five_day.css("div.wx-daypart").collect { |section| section.text.gsub(/\n/,'') } forecasts.each_with_index do |forecast, i| puts forecast.gsub("Details","") #.gsub("°F", "°").gsub("AM","AM\n").gsub("PM","PM\n").gsub("FEE", "\nFEE").gsub("°", "°\n").gsub("%", "%\n").gsub("Show 15 Minute Details","").gsub("mph","mph\n\n") if i % 4 == 0 puts end end
scrape_forecast(command)
click to toggle source
# File lib/find_forecast.rb, line 18 def scrape_forecast(command) scrape_six if command == "6 hours" scrape_twenty_four if command == "24 hours" scrape_five_days if command == "5 days" end
scrape_six()
click to toggle source
# File lib/find_forecast.rb, line 24 def scrape_six to_delete = @first_six.css("span.wx-date-label").collect {|x| x.text} + @first_six.css("span.wx-day-label").collect {|x| x.text} forecasts = @first_six.css("div.wx-timepart").collect { |section| section.text.gsub(/\n/,'') } forecasts.each_with_index do |forecast, i| to_delete.each do |word| forecast.gsub!("#{word}", "") end puts forecast.gsub("°F", "°").gsub("AM","AM\n").gsub("PM","PM\n").gsub("FEE", "\nFEE").gsub("°", "°\n").gsub("%", "%\n").gsub("Show 15 Minute Details","").gsub("mph","mph\n\n") if i % 4 == 0 end end
scrape_twenty_four()
click to toggle source
# File lib/find_forecast.rb, line 35 def scrape_twenty_four scrape_six to_delete = @next_eighteen.css("span.wx-date-label").collect {|x| x.text} + @next_eighteen.css("span.wx-day-label").collect {|x| x.text} forecasts = @next_eighteen.css("div.wx-timepart").collect { |section| section.text.gsub(/\n/,'') } forecasts.each_with_index do |forecast, i| to_delete.each do |word| forecast.gsub!("#{word}", "") end puts forecast.gsub("°F", "°").gsub("AM","AM\n").gsub("PM","PM\n").gsub("FEE", "\nFEE").gsub("°", "°\n").gsub("%", "%\n").gsub("Show 15 Minute Details","").gsub("mph","mph\n\n") end end