class Weatherb::API

API object manage the default properties for fulfilling an API request.

Constants

API_URL
DEFAULT_DAILY_FIELDS
DEFAULT_FIELDS
DEFAULT_HOURLY_FIELDS
DEFAULT_NOWCAST_FIELDS
DEFAULT_REALTIME_FIELDS
SUCCESS_STATUSES

Public Class Methods

new(api_key) click to toggle source

Create a new instance of the Weatherb::API using your API key. Also create Faraday connection with ParseJson middleware.

@param api_key [String] ClimaCell API key.

# File lib/weatherb/api.rb, line 69
def initialize(api_key)
  @api_key = api_key

  @connection = Faraday.new API_URL do |conn|
    conn.response :json, content_type: /\bjson$/

    conn.use FaradayMiddleware::RaiseError

    conn.adapter Faraday.default_adapter
  end
end

Public Instance Methods

daily(lat:, lon:, unit_system: 'si', start_time: 'now', end_time: nil, fields: DEFAULT_FIELDS | DEFAULT_DAILY_FIELDS) click to toggle source

Daily (<= 15d out) The daily API call provides a global daily forecast with summaries up to 15 days out.

Note Daily results are returned and calculated based on 6am to 6am periods (meteorological timeframe) in UTC (GMT+0). Therefore, requesting forecast for locations with negative GMT offset may provide the first element with yesterday's date.

@param lat [Float] Latitude, -87 to 89. @param lon [Float] Longitude, -180 to 180. @param unit_system [String] Unit system, “si” or “us”. @param start_time [String] Start time in ISO 8601 format “2019-03-20T14:09:50Z”, or “now”. @param end_time [String] End time in ISO 8601 format “2019-03-20T14:09:50Z”. @param fields [Array<String>] Selected fields from ClimaCell data layer (such as “precipitation” or “wind_gust”).

@return [Hash]

# File lib/weatherb/api.rb, line 190
def daily(lat:, lon:, unit_system: 'si', start_time: 'now', end_time: nil, fields: DEFAULT_FIELDS | DEFAULT_DAILY_FIELDS)
  path = 'weather/forecast/daily'

  query = {
    apikey: @api_key,
    lat: lat,
    lon: lon,
    unit_system: unit_system,
    start_time: start_time,
    end_time: end_time,
    fields: fields
  }

  response = @connection.get(path, query)
  response_body(response)
end
hourly(lat:, lon:, unit_system: 'si', start_time: 'now', end_time: nil, fields: DEFAULT_FIELDS | DEFAULT_HOURLY_FIELDS) click to toggle source

Hourly (<= 96hr out) The hourly call provides a global hourly forecast, up to 96 hours (4 days) out, for a specific location.

@param lat [Float] Latitude, -87 to 89. @param lon [Float] Longitude, -180 to 180. @param unit_system [String] Unit system, “si” or “us”. @param start_time [String] Start time in ISO 8601 format “2019-03-20T14:09:50Z”, or “now”. @param end_time [String] End time in ISO 8601 format “2019-03-20T14:09:50Z”. @param fields [Array<String>] Selected fields from ClimaCell data layer (such as “precipitation” or “wind_gust”).

@return [Hash]

# File lib/weatherb/api.rb, line 155
def hourly(lat:, lon:, unit_system: 'si', start_time: 'now', end_time: nil, fields: DEFAULT_FIELDS | DEFAULT_HOURLY_FIELDS)
  path = 'weather/forecast/hourly'

  query = {
    apikey: @api_key,
    lat: lat,
    lon: lon,
    unit_system: unit_system,
    start_time: start_time,
    end_time: end_time,
    fields: fields
  }

  response = @connection.get(path, query)
  response_body(response)
end
nowcast(lat:, lon:, unit_system: 'si', timestep: 1, start_time: 'now', end_time: nil, fields: DEFAULT_FIELDS | DEFAULT_NOWCAST_FIELDS) click to toggle source

Nowcast (<= 360min out) The nowcast call provides forecasting data on a minute-­by-­minute basis, based on ClimaCell’s proprietary sensing technology and models.

6 hours of nowcasting is available for the US. 3 hours of precipitation and 6 hours of non-precipitation data is provided for Japan and Western Europe.

@param lat [Float] Latitude, -87 to 89. @param lon [Float] Longitude, -180 to 180. @param unit_system [String] Unit system, “si” or “us”. @param timestep [Integer] Time step in minutes, 1-60. @param start_time [String] Start time in ISO 8601 format “2019-03-20T14:09:50Z”, or “now”. @param end_time [String] End time in ISO 8601 format “2019-03-20T14:09:50Z”. @param fields [Array<String>] Selected fields from ClimaCell data layer (such as “precipitation” or “wind_gust”).

@return [Hash]

# File lib/weatherb/api.rb, line 125
def nowcast(lat:, lon:, unit_system: 'si', timestep: 1, start_time: 'now', end_time: nil, fields: DEFAULT_FIELDS | DEFAULT_NOWCAST_FIELDS)
  path = 'weather/nowcast'

  query = {
    apikey: @api_key,
    lat: lat,
    lon: lon,
    unit_system: unit_system,
    timestep: timestep,
    start_time: start_time,
    end_time: end_time,
    fields: fields
  }

  response = @connection.get(path, query)
  response_body(response)
end
realtime(lat:, lon:, unit_system: 'si', fields: DEFAULT_FIELDS | DEFAULT_REALTIME_FIELDS) click to toggle source

Realtime (<= 1min out) The realtime call provides observational data at the present time, down to the minute, for a specific location. Information is available globally, with high-resolution data available for Japan Western Europe, India, and the US.

@param lat [Float] Latitude, -59.9 to 59.9. @param lon [Float] Longitude, -180 to 180. @param unit_system [String] Unit system, “si” or “us”. @param fields [Array<String>] Selected fields from ClimaCell data layer (such as “precipitation” or “wind_gust”).

@return [Hash]

# File lib/weatherb/api.rb, line 93
def realtime(lat:, lon:, unit_system: 'si', fields: DEFAULT_FIELDS | DEFAULT_REALTIME_FIELDS)
  path = 'weather/realtime'

  query = {
    apikey: @api_key,
    lat: lat,
    lon: lon,
    unit_system: unit_system,
    fields: fields
  }

  response = @connection.get(path, query)
  response_body(response)
end

Private Instance Methods

response_body(response) click to toggle source
# File lib/weatherb/api.rb, line 209
def response_body(response)
  if SUCCESS_STATUSES.member?(response.status)
    response.body
  else
    { 'status' => response.status, 'body' => response.body }
  end
end