Skip to content

Commit

Permalink
Merge pull request #13 from Pigmice2733/nit
Browse files Browse the repository at this point in the history
Fix some nitpicky stuff
  • Loading branch information
fharding1 committed Sep 25, 2018
2 parents 67f93d7 + d86dc88 commit b920d1d
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 14 deletions.
1 change: 1 addition & 0 deletions cmd/peregrine/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ func main() {
}

server := server.New(tba, store, c.Server.Address, c.Server.Origin, year)

if err := server.Run(); err != nil {
fmt.Printf("Error: server.Run: %v\n", err)
}
Expand Down
5 changes: 3 additions & 2 deletions internal/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,11 @@ func New(tba tba.Service, store store.Service, address string, origin string, ye

// Run starts the server, and returns if it runs into an error
func (s *Server) Run() error {
err := s.updateEvents()
if err != nil {
s.logger.Printf("Fetching seed events")
if err := s.updateEvents(); err != nil {
s.logger.Printf("Error: updating event data on Run: %v\n", err)
}

s.logger.Printf("Listening at: %s\n", s.address)
return http.ListenAndServe(s.address, s.handler)
}
21 changes: 12 additions & 9 deletions internal/tba/tba.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,13 +59,15 @@ func (s *Service) makeRequest(path string) (*http.Response, error) {
return tbaClient.Do(req)
}

func webcastURL(webcastType string, channel string) (string, store.WebcastType) {
if webcastType == string(store.Twitch) {
return fmt.Sprintf("https://www.twitch.tv/%s", channel), store.Twitch
} else if string(store.Youtube) == webcastType {
return fmt.Sprintf("https://www.youtube.com/watch?v=%s", channel), store.Youtube
func webcastURL(webcastType store.WebcastType, channel string) (string, error) {
switch webcastType {
case store.Twitch:
return fmt.Sprintf("https://www.twitch.tv/%s", channel), nil
case store.Youtube:
return fmt.Sprintf("https://www.youtube.com/watch?v=%s", channel), nil
}
return "", ""

return "", fmt.Errorf("got invalid webcast url")
}

// GetEvents retreives all events from the given year (e.g. 2018).
Expand Down Expand Up @@ -109,9 +111,10 @@ func (s *Service) GetEvents(year int) ([]store.Event, error) {

var webcasts []store.Webcast
for _, webcast := range tbaEvent.Webcasts {
url, webcastType := webcastURL(webcast.Type, webcast.Channel)
if url != "" {
webcasts = append(webcasts, store.Webcast{Type: webcastType, URL: url})
wt := store.WebcastType(webcast.Type)
url, err := webcastURL(wt, webcast.Channel)
if err == nil {
webcasts = append(webcasts, store.Webcast{Type: wt, URL: url})
}
}

Expand Down
6 changes: 3 additions & 3 deletions internal/tba/tba_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ func newInt(a int) *int {
return &a
}

func strPointer(s string) *string {
func newString(s string) *string {
return &s
}

Expand Down Expand Up @@ -180,7 +180,7 @@ func TestGetEvents(t *testing.T) {
events: []store.Event{{
ID: "key2",
Name: "Event",
District: strPointer("ABC"),
District: newString("ABC"),
Week: newInt(5),
StartDate: store.NewUnix(time.Date(2018, 5, 6, 0, 0, 0, 0, time.UTC)),
EndDate: store.NewUnix(time.Date(2018, 5, 7, 0, 0, 0, 0, time.UTC)),
Expand All @@ -199,7 +199,7 @@ func TestGetEvents(t *testing.T) {
}, {
ID: "key3",
Name: "PIGMICE_IS_BEST",
District: strPointer("PNW"),
District: newString("PNW"),
Week: newInt(2),
StartDate: store.NewUnix(time.Date(2018, 11, 19, 8, 0, 0, 0, time.UTC)),
EndDate: store.NewUnix(time.Date(2018, 11, 23, 8, 0, 0, 0, time.UTC)),
Expand Down

0 comments on commit b920d1d

Please sign in to comment.