Skip to content

Instantly share code, notes, and snippets.

@benaskins
Created June 18, 2009 05:57
Show Gist options
  • Save benaskins/131737 to your computer and use it in GitHub Desktop.
Save benaskins/131737 to your computer and use it in GitHub Desktop.
WEATHER_FINDER_OPTIONS = {
:include => [:forecasts, :conditions, :point_forecasts, :uv_index, :sun, :moon, :warnings, :district_forecasts, :marine_forecast, :moon_phases, :news_items,
:historical_observations, :daily_observations, :position, :almanac, :radar_animator, :radar_still, :satellite_animator, :satellite_still, :links],
:image => {:type => "syn", :size => "640x480", :days => 0},
:days => 7
}
Weather.find_by_location_name("#{location_name} #{state}", WEATHER_FINDER_OPTIONS)
INCLUDES_MAP = {
:forecasts => "fc=1",
:point_forecasts => "fc=3",
:district_forecasts => "dist_fc=1",
:conditions => "obs=1",
:warnings => "warn=2",
:state_forecasts => "state_fc=1",
:uv_index => "uv=1",
:sun => "fc_sun=2",
:moon => "fc_moon=1",
:historical_observations => "histobs=1",
:daily_observations => "dlyobs=7",
:position => "latlon=1",
:moon_phases => "moon=1",
:news_items => "news=2",
:almanac => "almanac=1",
:links => "links=1",
:radar_animator => "ra=1",
:radar_still => "rs=1",
:satellite_animator => "sa=1",
:satellite_still => "ss=1",
:marine_forecast => "marine=1",
:tides => "tides=4",
:tide_height => "tideh=1"
}
def find_by_location_name(location_name, options={})
location_name = location_name.gsub(" ", "%20").gsub("-", "%20")
options.merge!(:params => "&lt=aploc&ln=#{location_name}")
find(options)
end
def find(options)
make_request(build_params(nil, options))
end
def build_params(location_code, options)
params = location_code ? "&lc=#{location_code}" : ""
params += options.delete(:params) if options[:params]
params += include_params(options.delete(:include)) if options[:include]
params += include_image(options.delete(:image)) if options[:image]
params += parse_params(options) unless options.empty?
params
end
protected
def make_request(params)
response = @@connection.request(params)
parse(response)
end
def include_params(includes)
includes.inject("") do |params, relationship|
if param = INCLUDES_MAP[relationship]
params += "&#{param}"
else
params
end
end
end
def include_image(image)
"&images=#{image[:type]}(days=#{image[:days]};size=#{image[:size]})"
end
def parse_params(options)
options.inject("") do |params, (key, value)|
params += "&#{key}=#{value}"
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment