Saturday, August 27, 2011

Usage of Barometer gem

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).

  require 'barometer'
  barometer = Barometer.new("Paris")
  weather = barometer.measure  puts weather.current.temperature

The available sources are:

  Wunderground.com (:wunderground) [default]    Yahoo! Weather (:yahoo)


Google Weather (:google) Weather.com (:weather_dot_com) [requires key]

WeatherBug.com (:weather_bug) [requires key]

Source Configuration:

Barometer can be configured to use multiple weather service APIs (either in a primary/failover config or in parallel). Each weather service can also have its own config.

Weather services in parallel

  Barometer.config = { 1 => [:yahoo, :google] }

Weather services in primary/failover

  Barometer.config = { 1 => [:yahoo], 2 => :wunderground }

Weather services, one with some configuration. In this case we are setting a weight value, this weight is respected when calculating averages.

  Barometer.config = { 1 => [{:wunderground => {:weight => 2}}, :google] }

Weather services, one with keys.

  Barometer.config = { 1 => [:yahoo, {:weather_dot_com => {:keys =>

{:partner => PARTNER_KEY, :license => LICENSE_KEY } }}] }

Multiple weather API, with hierarchy:

  require 'barometer'  # use yahoo and google, if they both fail, use wunderground

Barometer.config = { 1 => [:yahoo, :google], 2 => :wunderground }
barometer = Barometer.new("Paris") weather = barometer.measure
puts weather.current.temperture

No comments:

Post a Comment