Skip to content

Commit

Permalink
Merge pull request #4 from SaintWish/master
Browse files Browse the repository at this point in the history
use Show object for episode funcs
  • Loading branch information
Michael Robinson authored Dec 25, 2019
2 parents ace96d9 + 9dc4b6e commit 2609a8a
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 10 deletions.
33 changes: 27 additions & 6 deletions tvmaze/episode.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,22 +18,22 @@ type Episode struct {
}

// GetEpisodes finds all episodes for the given show
func (c Client) GetEpisodes(s Show) (episodes []Episode, err error) {
func (s Show) GetEpisodes() (episodes []Episode, err error) {
url := baseURLWithPath(fmt.Sprintf("shows/%d/episodes", s.ID))

if _, err = c.get(url, &episodes); err != nil {
if _, err = s.get(url, &episodes); err != nil {
return nil, err
}

return episodes, nil
}

// GetNextEpisode returns the next un-air episode for the show
func (c Client) GetNextEpisode(s Show) (*Episode, error) {
func (s Show) GetNextEpisode() (*Episode, error) {
url := baseURLWithPathQuery(fmt.Sprintf("shows/%d", s.ID), "embed", "nextepisode")

var embed embeddedNextEpisode
if _, err := c.get(url, &embed); err != nil {
if _, err := s.get(url, &embed); err != nil {
return nil, err
}

Expand All @@ -44,19 +44,40 @@ func (c Client) GetNextEpisode(s Show) (*Episode, error) {
}

// GetEpisode returns a specific episode for a show
func (c Client) GetEpisode(s Show, season int, episode int) (*Episode, error) {
func (s Show) GetEpisode(season int, episode int) (*Episode, error) {
url := baseURLWithPathQueries(fmt.Sprintf("shows/%d/episodebynumber", s.ID), map[string]string{
"season": strconv.Itoa(season),
"number": strconv.Itoa(episode),
})

var epOut Episode
if _, err := c.get(url, &epOut); err != nil {
if _, err := s.get(url, &epOut); err != nil {
return nil, err
}
return &epOut, nil
}

/*
Backwards compatibility
*/
// GetEpisodes finds all episodes for the given show
func (c Client) GetEpisodes(s Show) (episodes []Episode, err error) {
fmt.Println("[WARN] (Client).GetEpisodes(Show) is deprecated.")
return s.GetEpisodes()
}

// GetNextEpisode returns the next un-air episode for the show
func (c Client) GetNextEpisode(s Show) (*Episode, error) {
fmt.Println("[WARN] (Client).GetNextEpisode(Show) is deprecated.")
return s.GetNextEpisode()
}

// GetEpisode returns a specific episode for a show
func (c Client) GetEpisode(s Show, season int, episode int) (*Episode, error) {
fmt.Println("[WARN] (Client).GetEpisode(Show) is deprecated.")
return s.GetEpisode(season, episode)
}

type embeddedNextEpisode struct {
Embedded struct {
NextEpisode Episode `json:"nextepisode"`
Expand Down
15 changes: 15 additions & 0 deletions tvmaze/show.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"encoding/json"
"fmt"
"net/http"
"net/url"
"reflect"
"strconv"
"time"
Expand Down Expand Up @@ -39,6 +40,20 @@ type Show struct {
}
}

func (s Show) get(url url.URL, ret interface{}) (status int, err error) {
r, err := http.Get(url.String())
if err != nil {
return 0, errors.Wrapf(err, "failed to get url: %s", url.String())
}

if r.StatusCode >= http.StatusBadRequest {
return r.StatusCode, errors.Errorf("received error status code (%d): %s", r.StatusCode, r.Status)
}

defer r.Body.Close()
return r.StatusCode, json.NewDecoder(r.Body).Decode(&ret)
}

// GetTitle return the show title
func (s Show) GetTitle() string {
return s.Name
Expand Down
9 changes: 5 additions & 4 deletions tvmaze/tvmaze_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,8 @@ func TestTVMaze(t *testing.T) {

t.Run("get episode", func(t *testing.T) {
t.Parallel()
result, err := c.GetEpisode(Show{ID: 315}, 4, 5)
show := Show{ID: 315}
result, err := show.GetEpisode(4, 5)
require.NoError(t, err)
require.NotNil(t, result, "expected a result")
require.NotEmpty(t, result.Name)
Expand Down Expand Up @@ -82,23 +83,23 @@ func TestTVMaze(t *testing.T) {
t.Run("get episodes", func(t *testing.T) {
t.Parallel()
show := Show{ID: 315} // Archer
episodes, err := c.GetEpisodes(show)
episodes, err := show.GetEpisodes()
require.NoError(t, err)
require.NotEmpty(t, episodes, "expected to get episodes")
})

t.Run("get next episode", func(t *testing.T) {
t.Parallel()
show := Show{ID: 1864} // Superstore
episode, err := c.GetNextEpisode(show)
episode, err := show.GetNextEpisode()
require.NoError(t, err)
require.NotNil(t, episode)
})

t.Run("null times", func(t *testing.T) {
t.Parallel()
show := Show{ID: 180} // Firefly
episodes, err := c.GetEpisodes(show)
episodes, err := show.GetEpisodes()
require.NoError(t, err)
require.NotEmpty(t, episodes, "expected to get episodes")
})
Expand Down

0 comments on commit 2609a8a

Please sign in to comment.