Add separate sort options for convenience

Signed-off-by: Vartan Benohanian <vartanbeno@gmail.com>
This commit is contained in:
Vartan Benohanian 2020-07-28 11:01:50 -04:00
parent ff682a6e70
commit 2eb02e7df6
4 changed files with 53 additions and 52 deletions

View file

@ -55,11 +55,45 @@ func SetLimit(v int) SearchOptionSetter {
}
}
// SetSort sets the sort option.
func SetSort(v Sort) SearchOptionSetter {
return func(opts url.Values) {
opts.Set("sort", v.String())
}
// SortByHot sets the sort option to return the hottest results first.
func SortByHot(opts url.Values) {
opts.Set("sort", "hot")
}
// SortByBest sets the sort option to return the best results first.
func SortByBest(opts url.Values) {
opts.Set("sort", "best")
}
// SortByNew sets the sort option to return the newest results first.
func SortByNew(opts url.Values) {
opts.Set("sort", "new")
}
// SortByRising sets the sort option to return the rising results first.
func SortByRising(opts url.Values) {
opts.Set("sort", "rising")
}
// SortByControversial sets the sort option to return the most controversial results first.
func SortByControversial(opts url.Values) {
opts.Set("sort", "controversial")
}
// SortByTop sets the sort option to return the top results first.
func SortByTop(opts url.Values) {
opts.Set("sort", "top")
}
// SortByRelevance sets the sort option to return the most relevant results first.
func SortByRelevance(opts url.Values) {
opts.Set("sort", "relevance")
}
// SortByNumberOfComments sets the sort option to return the results with the highest
// number of comments first.
func SortByNumberOfComments(opts url.Values) {
opts.Set("sort", "comments")
}
// FromThePastHour sets the timespan option to return results from the past hour.

View file

@ -51,10 +51,10 @@ type Moderator struct {
Permissions []string `json:"mod_permissions"`
}
func (s *SubredditService) getPosts(ctx context.Context, sort Sort, subreddits []string, opts ...SearchOptionSetter) (*Posts, *Response, error) {
path := sort.String()
func (s *SubredditService) getPosts(ctx context.Context, sort string, subreddits []string, opts ...SearchOptionSetter) (*Posts, *Response, error) {
path := sort
if len(subreddits) > 0 {
path = fmt.Sprintf("r/%s/%s", strings.Join(subreddits, "+"), sort.String())
path = fmt.Sprintf("r/%s/%s", strings.Join(subreddits, "+"), sort)
}
form := newSearchOptions(opts...)
@ -79,31 +79,31 @@ func (s *SubredditService) getPosts(ctx context.Context, sort Sort, subreddits [
// Note: when looking for hot posts in a subreddit, it will include the stickied
// posts (if any) PLUS posts from the limit parameter (25 by default).
func (s *SubredditService) Hot(ctx context.Context, subreddits []string, opts ...SearchOptionSetter) (*Posts, *Response, error) {
return s.getPosts(ctx, SortHot, subreddits, opts...)
return s.getPosts(ctx, "hot", subreddits, opts...)
}
// New returns the newest posts from the specified subreddits.
// If none are defined, it returns the ones from your subscribed subreddits.
func (s *SubredditService) New(ctx context.Context, subreddits []string, opts ...SearchOptionSetter) (*Posts, *Response, error) {
return s.getPosts(ctx, SortNew, subreddits, opts...)
return s.getPosts(ctx, "new", subreddits, opts...)
}
// Rising returns the rising posts from the specified subreddits.
// If none are defined, it returns the ones from your subscribed subreddits.
func (s *SubredditService) Rising(ctx context.Context, subreddits []string, opts ...SearchOptionSetter) (*Posts, *Response, error) {
return s.getPosts(ctx, SortRising, subreddits, opts...)
return s.getPosts(ctx, "rising", subreddits, opts...)
}
// Controversial returns the most controversial posts from the specified subreddits.
// If none are defined, it returns the ones from your subscribed subreddits.
func (s *SubredditService) Controversial(ctx context.Context, subreddits []string, opts ...SearchOptionSetter) (*Posts, *Response, error) {
return s.getPosts(ctx, SortControversial, subreddits, opts...)
return s.getPosts(ctx, "controversial", subreddits, opts...)
}
// Top returns the top posts from the specified subreddits.
// If none are defined, it returns the ones from your subscribed subreddits.
func (s *SubredditService) Top(ctx context.Context, subreddits []string, opts ...SearchOptionSetter) (*Posts, *Response, error) {
return s.getPosts(ctx, SortTop, subreddits, opts...)
return s.getPosts(ctx, "top", subreddits, opts...)
}
// Get gets a subreddit by name.

View file

@ -22,39 +22,6 @@ const (
kindModAction = "modaction"
)
// Sort is a sorting option.
type Sort int
var sorts = [...]string{
"hot",
"best",
"new",
"rising",
"controversial",
"top",
"relevance",
"comments",
}
// Different sorting options.
const (
SortHot Sort = iota
SortBest
SortNew
SortRising
SortControversial
SortTop
SortRelevance
SortComments
)
func (s Sort) String() string {
if s < SortHot || s > SortComments {
return ""
}
return sorts[s]
}
// Permalink is the link to a post or comment.
type Permalink string

View file

@ -259,7 +259,7 @@ func TestUserService_Overview_Options(t *testing.T) {
form := url.Values{}
form.Set("limit", "5")
form.Set("after", "t3_after")
form.Set("sort", SortTop.String())
form.Set("sort", "top")
err := r.ParseForm()
assert.NoError(t, err)
@ -268,7 +268,7 @@ func TestUserService_Overview_Options(t *testing.T) {
fmt.Fprint(w, blob)
})
_, _, _, err = client.User.Overview(ctx, SetLimit(5), SetAfter("t3_after"), SetSort(SortTop))
_, _, _, err = client.User.Overview(ctx, SetLimit(5), SetAfter("t3_after"), SortByTop)
assert.NoError(t, err)
}
@ -326,7 +326,7 @@ func TestUserService_Posts_Options(t *testing.T) {
form := url.Values{}
form.Set("limit", "10")
form.Set("sort", SortNew.String())
form.Set("sort", "new")
err := r.ParseForm()
assert.NoError(t, err)
@ -335,7 +335,7 @@ func TestUserService_Posts_Options(t *testing.T) {
fmt.Fprint(w, blob)
})
_, _, err = client.User.Posts(ctx, SetLimit(10), SetSort(SortNew))
_, _, err = client.User.Posts(ctx, SetLimit(10), SortByNew)
assert.NoError(t, err)
}
@ -446,7 +446,7 @@ func TestUserService_Saved_Options(t *testing.T) {
form := url.Values{}
form.Set("limit", "50")
form.Set("sort", SortControversial.String())
form.Set("sort", "controversial")
err := r.ParseForm()
assert.NoError(t, err)
@ -455,7 +455,7 @@ func TestUserService_Saved_Options(t *testing.T) {
fmt.Fprint(w, blob)
})
_, _, _, err = client.User.Saved(ctx, SetLimit(50), SetSort(SortControversial))
_, _, _, err = client.User.Saved(ctx, SetLimit(50), SortByControversial)
assert.NoError(t, err)
}
func TestUserService_Upvoted(t *testing.T) {