A multi API consuming weather forecasting superstar.
Barometer provides a common public API to one or more weather services (APIs) of your choice. Weather services can co-exist to retrieve extensive information, or they can be used in a hierarchical configuration where lower preferred weather services are only used if previous services are unavailable.
Version 0.3.1 is the current release of this gem. The gem is available from github (attack-barometer) or rubyforge (barometer). It is fully functional (for three weather service APIs).
Currently this project is in development and will only work for a few weather services (wunderground, google, yahoo).
Features to be added next:
In most cases you will need to have a free google geocode api key. Get one here: code.google.com/apis/maps/signup.html Then put it in the file ’~/.barometer’ by adding the line: geocode_google: YOUR_KEY_HERE
You will need this for:
Why? HTTParty was created and designed specifically for consuming web services. I choose to use this over using the Net::HTTP library directly to allow for faster development of this project.
HTTParty is also extended to include configurable Timeout support.
Why? Barometer deals with time information for locations all over the world. This information doesn’t mean that much if it can’t be converted to times that don’t correspond to the applicable timezone. Tzinfo handles this time zone manipulation.
Why? Barometer returns the weather for a given location. Most weather service APIs are somewhat restricted on the query format they receive. To bridge this gap and allow for maximum flexibility on the ‘barometer’ query format, the query will be geo-coded using the Google geocoding service, if required. Graticule can provide this geocoding interface.
Using Graticule requires a free Google API key for geocoding. It is possible to use barometer without geocoding, though your query format will be limited to that of the weather service API.
ALTERNATE: If you supply a Google API key but don’t install the Graticule gem, HTTParty will be used instead to provide the same geocoding. Basically Graticule is only used if it exists.
NOTE: you can force Barometer not to use Graticule, even if you have it installed using the following:
Barometer.skip_graticule = true
You can use barometer right out of the box, as it is configured to use one register-less (no API key required) international weather service (wunderground.com).
For better results, signup for a google-map key and enhance your barometer with geo-coding.
require 'barometer' Barometer.google_geocode_key = "THE_GOOGLE_API_KEY" barometer = Barometer.new("Paris") weather = barometer.measure puts weather.current.temperture
require 'barometer' Barometer.google_geocode_key = "THE_GOOGLE_API_KEY" # use yahoo and google, if they both fail, use wunderground Barometer.selection = { 1 => [:yahoo, :google], 2 => :wunderground } barometer = Barometer.new("Paris") weather = barometer.measure puts weather.current.temperture
You can use barometer from the command line.
# barometer berlin
This will output the weather information for the given query. See the help for more command line information.
# barometer -h
There is a Sinatra application that demos the functionality of Barometer, and provides Barometer information. Start this local demo with:
# barometer -w
NOTE: This requires the gems "sinatra" and "vegas".
What would cause a weather service to fail? The most obvious is that the particular weather service in currently unavailable or not reachable. Other possible reasons would include not having the API (or a valid API key for the particular weather service, if required), not providing a valid query, or providing a query for a location not supported by the weather service.
For example, if you look at the example above, the query of "Paris" refers to a city in France. Yahoo weather services only supports weather results for USA (at least at the time of writing). Therefore, Barometer would not use Yahoo, just Google and failover to use Wunderground (if needed).
You can use weather service drivers directly. Below is an example to use Wunderground, but since the driver interface is abstracted it will be the same for all supported services.
require 'barometer' Barometer.google_geocode_key = "THE_GOOGLE_API_KEY" query = Barometer::Query.new("Paris") weather = Barometer::Service.source(:wunderground).measure(query) puts weather.current.temperture # OR, even more raw measurement = Barometer::Measurement.new weather = Barometer::Wunderground.measure_all(measurement, "Paris") puts weather.current.temperture
NOTE: The disadvantage to using the drivers directly is that you lose the advantage of redundancy/failover added by the Module as a whole.
NOTE: You still must create the Barometer::Query object with your query string instead of directly feeding the query string to the service (as in bootstrap example #1). The Barometer::Query object has behavior required by the service that a regular String doesn’t have. Using a driver directly WILL accept a String (as in bootstrap example #2).
After you have measured the data, Barometer provides several methods to help you get the data you are after. All examples assume you already have measured the data as shown in the above examples.
weather.default # returns measurement for default source weather.current # returns current_measurement for default weather.now # returns current_measurement for default weather.forecast # returns all forecast_measurements for default weather.today # returns forecast_measurement for default today weather.tomorrow # returns forecast_measurement for default tomorrow puts weather.now.temperature.c puts weather.tomorrow.high.c
weather.source(:wunderground) # returns measurement for specified source weather.sources # lists all successful sources puts weather.source(:wunderground).current.temperature.c
# note, the date is the date of the locations weather, not the date of the # user measuring the weather date = Date.parse("01-01-2009") weather.for(date) # returns forecast_measurement for default on date weather.source(:wunderground).for(date) # same as above but specific source puts weather.source(:wunderground).for(date).high.c
# note, the time is the time of the locations weather, not the time of the # user measuring the weather time = Time.parse("13:00 01-01-2009") weather.for(time) # returns forecast_measurement for default at time weather.source(:wunderground).for(time) # same as above but specific source puts weather.source(:wunderground).for(time).low.f
If you consume more then one weather service, Barometer can provide averages for the values (currently only for the ‘current’ values and not the forecasted values).
require 'barometer' Barometer.google_geocode_key = "THE_GOOGLE_API_KEY" # use yahoo and wunderground Barometer.selection = { 1 => [:yahoo, :wunderground] } barometer = Barometer.new("90210") weather = barometer.measure puts weather.temperture
This will calculate the average temperature as given by :yahoo and :wunderground
After you have measured the data, Barometer provides several "simple answer" methods to help you get answers to some basic questions. All examples assume you already have measured the data as shown in the above examples.
All of these questions are ultimately specific to the weather source(s) you are configured to use. All sources that have successfully measured data will be asked, but if there is no data that can answer the question then there will be no answer.
# 1st parameter is the threshold wind speed for being windy # 2nd parameter is the utc_time for which you want to know the answer, # this defaults to the current time # NOTE: in my example the values are metric, so the threshold is 10 kph weather.windy?(10)
# 1st parameter is the threshold pop (%) for being wet # 2nd parameter is the utc_time for which you want to know the answer, # this defaults to the current time # NOTE: in my example the threshold is 50 % weather.wet?(50)
# 1st parameter is the utc_time for which you want to know the answer, # this defaults to the current time weather.sunny?
# 1st parameter is the utc_time for which you want to know the answer, # this defaults to the current time weather.day?
# 1st parameter is the utc_time for which you want to know the answer, # this defaults to the current time weather.night?
Barometer attempts to be a common API to any weather service API. I have included several weather service ‘drivers’, but I know there are many more available. Please use the provided ones as examples to create more.
Copyright © 2009 Mark G. See LICENSE for details.