Skip to content

Commit

Permalink
Merge branch 'feature/qod' into develop
Browse files Browse the repository at this point in the history
  • Loading branch information
surskitt committed Feb 24, 2020
2 parents 0749e94 + 2136709 commit 06baf5c
Show file tree
Hide file tree
Showing 4 changed files with 119 additions and 1 deletion.
4 changes: 3 additions & 1 deletion .github/workflows/buildx.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ on:

push:
branches: master

tag:
tags:
- '*'

Expand Down Expand Up @@ -71,7 +73,7 @@ jobs:
needs: test

runs-on: ubuntu-latest
if: github.event_name == 'push'
if: github.event_name == 'tag'

steps:
- name: Checkout
Expand Down
1 change: 1 addition & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
_ "github.com/shanedabes/ircbot/plugins/clock"
_ "github.com/shanedabes/ircbot/plugins/donger"
_ "github.com/shanedabes/ircbot/plugins/lastfm"
_ "github.com/shanedabes/ircbot/plugins/qod"
_ "github.com/shanedabes/ircbot/plugins/qwantz"
_ "github.com/shanedabes/ircbot/plugins/tenor"
_ "github.com/shanedabes/ircbot/plugins/trakt"
Expand Down
63 changes: 63 additions & 0 deletions plugins/qod/qod.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package qod

import (
"fmt"

"github.com/go-chat-bot/bot"
"github.com/go-chat-bot/plugins/web"
)

const (
quotesURL = "http://quotes.rest/qod.json"
)

// JSON represents the return from quotes.rest
type JSON struct {
Contents Contents `json:"contents"`
}

func (j JSON) String() string {
return j.Contents.String()
}

// Contents represents the list of quotes
type Contents struct {
Quotes []Quote `json:"quotes"`
}

func (c Contents) String() string {
if len(c.Quotes) < 1 {
return "No quotes found"
}
return c.Quotes[0].String()
}

// Quote represents a single quote
type Quote struct {
Quote string `json:"quote"`
Author string `json:"author"`
}

func (q Quote) String() string {
return fmt.Sprintf("%s\n- %s", q.Quote, q.Author)
}

func qod(command *bot.Cmd) (msg string, err error) {
j := &JSON{}
err = web.GetJSON(quotesURL, j)

if err != nil {
return "", err
}

return j.String(), nil
}

func init() {
bot.RegisterCommand(
"qod",
"Post today's quote from quotes.rest",
"",
qod,
)
}
52 changes: 52 additions & 0 deletions plugins/qod/qod_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package qod

import (
"fmt"
"testing"

"github.com/stretchr/testify/assert"
)

var (
j = JSON{
Contents: c,
}

c = Contents{
Quotes: []Quote{q},
}

q = Quote{
Quote: "test quote",
Author: "lorii",
}

s = "test quote\n- lorii"
)

func TestString(t *testing.T) {
cases := []struct {
name string
obj fmt.Stringer
expected string
}{
{
name: "JSON",
obj: j,
},
{
name: "Contents",
obj: c,
},
{
name: "Quote",
obj: q,
},
}

for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
assert.Equal(t, tc.obj.String(), s)
})
}
}

0 comments on commit 06baf5c

Please sign in to comment.