https://github.com/open-meteo-ruby/open-meteo-ruby
Ruby client for OpenMeteo
https://github.com/open-meteo-ruby/open-meteo-ruby
open-meteo ruby
Last synced: about 1 year ago
JSON representation
Ruby client for OpenMeteo
- Host: GitHub
- URL: https://github.com/open-meteo-ruby/open-meteo-ruby
- Owner: open-meteo-ruby
- License: mit
- Created: 2023-11-03T08:26:39.000Z (over 2 years ago)
- Default Branch: main
- Last Pushed: 2024-10-24T15:29:48.000Z (over 1 year ago)
- Last Synced: 2024-10-25T02:26:05.832Z (over 1 year ago)
- Topics: open-meteo, ruby
- Language: Ruby
- Homepage: https://rubygems.org/gems/open-meteo
- Size: 1.18 MB
- Stars: 5
- Watchers: 2
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE.txt
- Code of conduct: CODE_OF_CONDUCT.md
Awesome Lists containing this project
README
Ruby client for OpenMeteo
This client library lets you easily connect to the OpenMeteo API.
> NOTE: [There is a plan to create official SDKs](https://github.com/open-meteo/open-meteo-website/issues/40) via [FlatBuffers](https://flatbuffers.dev/). However, I couldn't find a Ruby implementation of FlatBuffers in general. Hence, most likely, there won't be a Ruby SDK in the near future.
## Usage
```ruby
require "open_meteo"
location = OpenMeteo::Entities::Location.new(latitude: 52.52.to_d, longitude: 13.41.to_d)
variables = { current: %i[], hourly: %i[weather_code], daily: %i[] }
data = OpenMeteo::Forecast.new.get(location:, variables:)
data.hourly.items.each { |item| puts item.weather_code_symbol }
```
### Forecast models
#### Forecast models in general request
The general forecast endpoint allows for models to be specified which adds suffixes to the response variables if multiple models are selected. You can make use of this by adding models to the forecast variables:
```ruby
variables = {
current: %i[],
hourly: %i[weather_code],
daily: %i[],
models: %i[best_match ecmwf_ifs04],
}
```
Other variables that can be set:
| Variable | Example value |
| ---------- | --------------- |
| `timezone` | `Europe/Berlin` |
#### Forecast models in separate requests
There are separate requests for certain weather models. You can make use of those by providing the model symbol to `Forecast#get`:
```ruby
data = OpenMeteo::Forecast.new.get(location:, variables:, model: :dwd_icon)
```
Default is `:general`.
Available models:
- `:general`: [OpenMeteo Weather Forecast](https://open-meteo.com/en/docs)
- `:dwd_icon`: [DWD ICON](https://open-meteo.com/en/docs/dwd-api)
## Configuration
### Global configuration
There is the possibility to configure `OpenMeteo` globally e.g. in an initializer:
```ruby
# config/initializer/open_meteo.rb
require "open-meteo"
OpenMeteo.configure do |config|
config.host = "api.my-own-open-meteo.com"
config.logger = Rails.logger
config.api_key = "your_open_meteo_api_key"
end
```
```ruby
# some/other/file.rb
forecast = OpenMeteo::Forecast.new
location = OpenMeteo::Entities::Location.new(latitude: 52.52, longitude: 13.41)
variables = { current: %i[weather_code], hourly: %i[], daily: %i[] }
forecast_response = forecast.get(location:, variables:)
```
| Config key | Default value | Remarks |
| ---------- | ---------------------------------------- | --------------------------------------------------------------------------------------------------------------------------- |
| `host` | `"api.open-meteo.com"` | |
| `api_key` | `ENV.fetch("OPEN_METEO_API_KEY", nil)` } | Use the host `customer-api.open-meteo.com` for the commercial version of OpenMeteo. |
| `logger` | `Logger.new($stdout)` | |
| `timeouts` | `{ timeout: 5, open_timeout: 5}` | Uses [Faraday configuration options](https://github.com/lostisland/faraday/blob/main/docs/customization/request-options.md) |
### Configuration of a client instance
You can also create a client that takes a configuration that overwrites the global configuration.
The configuration sent to the client initializer will be shared with the dependent classes:
```ruby
# some/other/file.rb
require "open-meteo"
config =
OpenMeteo::Client::Config.new(logger: Logger.new($stdout), host: "api.my-own-open-meteo.com")
client = OpenMeteo::Client.new(config:)
forecast = OpenMeteo::Forecast.new(client:)
location = OpenMeteo::Entities::Location.new(latitude: 52.52, longitude: 13.41)
variables = { current: %i[weather_code], hourly: %i[], daily: %i[] }
forecast_response = forecast.get(location:, variables:)
```
## Development
Useful commands are defined in the [`Justfile`](Justfile) and can be listed with [`just`](https://github.com/casey/just).
E.g. execute an [example](example.rb) request: `just example`.