Introducing WeatherMan, a Ruby Gem.

posted Sep 30th by Jared

So, I was looking into using RWeather by Carlos Kozuszko which is a Ruby Gem for accessing the Weather Channel, inc. XML API. I need to display the weather for various regions in Italy for a project I’m working on. The only problem with RWeather is that it doesn’t currently implement the whole API functionality, most importantly of which is forecasts.

No big deal, I’ll just implement that in RWeather and submit a patch, I thought. But when I dived into the code to do that, I couldn’t find an obvious spot for adding forecasts in RWeather’s OO hierarchy. RWeather was obviously designed for getting current weather conditions first and foremost. I also didn’t really like the way locations were handled.

So, I decided to create my own Ruby Gem(my 1st one!). Introducing WeatherMan!

WeatherMan implements most of the weather.com XML API. It it based in part on RWeather but adds some functionality and is structured slightly differently. With it you can search for locations, get current conditions and forecasts for a location, and access the promotional links that weather.com asks you to display when using the API.

Installation

First off, WeatherMan requires XmlSimple. Since it’s hosted on RubyForge and WeatherMan is on GitHub you’ll have to install this yourself.

% sudo gem install xml-simple

Then, it’s just a matter of adding the GitHub gem server as a source for RubyGems(If you haven’t already) and installing WeatherMan.

% gem sources -a http://gems.github.com # You only need to do this once.
% sudo gem install jdpace-weatherman

Usage

Find or load a location:

require 'weather_man'
WeatherMan.partner_id = '0123456789'
WeatherMan.license_key = '0123456789abcdef'

# Search for a location
# Returns an array of WeatherMan objects
locations = WeatherMan.search('New York')

# or if you know the location id or just want to use a US Zip code
ny = WeatherMan.new('USNY0996')

Fetch the weather:

# Fetch the current conditions and 5 day forecast in 'standard' units
weather = ny.fetch

# Fetch only current conditions in metric units
weather = ny.fetch(:days => 0, :unit => 'm')

# Fetch a 3 day forecast only
weather = ny.fetch(:days => 3, :current_conditions => false)

Look at the Current Conditions:

# current temperature
temp = weather.current_conditions.temperature

feels_like = weather.current_conditions.feels_like

wind_speed = weather.current_conditions.wind.speed
wind_direction = weather.current_conditions.wind.direction

Look at the forecast:

# how many days?
weather.forecast.size

# Some different forecasts
weather.forecast.today
weather.forecast.tomorrow
weather.forecast.monday
weather.forecast.for(Date.today)
weather.forecast.for(3.days.from_now) # Note: using rails core extensions
weather.forecast.for('Sep 1')

# data for a forecast
friday = weather.forecast.friday

high_temp = friday.high
low_temp = friday.low

# forecasts are split into 2 parts day/night
friday.day.description # Partly Cloudy, Sunny...
friday.day.chance_percipitation # 0..100

night_wind_speed = friday.night.wind.speed

The Weather Channel requires that you 4 promotional links for them if you use their service. Here’s how to access those links:

# The array of pr links
weather.links

# Getting the first links text and url
weather.links.first.text
weather.links.first.url

That’s pretty much it. Check out the README for full documentation. (It’s not there yet but I plan on listing a complete set of attributes for the current conditions and forecasts). Play with it if you like and let me know what you think.

 

6 comments

  • Timothy Johnson's gravitar Timothy Johnson
    Oct 1st

    Don’t forget to add these if using outside of a Rails app, if you just want to play from your commandline, or in another ruby project:

    require ‘rubygems’ require ‘date’ require ‘active_support’

  • Jared Pace's gravitar Jared Pace
    Oct 1st

    Thanks Tim.

    I’ve actually only worked with WeatherMan from irb so far. Mine seems to automatically load ‘rubygems’ and ‘date’. I should probably update the README with those requires though. ActiveSupport should only be needed if you want to use this style syntax:

    weather.forecast.for(3.days.from_now)

  • Bob Aman's gravitar Bob Aman
    Oct 13th

    Since this is your first gem, I figure I’ll point you in what I would consider the right direction.

    Never use ActiveSupport as a dependency unless you are making something Rails-specific. Whatever you have to do to avoid that dependency, do it. ActiveSupport mucks about with way too much of Ruby itself for it to be safely and securely used by most other web frameworks. I consider ActiveSupport to be poisonous.

    And second, at the top of your main Ruby file, require all of your dependencies. The date library should have been required there. That way people don’t have to require the dependencies manually in irb, they can just require the main Ruby file. Just because Rails does it for you doesn’t mean you should omit it.

    And finally, I personally think hosting gems on GitHub is a bad idea, but that’s just me.

  • Bob Aman's gravitar Bob Aman
    Oct 13th

    In any case, the code itself is a lot nicer than RWeather, so thanks!

  • Jared Pace's gravitar Jared Pace
    Oct 13th

    Bob,

    Thanks for the advice. I’m really interested in what people think since this is my first gem.

    ActiveSupport in not actually a dependency. I just used an example that would require you to have active support ie. weather.forecast.for(3.days.from_now). The 3.days.from_now part is the part that uses the ActiveSupport magic which just returns a Date. You can pass a date, time, or string that represents a date in however you like.

    Yeah, my initial release did not explicitly require those dependencies. I’ve since changed that and posted a new version of the gem after Timothy’s comment. However, I didn’t place them all at the top of the main file. The dependencies for the response object are specified in that file. What’s the correct way for doing that – In the main file, or where the dependencies are used?

    I know what you mean about hosting gems on GitHub too. It’s a pain when a dependency is located on another gem server as is the case with xml-simple. I was thinking about publishing the next version to RubyForge after I update the Documentation and get some more feed back.

    Thanks for the input.

  • Caiden's gravitar Caiden
    Oct 16th

    Its great! WeatherMan is a Ruby gem that provides current weather plus forecasts.

leave a comment