Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

make Reaper accessible in Bot #89

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 9 additions & 9 deletions reddit/account.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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,
Expand All @@ -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,
Expand All @@ -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",
Expand All @@ -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",
Expand All @@ -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",
Expand All @@ -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",
Expand Down
3 changes: 3 additions & 0 deletions reddit/bot.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -52,6 +54,7 @@ func NewBot(c BotConfig) (Bot, error) {
Account: newAccount(r),
Lurker: newLurker(r),
Scanner: newScanner(r),
Reaper: r,
}, err
}

Expand Down
6 changes: 3 additions & 3 deletions reddit/lurker.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"},
)
Expand Down
8 changes: 5 additions & 3 deletions reddit/mockreaper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
36 changes: 19 additions & 17 deletions reddit/reaper.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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,
Expand All @@ -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",
Expand All @@ -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",
Expand All @@ -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{
Expand All @@ -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()

Expand Down
10 changes: 5 additions & 5 deletions reddit/reaper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down Expand Up @@ -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)
}

Expand All @@ -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{},
Expand Down
1 change: 1 addition & 0 deletions reddit/reddit_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
10 changes: 5 additions & 5 deletions reddit/scanner.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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)
}
return s.r.Reap(path, reaperParams)
}