Skip to content

open-meteo-ruby/open-meteo-ruby

Repository files navigation

Ruby client for OpenMeteo

CI CodeCov Gem Version

This client library lets you easily connect to the OpenMeteo API.

NOTE: There is a plan to create official SDKs via FlatBuffers. 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

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:

variables = {
  current: %i[],
  hourly: %i[weather_code],
  daily: %i[],
  models: %i[best_match ecmwf_ifs04],
}

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:

data = OpenMeteo::Forecast.new.get(location:, variables:, model: :dwd_icon)

Default is :general.

Available models:

Configuration

Global configuration

There is the possibility to configure OpenMeteo globally e.g. in an initializer:

# 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
# 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

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:

# 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 and can be listed with just.

E.g. execute an example request: just example.