From 8cab85387ea7c65bdc28894ecbb37d1db323fd7f Mon Sep 17 00:00:00 2001 From: Michael Cordell Date: Sat, 26 Apr 2014 14:04:25 -0700 Subject: [PATCH] Allows for more flexible configuration of station For example: NOOA.configure { |config| config.station = 'TAPA' } --- lib/noaa.rb | 14 +++++++++++++- lib/noaa/configuration.rb | 13 +++++++++++++ 2 files changed, 26 insertions(+), 1 deletion(-) create mode 100644 lib/noaa/configuration.rb diff --git a/lib/noaa.rb b/lib/noaa.rb index 90801a4..b5986af 100644 --- a/lib/noaa.rb +++ b/lib/noaa.rb @@ -8,7 +8,7 @@ end end -%w(current_conditions forecast forecast_day http_service station station_writer).each { |file| require File.join(File.dirname(__FILE__), 'noaa', file) } +%w(configuration 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. @@ -59,5 +59,17 @@ def current_conditions_at_station(station_id) def forecast(num_days, lat, lng) Forecast.from_xml(HttpService.new.get_forecast(num_days, lat, lng)) end + + def configure + yield configuration if block_given? + end + + def configuration + @configuration ||= Configuration.new + end + + def default_station + configuration.station + end end end diff --git a/lib/noaa/configuration.rb b/lib/noaa/configuration.rb new file mode 100644 index 0000000..7b09eae --- /dev/null +++ b/lib/noaa/configuration.rb @@ -0,0 +1,13 @@ +require 'yaml' +module NOAA + class Configuration + attr_accessor :station + + def load_with_hash(hash) + hash.each do |k, v| + setter_command = "#{k}=" + send(setter_command, v) if respond_to? setter_command + end + end + end +end