Class: Weatherb::API

Inherits:
Object
  • Object
show all
Defined in:
lib/weatherb/api.rb

Overview

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

Constant Summary collapse

API_URL =
'https://api.climacell.co/v3'
DEFAULT_FIELDS =
%w[
  temp
  feels_like
  humidity
  wind_speed
  wind_direction
  baro_pressure
  precipitation
  sunrise
  sunset
  visibility
  weather_code
].freeze
DEFAULT_REALTIME_FIELDS =
%w[
  dewpoint
  wind_gust
  precipitation_type
  cloud_cover
  cloud_base
  cloud_ceiling
  surface_shortwave_radiation
  moon_phase
].freeze
DEFAULT_NOWCAST_FIELDS =
%w[
  dewpoint
  wind_gust
  precipitation_type
  cloud_cover
  cloud_base
  cloud_ceiling
  surface_shortwave_radiation
].freeze
DEFAULT_HOURLY_FIELDS =
%w[
  dewpoint
  wind_gust
  precipitation_type
  precipitation_probability
  cloud_cover
  cloud_base
  cloud_ceiling
  surface_shortwave_radiation
  moon_phase
].freeze
DEFAULT_DAILY_FIELDS =
%w[
  precipitation_probability
  precipitation_accumulation
].freeze
SUCCESS_STATUSES =
(200..300).freeze

Instance Method Summary collapse

Constructor Details

#initialize(api_key) ⇒ API

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

Parameters:

  • ClimaCell API key.



69
70
71
72
73
74
75
76
77
78
79
# 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

Instance Method Details

#daily(lat:, lon:, unit_system: 'si', start_time: 'now', end_time: nil, fields: DEFAULT_FIELDS | DEFAULT_DAILY_FIELDS) ⇒ Hash

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.

Parameters:

  • Latitude, -87 to 89.

  • Longitude, -180 to 180.

  • (defaults to: 'si')

    Unit system, “si” or “us”.

  • (defaults to: 'now')

    Start time in ISO 8601 format “2019-03-20T14:09:50Z”, or “now”.

  • (defaults to: nil)

    End time in ISO 8601 format “2019-03-20T14:09:50Z”.

  • (defaults to: DEFAULT_FIELDS | DEFAULT_DAILY_FIELDS)

    Selected fields from ClimaCell data layer (such as “precipitation” or “wind_gust”).

Returns:



190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
# 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) ⇒ Hash

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

Parameters:

  • Latitude, -87 to 89.

  • Longitude, -180 to 180.

  • (defaults to: 'si')

    Unit system, “si” or “us”.

  • (defaults to: 'now')

    Start time in ISO 8601 format “2019-03-20T14:09:50Z”, or “now”.

  • (defaults to: nil)

    End time in ISO 8601 format “2019-03-20T14:09:50Z”.

  • (defaults to: DEFAULT_FIELDS | DEFAULT_HOURLY_FIELDS)

    Selected fields from ClimaCell data layer (such as “precipitation” or “wind_gust”).

Returns:



155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
# 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) ⇒ Hash

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.

Parameters:

  • Latitude, -87 to 89.

  • Longitude, -180 to 180.

  • (defaults to: 'si')

    Unit system, “si” or “us”.

  • (defaults to: 1)

    Time step in minutes, 1-60.

  • (defaults to: 'now')

    Start time in ISO 8601 format “2019-03-20T14:09:50Z”, or “now”.

  • (defaults to: nil)

    End time in ISO 8601 format “2019-03-20T14:09:50Z”.

  • (defaults to: DEFAULT_FIELDS | DEFAULT_NOWCAST_FIELDS)

    Selected fields from ClimaCell data layer (such as “precipitation” or “wind_gust”).

Returns:



125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
# 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) ⇒ Hash

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.

Parameters:

  • Latitude, -59.9 to 59.9.

  • Longitude, -180 to 180.

  • (defaults to: 'si')

    Unit system, “si” or “us”.

  • (defaults to: DEFAULT_FIELDS | DEFAULT_REALTIME_FIELDS)

    Selected fields from ClimaCell data layer (such as “precipitation” or “wind_gust”).

Returns:



93
94
95
96
97
98
99
100
101
102
103
104
105
106
# 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