Add separate timespan options for convenience

Signed-off-by: Vartan Benohanian <vartanbeno@gmail.com>
This commit is contained in:
Vartan Benohanian 2020-07-27 22:26:29 -04:00
parent c852306cb2
commit ff682a6e70
3 changed files with 31 additions and 38 deletions

View File

@ -62,11 +62,34 @@ func SetSort(v Sort) SearchOptionSetter {
} }
} }
// SetTimespan sets the timespan option. // FromThePastHour sets the timespan option to return results from the past hour.
func SetTimespan(v Timespan) SearchOptionSetter { func FromThePastHour(opts url.Values) {
return func(opts url.Values) { opts.Set("t", "hour")
opts.Set("timespan", v.String()) }
}
// FromThePastDay sets the timespan option to return results from the past day.
func FromThePastDay(opts url.Values) {
opts.Set("t", "day")
}
// FromThePastWeek sets the timespan option to return results from the past week.
func FromThePastWeek(opts url.Values) {
opts.Set("t", "week")
}
// FromThePastMonth sets the timespan option to return results from the past month.
func FromThePastMonth(opts url.Values) {
opts.Set("t", "month")
}
// FromThePastYear sets the timespan option to return results from the past year.
func FromThePastYear(opts url.Values) {
opts.Set("t", "year")
}
// FromAllTime sets the timespan option to return results from all time.
func FromAllTime(opts url.Values) {
opts.Set("t", "all")
} }
// setType sets the type option. // setType sets the type option.
@ -119,7 +142,7 @@ func (s *SearchService) Posts(ctx context.Context, query string, subreddits []st
} }
// Subreddits searches for subreddits. // Subreddits searches for subreddits.
// The Sort and Timespan options don't affect the results for this search. // The sort and timespan options don't affect the results for this search.
func (s *SearchService) Subreddits(ctx context.Context, query string, opts ...SearchOptionSetter) (*Subreddits, *Response, error) { func (s *SearchService) Subreddits(ctx context.Context, query string, opts ...SearchOptionSetter) (*Subreddits, *Response, error) {
opts = append(opts, setType("sr"), setQuery(query)) opts = append(opts, setType("sr"), setQuery(query))
form := newSearchOptions(opts...) form := newSearchOptions(opts...)
@ -142,7 +165,7 @@ func (s *SearchService) Subreddits(ctx context.Context, query string, opts ...Se
} }
// Users searches for users. // Users searches for users.
// The Sort and Timespan options don't affect the results for this search. // The sort and timespan options don't affect the results for this search.
func (s *SearchService) Users(ctx context.Context, query string, opts ...SearchOptionSetter) (*Users, *Response, error) { func (s *SearchService) Users(ctx context.Context, query string, opts ...SearchOptionSetter) (*Users, *Response, error) {
opts = append(opts, setType("user"), setQuery(query)) opts = append(opts, setType("user"), setQuery(query))
form := newSearchOptions(opts...) form := newSearchOptions(opts...)

View File

@ -128,6 +128,7 @@ func (s *SubredditService) Get(ctx context.Context, name string) (*Subreddit, *R
} }
// GetPopular returns popular subreddits. // GetPopular returns popular subreddits.
// todo: use search opts for this
func (s *SubredditService) GetPopular(ctx context.Context, opts *ListOptions) (*Subreddits, *Response, error) { func (s *SubredditService) GetPopular(ctx context.Context, opts *ListOptions) (*Subreddits, *Response, error) {
return s.getSubreddits(ctx, "subreddits/popular", opts) return s.getSubreddits(ctx, "subreddits/popular", opts)
} }

View File

@ -55,37 +55,6 @@ func (s Sort) String() string {
return sorts[s] return sorts[s]
} }
// Timespan is a timespan option.
// E.g. "hour" means in the last hour, "all" means all-time.
// It is used when conducting searches.
type Timespan int
var timespans = [...]string{
"hour",
"day",
"week",
"month",
"year",
"all",
}
// Different timespan options.
const (
TimespanHour Timespan = iota
TimespanDay
TimespanWeek
TimespanMonth
TimespanYear
TimespanAll
)
func (t Timespan) String() string {
if t < TimespanHour || t > TimespanAll {
return ""
}
return timespans[t]
}
// Permalink is the link to a post or comment. // Permalink is the link to a post or comment.
type Permalink string type Permalink string