-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnoaa_weather.rb
67 lines (58 loc) · 1.65 KB
/
noaa_weather.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
require 'open-uri'
require 'rubygems'
require 'nokogiri'
require 'rest_client'
require 'date'
module NOAA
WEATHER_URL = 'http://www.weather.gov/forecasts/xml/sample_products/browser_interface/ndfdXMLclient.php'
def current_weather(zip)
response = RestClient.get WEATHER_URL, {
:params => {
:zipCodeList => zip,
:product => 'time-series',
:begin => DateTime.now.to_s,
:end => (DateTime.now + 3).to_s,
:appt => 'appt',
}
}
parsed_resp = Nokogiri::XML(response)
times = parsed_resp.css("time-layout start-valid-time").map do |date_elem|
DateTime.parse(date_elem.content)
end
temps = parsed_resp.css("temperature[type=apparent] value").map do |temp_elem|
temp_elem.content
end
zipped = times.zip(temps)
dict = zipped.map do |z|
{
:time => z[0],
:temp => z[1],
}
end
return dict
end
module_function :current_weather
def history_weather(station_code)
url = "http://www.weather.gov/data/obhistory/#{station_code}.html"
today = Date.today.day
yesterday = (Date.today - 1).day
html_doc = Nokogiri::HTML(open(url))
table = html_doc.css('table')[3]
rows = table.css('tr[bgcolor!="#b0c4de"]')
temps = rows.map do |row|
td = row.css('td')
p td.to_xml
date = td[0].content.to_i
if date == today || date == yesterday then
if(td[8].content != '') then
return {
:max => td[8].content.to_i,
:min => td[9].content.to_i,
}
end
end
end
return temps
end
module_function :history_weather
end