From ff682a6e70bcc6045cf34bfb5161d4f90ca2348e Mon Sep 17 00:00:00 2001 From: Vartan Benohanian Date: Mon, 27 Jul 2020 22:26:29 -0400 Subject: [PATCH] Add separate timespan options for convenience Signed-off-by: Vartan Benohanian --- search.go | 37 ++++++++++++++++++++++++++++++------- subreddit.go | 1 + things.go | 31 ------------------------------- 3 files changed, 31 insertions(+), 38 deletions(-) diff --git a/search.go b/search.go index 3d11870..ac73e90 100644 --- a/search.go +++ b/search.go @@ -62,11 +62,34 @@ func SetSort(v Sort) SearchOptionSetter { } } -// SetTimespan sets the timespan option. -func SetTimespan(v Timespan) SearchOptionSetter { - return func(opts url.Values) { - opts.Set("timespan", v.String()) - } +// FromThePastHour sets the timespan option to return results from the past hour. +func FromThePastHour(opts url.Values) { + opts.Set("t", "hour") +} + +// 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. @@ -119,7 +142,7 @@ func (s *SearchService) Posts(ctx context.Context, query string, subreddits []st } // 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) { opts = append(opts, setType("sr"), setQuery(query)) form := newSearchOptions(opts...) @@ -142,7 +165,7 @@ func (s *SearchService) Subreddits(ctx context.Context, query string, opts ...Se } // 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) { opts = append(opts, setType("user"), setQuery(query)) form := newSearchOptions(opts...) diff --git a/subreddit.go b/subreddit.go index 7264e1a..ba5e3ec 100644 --- a/subreddit.go +++ b/subreddit.go @@ -128,6 +128,7 @@ func (s *SubredditService) Get(ctx context.Context, name string) (*Subreddit, *R } // GetPopular returns popular subreddits. +// todo: use search opts for this func (s *SubredditService) GetPopular(ctx context.Context, opts *ListOptions) (*Subreddits, *Response, error) { return s.getSubreddits(ctx, "subreddits/popular", opts) } diff --git a/things.go b/things.go index 0d34287..b15e8af 100644 --- a/things.go +++ b/things.go @@ -55,37 +55,6 @@ func (s Sort) String() string { 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. type Permalink string