Skip to content

Commit

Permalink
Update openweathermap.org.go (#166)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
ErikThiart committed Jun 4, 2023
1 parent 3498aaa commit 00f2d2c
Showing 1 changed file with 36 additions and 18 deletions.
54 changes: 36 additions & 18 deletions backends/openweathermap.org.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {

Check failure on line 268 in backends/openweathermap.org.go

View workflow job for this annotation

GitHub Actions / build

undefined: openweather

Check failure on line 268 in backends/openweathermap.org.go

View workflow job for this annotation

GitHub Actions / build

undefined: iface.CurrentWeather
return c.parseCond(weather)
}

func (c *openWeatherConfig) parseForecast(list []openweather.Weather, numDays int) []iface.DailyForecast {

Check failure on line 272 in backends/openweathermap.org.go

View workflow job for this annotation

GitHub Actions / build

undefined: openweather

Check failure on line 272 in backends/openweathermap.org.go

View workflow job for this annotation

GitHub Actions / build

undefined: iface.DailyForecast
return c.parseDaily(list, numDays)
}

func init() {
Expand Down

0 comments on commit 00f2d2c

Please sign in to comment.