today_ai_weather/app/services/weather_service.rb
songtianlun b5c40f2e13 fix: update Dockerfile and weather service configuration
- Added 'libpq-dev' to the packages installed in the Dockerfile
- Updated the base_uri in WeatherService to use 'dig' for safer access

These changes improve the Docker environment by ensuring that
necessary PostgreSQL development headers are available during
installation. The weather service now safely accesses the
URI from the credentials, reducing the risk of errors when
fetching nested configuration data.
2025-01-22 17:07:04 +08:00

38 lines
993 B
Ruby

# app/services/weather_service.rb
class WeatherService
include HTTParty
base_uri Rails.application.credentials.dig(:qweather, :uri)
def initialize
@api_key = Rails.application.credentials.qweather.token
end
def get_weather(latitude, longitude)
Rails.logger.debug "Get Weather for #{latitude},#{longitude}"
response = self.class.get(
"/weather/now",
headers: {
"X-QW-Api-Key" => "#{@api_key}"
},
query: {
location: "#{longitude},#{latitude}"
})
return nil unless response.success?
data = response["now"]
{
temperature: data["temp"].to_f,
feeling_temp: data["feelsLike"].to_f,
humidity: data["humidity"].to_f,
wind_scale: "#{data['windScale']}",
wind_speed: data["windSpeed"].to_f,
precipitation: data["precip"].to_f,
pressure: data["pressure"].to_f,
visibility: data["vis"].to_f,
cloud: data["cloud"].to_f,
description: data["text"]
}
end
end