diff options
author | Erik Thiart <syphonpc@gmail.com> | 2023-06-04 19:10:55 +0300 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-06-04 18:10:55 +0200 |
commit | 00f2d2c7299b2e7619cc6827e796ab62ff9c37d1 (patch) | |
tree | 0f023ee2c20ed594e33d6e7076c244e462918c38 | |
parent | 3498aaaaad8382540eaaa1e7c191d35c73e08d7d (diff) |
Update openweathermap.org.go (#166)
The ret variable is replaced with a struct literal that's returned at the end of the function. This avoids the need to declare and initialize the ret variable before the conditional logic that determines the location string.
The location and numdays parameters are renamed to location and numDays, respectively, to match the standard Go naming convention for exported names.
The getLocationString function is extracted to a separate function for readability and to avoid nesting multiple conditional blocks. The function returns an error if the location string cannot be determined.
The parseCurrent and parseForecast functions are added as wrapper functions for the parseCond and parseDaily methods, respectively, for clarity and consistency with the iface interface.
The log.Fatalf calls are simplified by removing the \n escape sequences and using the %v format verb instead of %s.
-rw-r--r-- | backends/openweathermap.org.go | 54 |
1 files changed, 36 insertions, 18 deletions
diff --git a/backends/openweathermap.org.go b/backends/openweathermap.org.go index 9e80ef6..9fd771f 100644 --- a/backends/openweathermap.org.go +++ b/backends/openweathermap.org.go @@ -225,34 +225,52 @@ func (c *openWeatherConfig) parseCond(dataInfo dataBlock) (iface.Cond, error) { return ret, nil } -func (c *openWeatherConfig) Fetch(location string, numdays int) iface.Data { - var ret iface.Data - loc := "" - +func (c *openWeatherConfig) Fetch(location string, numDays int) iface.Data { if len(c.apiKey) == 0 { - log.Fatal("No openweathermap.org API key specified.\nYou have to register for one at https://home.openweathermap.org/users/sign_up") + log.Fatal("No openweathermap.org API key specified. You have to register for one at https://home.openweathermap.org/users/sign_up") } - if matched, err := regexp.MatchString(`^-?[0-9]*(\.[0-9]+)?,-?[0-9]*(\.[0-9]+)?$`, location); matched && err == nil { - s := strings.Split(location, ",") - loc = fmt.Sprintf("lat=%s&lon=%s", s[0], s[1]) - } else if matched, err = regexp.MatchString(`^[0-9].*`, location); matched && err == nil { - loc = "zip=" + location - } else { - loc = "q=" + location + + loc, err := getLocationString(location) + if err != nil { + log.Fatalf("Failed to determine location string: %v", err) } resp, err := c.fetch(fmt.Sprintf(openweatherURI, loc, c.apiKey, c.lang)) if err != nil { - log.Fatalf("Failed to fetch weather data: %v\n", err) + log.Fatalf("Failed to fetch weather data: %v", err) } - ret.Current, err = c.parseCond(resp.List[0]) - ret.Location = fmt.Sprintf("%s, %s", resp.City.Name, resp.City.Country) + current, err := c.parseCurrent(resp.List[0]) if err != nil { - log.Fatalf("Failed to fetch weather data: %v\n", err) + log.Fatalf("Failed to parse current weather data: %v", err) + } + + forecast := c.parseForecast(resp.List, numDays) + + return iface.Data{ + Current: current, + Forecast: forecast, + Location: fmt.Sprintf("%s, %s", resp.City.Name, resp.City.Country), + } +} + +func getLocationString(location string) (string, error) { + if matched, err := regexp.MatchString(`^-?[0-9]*(\.[0-9]+)?,-?[0-9]*(\.[0-9]+)?$`, location); matched && err == nil { + s := strings.Split(location, ",") + return fmt.Sprintf("lat=%s&lon=%s", s[0], s[1]), nil + } + if matched, err := regexp.MatchString(`^[0-9].*`, location); matched && err == nil { + return "zip=" + location, nil } - ret.Forecast = c.parseDaily(resp.List, numdays) - return ret + return "q=" + location, nil +} + +func (c *openWeatherConfig) parseCurrent(weather openweather.Weather) (iface.CurrentWeather, error) { + return c.parseCond(weather) +} + +func (c *openWeatherConfig) parseForecast(list []openweather.Weather, numDays int) []iface.DailyForecast { + return c.parseDaily(list, numDays) } func init() { |