Inspired by a raspberry pi project from one that was collecting dust. Goal of the project was to create a simple circuit that would light up the appropriate led based on the current weather conditions. In my search to find a good, preferably free, api provider for weather I stumbled upon openweathermap.

After registering for a free api key, I wrote this ruby class to interface between my raspberry pi and the weather api. I had it ping every 30 seconds to get the latest weather and turn on an led or begin pulsating the led based on temperate and weather conditions.

Here are the details below.

Build Status

Installation

Add this line to your application’s Gemfile:

gem 'openweatherlite'

And then execute:

$ bundle

Or install it yourself as:

$ gem install openweatherlite

Example

weather = OpenWeatherLite::Weather.new('api_key')
weather.by_zip_code(60606)

Usage

OpenWeatherLite::Weather

This class is the api class for openweather

attributes

  • api_key
  • units (metric, imperial)

initialize

Provide an optional api_key

# With optional api_key
OpenWeatherLite::Weather.new('api_key')

# Without api key
weather = OpenWeatherLite::Weather.new
weather.api_key = 'api_key'

by_zip_code

Provide zip_code and optionally country. country defaults to us. Returns OpenWeatherLite::WeatherResponse

weather = OpenWeatherLite::Weather.new('api_key')
# returns OpenWeatherLite::WeatherResponse
response = weather.by_zip_code(60606)

by_city_id

Provide a city_id according to openweathermap and returns OpenWeatherLite::WeatherResponse

# returns OpenWeatherLite::WeatherResponse
weather.by_city_id(2172797)

by_coords

Provide latitude and longitude and returns OpenWeatherLite::WeatherResponse

# returns OpenWeatherLite::WeatherResponse
weather.by_coords(35, 139)

OpenWeatherLite::WeatherResponse

coords

Returns a hash of the coordinates

response.coords
# { latitude: 123, longitude: 123 }

temperature

Returns the temperature value from the reponse. Unit is in units provided by to OpenWeatherLite::Weather

  response.temperature

pressure

Returns the pressure value from the response

  response.pressure

humidity

Returns the humidity value from the response

  response.humidity

temperature_range

Returns an array with the [min, max] values from the response

  response.temperature_range

wind

Returns the wind details hash with speed and deg from the response

  response.wind

cloudy?

Returns true or false if there are any clouds

  response.cloudy?

clouds

Returns the percentage of clouds in a hash

  response.clouds

rainy?

Returns true or false depending on whether it is, or has rained in the past 3 hours

rain

Returns the amount that it has rained in the last 3 hours in a hash

  response.rain
  # { '3h' => .445 }

snow?

Returns true or false depending on whether it is, or has rained in the past 3 hours

  response.snowy?

snow

Returns the amount that it has snowed in the last 3 hours in a hash

  response.snow
  # { '3h' => .445 }

sunrise

Returns the time for sunrise today

  response.sunrise

sunset

Returns the time for sunset today

  response.sunset

Source

Github source code: OpenWeatherLite