diff --git a/Gemfile b/Gemfile new file mode 100644 index 0000000..fa75df1 --- /dev/null +++ b/Gemfile @@ -0,0 +1,3 @@ +source 'https://rubygems.org' + +gemspec diff --git a/lib/noaa.rb b/lib/noaa.rb index 90801a4..9ef2dcf 100644 --- a/lib/noaa.rb +++ b/lib/noaa.rb @@ -2,6 +2,8 @@ require 'time' require 'nokogiri' require 'geokit' + require 'libxml' + require 'open-uri' rescue LoadError => e if require 'rubygems' then retry else raise(e) @@ -10,7 +12,7 @@ %w(current_conditions forecast forecast_day http_service station station_writer).each { |file| require File.join(File.dirname(__FILE__), 'noaa', file) } -# +# # The NOAA singleton provides methods to conveniently access information from the NOAA weather feed. # For the most part, NOAA.current_conditions and NOAA.forecast will be the only entry point into the # NOAA API you will need; one exception is discussed below. @@ -19,7 +21,7 @@ module NOAA autoload :VERSION, File.join(File.dirname(__FILE__), 'noaa', 'version') class < pressure in inches @@ -131,7 +131,7 @@ def pressure(unit = :in) text_from_node_with_unit('pressure', unit, :in, :mb).to_f end - # + # # The current dew point. # # conditions.dew_point #=> dew point in fahrenheit @@ -142,7 +142,7 @@ def dew_point(unit = :f) text_from_node_with_unit('dewpoint', unit, :f, :c).to_i end - # + # # The current heat index # # conditions.heat_index #=> heat index in fahrenheit @@ -153,7 +153,7 @@ def heat_index(unit = :f) text_from_node_with_unit('heat_index', unit, :f, :c).to_i end - # + # # The current wind chill # # conditions.wind_chill #=> wind chill in fahrenheit @@ -164,7 +164,7 @@ def wind_chill(unit = :f) text_from_node_with_unit('windchill', unit, :f, :c).to_i end - # + # # The current visibility in miles # def visibility diff --git a/lib/noaa/forecast.rb b/lib/noaa/forecast.rb index 365dfa6..f68a513 100644 --- a/lib/noaa/forecast.rb +++ b/lib/noaa/forecast.rb @@ -1,11 +1,11 @@ module NOAA - # + # # A Forecast object represents a multi-day forecast for a particular place. The forecast for a given day can # be accessed using the [] method; e.g. (assuming +forecast+ is a forecast for 12/20/2008 - 12/24/2008): # # forecast[1] #=> ForecastDay for 12/21/2008 # forecast.length #=> 4 - # + # class Forecast class < returns the ForecastDay for the second day - # + # def [](i) forecast_days[i] end diff --git a/lib/noaa/forecast_day.rb b/lib/noaa/forecast_day.rb index 93ff228..358d0b5 100644 --- a/lib/noaa/forecast_day.rb +++ b/lib/noaa/forecast_day.rb @@ -1,5 +1,5 @@ module NOAA - # + # # A ForecastDay contains forecast data for a single day. Each day should start at 6am and # end at 6am the following day (assuming that is invariant on the part of the NOAA's data # feed). ForecastDay objects are accessed using NOAA::Forecast#[] diff --git a/lib/noaa/http_service.rb b/lib/noaa/http_service.rb index 2595b61..6a26d5f 100644 --- a/lib/noaa/http_service.rb +++ b/lib/noaa/http_service.rb @@ -5,15 +5,19 @@ def initialize(http = Net::HTTP) end def get_current_conditions(station_id) - LibXML::XML::Document.string(@HTTP.get(URI.parse("http://www.weather.gov/xml/current_obs/#{station_id}.xml"))) + LibXML::XML::Document.string(get_html_document("http://www.weather.gov/xml/current_obs/#{station_id}.xml")) end def get_forecast(num_days, lat, lng) - LibXML::XML::Document.string(@HTTP.get(URI.parse("http://www.weather.gov/forecasts/xml/sample_products/browser_interface/ndfdBrowserClientByDay.php?lat=#{lat}&lon=#{lng}&format=24+hourly&numDays=#{num_days}"))) + LibXML::XML::Document.string(get_html_document("http://www.weather.gov/forecasts/xml/sample_products/browser_interface/ndfdBrowserClientByDay.php?lat=#{lat}&lon=#{lng}&format=24+hourly&numDays=#{num_days}")) end def get_station_list - LibXML::XML::Document.string(@HTTP.get(URI.parse("http://www.weather.gov/xml/current_obs/index.xml"))) + LibXML::XML::Document.string(get_html_document("http://www.weather.gov/xml/current_obs/index.xml")) + end + + def get_html_document(url) + open(url).read end end end diff --git a/lib/noaa/station.rb b/lib/noaa/station.rb index 1a23df9..c645ae1 100644 --- a/lib/noaa/station.rb +++ b/lib/noaa/station.rb @@ -1,5 +1,5 @@ module NOAA - # + # # Data about an NOAA observation station. When accessing current conditions, the NOAA XML API # takes a station ID as input; thus, to find the current conditions for an arbitrary location, one # must first determine the closest weather station to that location. The NOAA.current_conditions @@ -25,7 +25,7 @@ def find(id) stations.find { |station| station.id == id } end - # + # # Find the station closest to a given location. Can accept arguments in any of the following # three forms (all are equivalent): # @@ -82,10 +82,10 @@ def stations_file # Station name (e.g., "New York City, Central Park") attr_reader :name - + # Two-digit abbreviation for state in which station resides (e.g., "NY") attr_reader :state - + attr_reader :xml_url #:nodoc: def initialize(properties) @@ -94,7 +94,7 @@ def initialize(properties) end @coordinates = GeoKit::LatLng.new(properties['latitude'], properties['longitude']) end - + # Latitude of station def latitude @coordinates.lat