OpenWeatherApi wrapper for Ruby
Simple wrapper for Open Weather Map API.
Please visit the this link for more information.
Installation
Add this line to your application's Gemfile:
gem 'open-weather-api'
And then execute:
$ bundle install
Or install it yourself as:
$ gem install open-weather-api
Usage
First, you need to init the API:
Rails
# config/initializers/open-weather-api.rb
# Note that 'config' is an instance of `OpenWeatherAPI::API` (just name it as you like).
OpenWeatherAPI.configure do |config|
# API key
config.api_key = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
# Optionals
config.default_language = 'es' # 'en' by default
config.default_country_code = 'es' # nil by default (ISO 3166-1 alfa2)
config.default_units = 'metric' # 'metric' by default
end
Outside of the configuration file, you can access the api
object as follows:
Rails.configuration.open_weather_api
Generic
open_weather_api = OpenWeatherAPI::API.new api_key: "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx", default_language: 'es', default_units: 'metric', default_country_code: 'es'
# ...
Finally, you can use the different resources of the API:
Current Weather
By city name:
json = open_weather_api.current city: 'Santa Cruz de Tenerife', country_code: 'es'
By city id:
json = open_weather_api.current id: 6360638
By multiple cities ids:
json = open_weather_api.current id: [6360638, 2511401]
By geolocation:
json = open_weather_api.current lon: -16.20302, lat: 28.53924
By zipcode:
json = open_weather_api.current zipcode: 38190, country_code: 'es'
By a geolocated rectangle:
json = open_weather_api.current rectangle: { topleft: { lat: -16.3319, lon: 28.5046 }, bottomright: { lat: -16.1972, lon: 28.4400}, zoom: 10 }
By a geolocated circle (WARNING: Unexpected behaviour by API):
json = open_weather_api.current circle: { lat: -16.3319, lon: 28.5046 }, cities_count: 2
For more information about the API, visit http://openweathermap.org/current.
Forecast
Hourly (actually, every 3 hours, up to 5 days)
By city name:
json = open_weather_api.forecast :hourly, city: 'Santa Cruz de Tenerife', country_code: 'es'
By city id:
json = open_weather_api.forecast :hourly, id: 6360638
By geolocation:
json = open_weather_api.forecast :hourly, lon: -16.20302, lat: 28.53924
For more information about the API, visit http://openweathermap.org/forecast5.
Other
Retrieve icon url:
open_weather_api.current city: 'Santa Cruz de Tenerife', country_code: 'es' do |json|
puts "Icon url: #{api.icon_url json['weather'].first['icon']}"
end
You can add manually any parameter you need for each request, and they will override the computed parameters:
open_weather_api.current city: 'Balashikha', country_code: "ru", lang: "ru"
Also, you can define the response format with the :mode
parameters. Valid formats are :json
(returns a Hash
), :xml
and :html
(both return a String
):
open_weather_api.current city: 'Santa Cruz de Tenerife', mode: :xml do |xml_str|
puts "XML data: #{xml_str}"
end
You can use ruby blocks to handle the response:
open_weather_api.current city: 'Santa Cruz de Tenerife', country_code: 'es', mode: :json do |json|
puts JSON.pretty_generate(json)
end
Authors
This project has been developed by:
Avatar | Name | Nickname | |
---|---|---|---|
Daniel Herzog | Wikiti | [email protected] |
Contributing
- Fork it ( https://gitlab.com/wikiti-random-stuff/open-weather-api/fork/new )
- Create your feature branch (
git checkout -b my-new-feature
) - Commit your changes (
git commit -am 'Add some feature'
) - Run tests (
rake test
) - Push to the branch (
git push origin my-new-feature
) - Create new Merge Request