Class: WeatherGovApi::Client
- Inherits:
-
Object
- Object
- WeatherGovApi::Client
- Defined in:
- lib/weather_gov_api/client.rb
Overview
Client for interacting with the Weather.gov API Handles requests to fetch weather data for US coordinates
Constant Summary collapse
- BASE_URL =
"https://api.weather.gov"
Instance Method Summary collapse
-
#current_weather(latitude:, longitude:) ⇒ Object
rubocop:enable Metrics/MethodLength.
- #forecast(latitude:, longitude:) ⇒ Object
-
#initialize(user_agent: nil) ⇒ Client
constructor
A new instance of Client.
-
#observation_stations(latitude:, longitude:) ⇒ Object
rubocop:disable Metrics/MethodLength.
- #points(latitude:, longitude:) ⇒ Object
Constructor Details
#initialize(user_agent: nil) ⇒ Client
Returns a new instance of Client.
9 10 11 |
# File 'lib/weather_gov_api/client.rb', line 9 def initialize(user_agent: nil) @user_agent = user_agent || "WeatherGovApi Ruby Gem (#{WeatherGovApi::VERSION})" end |
Instance Method Details
#current_weather(latitude:, longitude:) ⇒ Object
rubocop:enable Metrics/MethodLength
39 40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/weather_gov_api/client.rb', line 39 def current_weather(latitude:, longitude:) stations_response = observation_stations(latitude: latitude, longitude: longitude) station = stations_response.data.dig("features", 0) raise WeatherGovApi::ApiError.new(message: "No observation stations found") unless station station_id = station.dig("properties", "stationIdentifier") response = connection.get("/stations/#{station_id}/observations/latest") raise_api_error(response) unless response.success? Response.new(response) rescue Faraday::Error => e raise WeatherGovApi::ApiError.new(message: "API request failed: #{e.message}") end |
#forecast(latitude:, longitude:) ⇒ Object
52 53 54 55 56 57 58 59 60 61 62 63 |
# File 'lib/weather_gov_api/client.rb', line 52 def forecast(latitude:, longitude:) grid_data = points(latitude: latitude, longitude: longitude).data["properties"] grid_id = grid_data["gridId"] grid_x = grid_data["gridX"] grid_y = grid_data["gridY"] response = connection.get("/gridpoints/#{grid_id}/#{grid_x},#{grid_y}/forecast") raise_api_error(response) unless response.success? Response.new(response) rescue Faraday::Error => e raise WeatherGovApi::ApiError.new(message: "API request failed: #{e.message}") end |
#observation_stations(latitude:, longitude:) ⇒ Object
rubocop:disable Metrics/MethodLength
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
# File 'lib/weather_gov_api/client.rb', line 22 def observation_stations(latitude:, longitude:) points_response = points(latitude: latitude, longitude: longitude) stations_url = points_response.data.dig("properties", "observationStations") unless stations_url raise WeatherGovApi::ApiError.new(message: "No observation stations URL found in points response") end stations_path = observation_stations_path(stations_url) response = connection.get(stations_path) raise_api_error(response) unless response.success? Response.new(response) rescue Faraday::Error => e raise WeatherGovApi::ApiError.new(message: "API request failed: #{e.message}") end |
#points(latitude:, longitude:) ⇒ Object
13 14 15 16 17 18 19 |
# File 'lib/weather_gov_api/client.rb', line 13 def points(latitude:, longitude:) response = connection.get("/points/#{latitude},#{longitude}") raise_api_error(response) unless response.success? Response.new(response) rescue Faraday::Error => e raise WeatherGovApi::ApiError.new(message: "API request failed: #{e.message}") end |