Class: NoaaWeatherClient::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/noaa_weather_client/client.rb

Overview

Provides entry point for interacting with the noaa api via multiple services.

Instance Method Summary collapse

Instance Method Details

#current_observations(lat, lon, options = {}) ⇒ Responses::CurrentObservation

Note:

It is important to cache a copy of the available stations for use here, as the xml stations response is quite large noaa does not appreciate repeated calls.

Retrieves the current weather observations for a location.

Parameters:

  • lat (Float)

    latitude

  • lon (Float)

    longitude

  • options (Hash) (defaults to: {})

Options Hash (options):

  • stations (Responses::Stations)

    A cached stations response to prevent having to query noaa for the list.

Returns:



36
37
38
39
# File 'lib/noaa_weather_client/client.rb', line 36

def current_observations(lat, lon, options = {})
  station = options.fetch(:station) { nearest_weather_station(lat, lon, options) }
  Services::CurrentObservations.new(options).fetch(station)
end

#forecast_by_day(lat, lon, options = {}) ⇒ Responses::Forecast

Fetches a daily forecast for a location from today. Default is 7 days. The forecast for the current day ceases to be returned after 6:00pm at the observation area.

Parameters:

  • lat (Float)

    latitude

  • lon (Float)

    longitude

Returns:



16
17
18
# File 'lib/noaa_weather_client/client.rb', line 16

def forecast_by_day(lat, lon, options = {})
  Services::ForecastByDay.new.fetch(lat, lon, options)
end

#nearest_weather_station(lat, lon, options = {}) ⇒ Responses::Station

Note:

It is important to cache a copy of the available stations for use here, as the xml stations response is quite large noaa does not appreciate repeated calls.

Retrieves the weather station nearest to the location.

Parameters:

  • lat (Float)

    latitude

  • lon (Float)

    longitude

  • options (Hash) (defaults to: {})

Options Hash (options):

  • stations (Responses::Stations)

    A cached stations response to prevent having to query noaa for the list.

Returns:



48
49
50
51
# File 'lib/noaa_weather_client/client.rb', line 48

def nearest_weather_station(lat, lon, options = {})
  stations = options.fetch(:stations) { weather_stations }
  Services::FindNearestStation.find(lat, lon, stations)
end

#postal_code_to_coordinate(postal_code, options = {}) ⇒ Responses::LatLonList

Converts a zip code to a latitude and longitude.

Parameters:

  • lat (Float)

    latitude

  • lon (Float)

    longitude

Returns:



57
58
59
# File 'lib/noaa_weather_client/client.rb', line 57

def postal_code_to_coordinate(postal_code, options = {})
  Services::PostalCodeToCoordinate.new(options).resolve(postal_code)
end

#weather_stations(options = {}) ⇒ Responses::Stations

Retrieves a list of weather stations from noaa.

Returns:



22
23
24
25
26
27
# File 'lib/noaa_weather_client/client.rb', line 22

def weather_stations(options = {})
  if options.delete(:reload) { false } || @weather_stations.nil?
    @weather_stations = Services::WeatherStations.new(options).fetch
  end
  @weather_stations
end