module BomObservationsApi
BomObservationsApi — latest weather station observations via BOM public API.
Public Instance Methods
Source
# File lib/bom_observations_api.rb, line 13 def fetch_latest(station_id, fetch:) return nil if station_id.nil? || station_id.to_s.strip.empty? path = "/observations/latest/#{station_id}/atm/surf_air?include_qc_results=false" json = BomApi.fetch_json(path, fetch: fetch) parse_observations(json) rescue StandardError nil end
Source
# File lib/bom_observations_api.rb, line 50 def format_observed_at(datetime_utc, timezone) return "" if datetime_utc.to_s.strip.empty? zone = TZInfo::Timezone.get(timezone) local = zone.to_local(Time.parse(datetime_utc).utc) "at #{local.strftime('%-l:%M%P')}, #{local.strftime('%A %-d %b %Y')}." rescue StandardError "" end
Source
# File lib/bom_observations_api.rb, line 23 def parse_observations(json) obs = json["obs"] return nil unless obs timezone = json.dig("stn", "location", "timezone") || "Australia/Sydney" temp = obs.dig("temp", "dry_bulb_1min_cel") feels = obs.dig("temp", "apparent_1min_cel") humidity = obs.dig("temp", "rel_hum_percent") wind_dir = obs.dig("wind", "dirn_10m_ord") wind_mps = obs.dig("wind", "speed_10m_mps") pressure = obs.dig("pres", "msl_hpa") return nil if temp.nil? wind_kmh = wind_mps ? (wind_mps.to_f * 3.6).round : nil { temperature: BomForecast.normalize_value("#{temp} °C"), observed_at: format_observed_at(obs["datetime_utc"], timezone), humidity: humidity ? "#{humidity.round}%" : nil, feels_like: feels ? BomForecast.normalize_value("#{feels} °C") : nil, pressure: pressure ? "#{pressure} hPa" : nil, wind_direction: wind_dir, wind_speed: wind_kmh ? "#{wind_kmh} km/h" : nil } end