diff --git a/aoc.go b/aoc.go index 38acead..14032e2 100644 --- a/aoc.go +++ b/aoc.go @@ -2,9 +2,8 @@ package aoc import ( "encoding/json" - "errors" "fmt" - "io/ioutil" + "io" "net/http" "strconv" "time" @@ -18,10 +17,10 @@ type AoC struct { func NewAoC(boardId, session string) (*AoC, error) { if boardId == "" { - return nil, errors.New("Board ID is required") + return nil, new(BoardIdRequiredError) } if session == "" { - return nil, errors.New("Session key is required") + return nil, new(SessionRequiredError) } return &AoC{ session: session, @@ -30,19 +29,29 @@ func NewAoC(boardId, session string) (*AoC, error) { }, nil } +func (a *AoC) SetCachedLeaderboard(l *Leaderboard) error { + yr, err := strconv.Atoi(l.Event) + if err != nil { + return new(InvalidYearError) + } + + a.boards[yr] = l + return nil +} + func (a *AoC) GetCachedLeaderboard(year int) (*Leaderboard, error) { if year < 2015 || year > time.Now().Year() { - return nil, errors.New("Invalid Year") + return nil, new(InvalidYearError) } if board, ok := a.boards[year]; ok { return board, nil } - return nil, errors.New(fmt.Sprintf("Leaderboard (%d) is not cached", year)) + return nil, LeaderboardNotCachedError{year: year} } func (a *AoC) GetLeaderboard(year int) (*Leaderboard, error) { if year < 2015 || year > time.Now().Year() { - return nil, errors.New("Invalid Year") + return nil, new(InvalidYearError) } if board, ok := a.boards[year]; ok { if time.Since(board.LastFetch) < (time.Minute * 15) { @@ -53,7 +62,7 @@ func (a *AoC) GetLeaderboard(year int) (*Leaderboard, error) { a.boards[year], err = a.fetchLeaderboard(year) if err != nil { if err.Error() == "invalid character '<' looking for beginning of value" { - return nil, errors.New("Invalid Session Cookie") + return nil, new(SessionError) } return nil, err } @@ -77,17 +86,15 @@ func (a *AoC) fetchLeaderboard(year int) (*Leaderboard, error) { return nil, err } defer resp.Body.Close() - body, err = ioutil.ReadAll(resp.Body) + body, err = io.ReadAll(resp.Body) if err != nil { return nil, err } strBody := string(body) - //strBody = strings.ReplaceAll(strBody, "\"last_star_ts\":\"0\"", "\"last_star_ts\":0") + // strBody = strings.ReplaceAll(strBody, "\"last_star_ts\":\"0\"", "\"last_star_ts\":0") err = json.Unmarshal([]byte(strBody), &leaderboard) if err != nil { - fmt.Println("Error parsing board") - fmt.Println(err) - return nil, err + return nil, fmt.Errorf("error parsing board: %w", err) } for k, mbr := range leaderboard.Members { starTs, err := strconv.ParseInt(mbr.RawStarTs, 10, 64) @@ -144,7 +151,6 @@ func (a ByStarsThenScore) Less(i, j int) bool { } func (m *Member) UnmarshalJSON(data []byte) error { - var v map[string]interface{} var err error diff --git a/aoc_errors.go b/aoc_errors.go new file mode 100644 index 0000000..b9cbd6c --- /dev/null +++ b/aoc_errors.go @@ -0,0 +1,35 @@ +package aoc + +import "fmt" + +type SessionRequiredError struct{} + +func (e SessionRequiredError) Error() string { + return "session key is required" +} + +type SessionError struct{} + +func (e SessionError) Error() string { + return "invalid session" +} + +type BoardIdRequiredError struct{} + +func (e BoardIdRequiredError) Error() string { + return "board id is required" +} + +type InvalidYearError struct{} + +func (e InvalidYearError) Error() string { + return "invalid year" +} + +type LeaderboardNotCachedError struct { + year int +} + +func (e LeaderboardNotCachedError) Error() string { + return fmt.Sprintf("leaderboard (%d) is not cached", e.year) +}