location.go 891 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. // This file is subject to a 1-clause BSD license.
  2. // Its contents can be found in the enclosed LICENSE file.
  3. package weather
  4. import (
  5. "fmt"
  6. "net/url"
  7. "github.com/monkeybird/autimaat/irc"
  8. )
  9. type location struct {
  10. City string `json:"city"`
  11. State string `json:"state"`
  12. Country string `json:"country_iso3166"`
  13. }
  14. // newLocation creates a new location from the given command request data.
  15. func newLocation(r *irc.Request) *location {
  16. var l location
  17. fields := r.Fields(1)
  18. l.City = url.QueryEscape(fields[0])
  19. if len(fields) > 1 {
  20. l.Country = url.QueryEscape(fields[1])
  21. }
  22. if len(fields) > 2 {
  23. l.State = url.QueryEscape(fields[2])
  24. }
  25. return &l
  26. }
  27. func (l *location) String() string {
  28. if len(l.Country) == 0 {
  29. return l.City
  30. }
  31. if len(l.State) == 0 {
  32. return fmt.Sprintf("%s/%s", l.Country, l.City)
  33. }
  34. return fmt.Sprintf("%s/%s/%s", l.Country, l.State, l.City)
  35. }