openweathermap

This is a crate which lets you easily access current weather data from OpenWeatherMap.

How to use

First add this crate to your dependencies in you Cargo.toml file:

toml [dependencies] openweathermap = "0.0.7"

Then use the crate in your rust source file by calling openweathermap::init() which returns a receiver object which you can then use to call openweathermap::update() to get weather updates like in the following example:

```rust extern crate openweathermap;

fn main() { // start our observatory via OWM let receiver = &openweathermap::init( "Berlin,DE", "metric", "en", "", 10, ); loop { match openweathermap::update(receiver) { Some(response) => match response { Ok(current) => println!( "Today's weather in {} is {}", current.name.asstr(), current.weather[0].main.asstr() ), Err(e) => println!("Could not fetch weather because: {}", e), }, None => (), } } } ```

openweathermap::init() will spawn a thread which asks OpenWeatherMap for the current weather periodically. Whenever there is an update you can get it by using openweathermap::update().

Within the polling period you might get None which tells you that there is no new update available (see the outer match statement in the above example).

You may get an Err object if an error occurs. Initially while waiting for the first update you will get an Err that includes the String "loading..." but also http or json errors may occur. For example if you use an invalid API key you will get 401 Unauthorized.

Reference

openweathermap::init()

Spawns a thread which fetches the current weather from openweathermap.org periodically.

Definition:

pub fn init(location: &str, units: &str, lang: &str, api_key: &str, poll_mins: u64) -> Receiver

Parameters:
Return Value:

Returns the receiver object which you need to get the latest weather update from openweathermap::update().

openweathermap::update()

Get the latest weather update that the spawned thread has fetched.

Definition:

pub fn update(receiver: &Receiver) -> Option<Result<CurrentWeather,String>>

Parameters:
Return Value:

Returns a Option<Result<CurrentWeather,String>> which is None if currently there is no new weather update available.

Otherwise could be Some<CurrentWeather> on success or Err<String> if an error has occurred. The error could be about a http or json issue. For example you will get 401 Unauthorized if your API key is invalid or some json parser error message if there is something wrong with the response from OpenWeatherMap.

On success you get a CurrentWeather object which is a nested struct including all weather properties which are provided. Those properties are well described here. All property names are like in this description except sys.type_ which has a _ appended so that it does not collide with the rust keyword type.