diff --git a/reddit/account.go b/reddit/account.go index 5aa16f8..10b9f1c 100644 --- a/reddit/account.go +++ b/reddit/account.go @@ -28,19 +28,19 @@ type Account interface { type account struct { // r is used to execute requests to Reddit. - r reaper + r Reaper } // newAccount returns a new Account using the given reaper to make requests // to Reddit. -func newAccount(r reaper) Account { +func newAccount(r Reaper) Account { return &account{ r: r, } } func (a *account) Reply(parentName, text string) error { - return a.r.sow( + return a.r.Sow( "/api/comment", map[string]string{ "thing_id": parentName, "text": text, @@ -49,7 +49,7 @@ func (a *account) Reply(parentName, text string) error { } func (a *account) GetReply(parentName, text string) (Submission, error) { - return a.r.get_sow( + return a.r.GetSow( "/api/comment", map[string]string{ "thing_id": parentName, "text": text, @@ -58,7 +58,7 @@ func (a *account) GetReply(parentName, text string) (Submission, error) { } func (a *account) SendMessage(user, subject, text string) error { - return a.r.sow( + return a.r.Sow( "/api/compose", map[string]string{ "to": user, "subject": subject, @@ -68,7 +68,7 @@ func (a *account) SendMessage(user, subject, text string) error { } func (a *account) PostSelf(subreddit, title, text string) error { - return a.r.sow( + return a.r.Sow( "/api/submit", map[string]string{ "sr": subreddit, "kind": "self", @@ -79,7 +79,7 @@ func (a *account) PostSelf(subreddit, title, text string) error { } func (a *account) GetPostSelf(subreddit, title, text string) (Submission, error) { - return a.r.get_sow( + return a.r.GetSow( "/api/submit", map[string]string{ "sr": subreddit, "kind": "self", @@ -90,7 +90,7 @@ func (a *account) GetPostSelf(subreddit, title, text string) (Submission, error) } func (a *account) PostLink(subreddit, title, url string) error { - return a.r.sow( + return a.r.Sow( "/api/submit", map[string]string{ "sr": subreddit, "kind": "link", @@ -101,7 +101,7 @@ func (a *account) PostLink(subreddit, title, url string) error { } func (a *account) GetPostLink(subreddit, title, url string) (Submission, error) { - return a.r.get_sow( + return a.r.GetSow( "/api/submit", map[string]string{ "sr": subreddit, "kind": "link", diff --git a/reddit/bot.go b/reddit/bot.go index 717e183..9068d31 100644 --- a/reddit/bot.go +++ b/reddit/bot.go @@ -28,12 +28,14 @@ type Bot interface { Account Lurker Scanner + Reaper } type bot struct { Account Lurker Scanner + Reaper } // NewBot returns a logged in handle to the Reddit API. @@ -52,6 +54,7 @@ func NewBot(c BotConfig) (Bot, error) { Account: newAccount(r), Lurker: newLurker(r), Scanner: newScanner(r), + Reaper: r, }, err } diff --git a/reddit/lurker.go b/reddit/lurker.go index 949c155..0ca19bb 100644 --- a/reddit/lurker.go +++ b/reddit/lurker.go @@ -7,15 +7,15 @@ type Lurker interface { } type lurker struct { - r reaper + r Reaper } -func newLurker(r reaper) Lurker { +func newLurker(r Reaper) Lurker { return &lurker{r: r} } func (s *lurker) Thread(permalink string) (*Post, error) { - harvest, err := s.r.reap( + harvest, err := s.r.Reap( permalink+".json", map[string]string{"raw_json": "1"}, ) diff --git a/reddit/mockreaper_test.go b/reddit/mockreaper_test.go index b8a641f..2d756b6 100644 --- a/reddit/mockreaper_test.go +++ b/reddit/mockreaper_test.go @@ -10,21 +10,23 @@ type mockReaper struct { err error } -func (m *mockReaper) reap(path string, _ map[string]string) (Harvest, error) { +func (m *mockReaper) Reap(path string, _ map[string]string) (Harvest, error) { m.path = path return m.h, m.err } -func (m *mockReaper) sow(path string, _ map[string]string) error { +func (m *mockReaper) Sow(path string, _ map[string]string) error { m.path = path return m.err } -func (m *mockReaper) get_sow(path string, _ map[string]string) (Submission, error) { +func (m *mockReaper) GetSow(path string, _ map[string]string) (Submission, error) { m.path = path return m.s, m.err } +func (m *mockReaper) RateBlock() {} + func reaperWhich(h Harvest, err error) *mockReaper { return &mockReaper{ h: h, diff --git a/reddit/reaper.go b/reddit/reaper.go index a6ae18b..88bdee1 100644 --- a/reddit/reaper.go +++ b/reddit/reaper.go @@ -28,16 +28,18 @@ type reaperConfig struct { rate time.Duration } -// reaper is a high level api for Reddit HTTP requests. -type reaper interface { - // reap executes a GET request to Reddit and returns the elements from +// Reaper is a high level api for Reddit HTTP requests. +type Reaper interface { + // Reap executes a GET request to Reddit and returns the elements from // the endpoint. - reap(path string, values map[string]string) (Harvest, error) - // sow executes a POST request to Reddit. - sow(path string, values map[string]string) error - // get_sow executes a POST request to Reddit - // and returns the response, usually the posted item - get_sow(path string, values map[string]string) (Submission, error) + Reap(path string, values map[string]string) (Harvest, error) + // Sow executes a POST request to Reddit. + Sow(path string, values map[string]string) error + // GetSow executes a POST request to Reddit + // and returns the response, usually the posted item. + GetSow(path string, values map[string]string) (Submission, error) + // RateBlock consumes an API call, if none are available RateBlock blocks. + RateBlock() } type reaperImpl struct { @@ -51,7 +53,7 @@ type reaperImpl struct { mu *sync.Mutex } -func newReaper(c reaperConfig) reaper { +func newReaper(c reaperConfig) Reaper { return &reaperImpl{ cli: c.client, parser: c.parser, @@ -63,8 +65,8 @@ func newReaper(c reaperConfig) reaper { } } -func (r *reaperImpl) reap(path string, values map[string]string) (Harvest, error) { - r.rateBlock() +func (r *reaperImpl) Reap(path string, values map[string]string) (Harvest, error) { + r.RateBlock() resp, err := r.cli.Do( &http.Request{ Method: "GET", @@ -85,8 +87,8 @@ func (r *reaperImpl) reap(path string, values map[string]string) (Harvest, error }, err } -func (r *reaperImpl) sow(path string, values map[string]string) error { - r.rateBlock() +func (r *reaperImpl) Sow(path string, values map[string]string) error { + r.RateBlock() _, err := r.cli.Do( &http.Request{ Method: "POST", @@ -99,8 +101,8 @@ func (r *reaperImpl) sow(path string, values map[string]string) error { return err } -func (r *reaperImpl) get_sow(path string, values map[string]string) (Submission, error) { - r.rateBlock() +func (r *reaperImpl) GetSow(path string, values map[string]string) (Submission, error) { + r.RateBlock() values["api_type"] = "json" resp, err := r.cli.Do( &http.Request{ @@ -118,7 +120,7 @@ func (r *reaperImpl) get_sow(path string, values map[string]string) (Submission, return r.parser.parse_submitted(resp) } -func (r *reaperImpl) rateBlock() { +func (r *reaperImpl) RateBlock() { r.mu.Lock() defer r.mu.Unlock() diff --git a/reddit/reaper_test.go b/reddit/reaper_test.go index 829a09e..5e7c5c2 100644 --- a/reddit/reaper_test.go +++ b/reddit/reaper_test.go @@ -95,7 +95,7 @@ func TestReap(t *testing.T) { mu: &sync.Mutex{}, } - Harvest, err := r.reap(test.path, test.values) + Harvest, err := r.Reap(test.path, test.values) if err != nil { t.Errorf("Error reaping input %d: %v", i, err) } @@ -159,7 +159,7 @@ func TestSow(t *testing.T) { mu: &sync.Mutex{}, } - if err := r.sow(test.path, test.values); err != nil { + if err := r.Sow(test.path, test.values); err != nil { t.Errorf("Error reaping input %d: %v", i, err) } @@ -170,14 +170,14 @@ func TestSow(t *testing.T) { } func TestRateBlockReap(t *testing.T) { - testRateBlock(func(r reaper) { r.reap("", nil) }, t) + testRateBlock(func(r Reaper) { r.Reap("", nil) }, t) } func TestRateBlockSow(t *testing.T) { - testRateBlock(func(r reaper) { r.sow("", nil) }, t) + testRateBlock(func(r Reaper) { r.Sow("", nil) }, t) } -func testRateBlock(f func(reaper), t *testing.T) { +func testRateBlock(f func(Reaper), t *testing.T) { start := time.Now() r := &reaperImpl{ cli: &mockClient{}, diff --git a/reddit/reddit_test.go b/reddit/reddit_test.go index d8e451d..8ca5009 100644 --- a/reddit/reddit_test.go +++ b/reddit/reddit_test.go @@ -211,6 +211,7 @@ func testRequests(cases []testCase, t *testing.T) { Account: newAccount(r), Lurker: newLurker(r), Scanner: newScanner(r), + Reaper: r, } for _, test := range cases { if err := test.f(b); err != test.err { diff --git a/reddit/scanner.go b/reddit/scanner.go index 181261d..a5c4531 100644 --- a/reddit/scanner.go +++ b/reddit/scanner.go @@ -31,15 +31,15 @@ type Scanner interface { } type scanner struct { - r reaper + r Reaper } -func newScanner(r reaper) Scanner { +func newScanner(r Reaper) Scanner { return &scanner{r: r} } func (s *scanner) Listing(path, after string) (Harvest, error) { - return s.r.reap( + return s.r.Reap( path, map[string]string{ "raw_json": "1", "limit": "100", @@ -59,5 +59,5 @@ func (s *scanner) ListingWithParams(path string, params map[string]string) ( for key, value := range params { reaperParams[key] = value } - return s.r.reap(path, reaperParams) -} \ No newline at end of file + return s.r.Reap(path, reaperParams) +}