Class: FindForecast

Inherits:
Object
  • Object
show all
Defined in:
lib/find_forecast.rb

Instance Method Summary collapse

Constructor Details

#initialize(command) ⇒ FindForecast

Returns a new instance of FindForecast.



5
6
7
8
9
10
11
# 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

Instance Method Details

#intro(command) ⇒ Object



13
14
15
16
# 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_daysObject



47
48
49
50
51
52
53
# 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) ⇒ Object



18
19
20
21
22
# 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_sixObject



24
25
26
27
28
29
30
31
32
33
# 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_fourObject



35
36
37
38
39
40
41
42
43
44
45
# 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