From ad931ba0c025947e20ef2efd62f03c94cc30be99 Mon Sep 17 00:00:00 2001 From: shanedabes Date: Mon, 24 Feb 2020 17:31:16 +0000 Subject: [PATCH 1/2] add new jokes plugin --- main.go | 1 + plugins/jod/jod.go | 62 +++++++++++++++++++++++++++++++++++++++++ plugins/jod/jod_test.go | 58 ++++++++++++++++++++++++++++++++++++++ 3 files changed, 121 insertions(+) create mode 100644 plugins/jod/jod.go create mode 100644 plugins/jod/jod_test.go diff --git a/main.go b/main.go index c1a7622..75f8d64 100644 --- a/main.go +++ b/main.go @@ -11,6 +11,7 @@ import ( _ "github.com/shanedabes/ircbot/plugins/clock" _ "github.com/shanedabes/ircbot/plugins/donger" _ "github.com/shanedabes/ircbot/plugins/fact" + _ "github.com/shanedabes/ircbot/plugins/jod" _ "github.com/shanedabes/ircbot/plugins/lastfm" _ "github.com/shanedabes/ircbot/plugins/qod" _ "github.com/shanedabes/ircbot/plugins/qwantz" diff --git a/plugins/jod/jod.go b/plugins/jod/jod.go new file mode 100644 index 0000000..07bf87b --- /dev/null +++ b/plugins/jod/jod.go @@ -0,0 +1,62 @@ +package jod + +import ( + "github.com/go-chat-bot/bot" + "github.com/go-chat-bot/plugins/web" +) + +const ( + url = "https://api.jokes.one/jod" +) + +type json struct { + Contents contents `json:"contents"` +} + +func (j json) String() string { + return j.Contents.String() +} + +type contents struct { + Jokes []jokes `json:"jokes"` +} + +func (c contents) String() string { + return c.Jokes[0].String() +} + +type jokes struct { + Joke joke `json:"joke"` +} + +func (js jokes) String() string { + return js.Joke.String() +} + +type joke struct { + Text string `json:"text"` +} + +func (j joke) String() string { + return j.Text +} + +func jod(command *bot.Cmd) (msg string, err error) { + j := &json{} + err = web.GetJSON(url, j) + + if err != nil { + return "", err + } + + return j.String(), nil +} + +func init() { + bot.RegisterCommand( + "jod", + "Post the joke of the day from api.jokes.one", + "", + jod, + ) +} diff --git a/plugins/jod/jod_test.go b/plugins/jod/jod_test.go new file mode 100644 index 0000000..641b35e --- /dev/null +++ b/plugins/jod/jod_test.go @@ -0,0 +1,58 @@ +package jod + +import ( + "fmt" + "testing" + + "github.com/stretchr/testify/assert" +) + +var ( + jsn = json{ + Contents: c, + } + + c = contents{ + Jokes: []jokes{js}, + } + + js = jokes{ + Joke: j, + } + + j = joke{ + Text: "joke", + } + + s = "joke" +) + +func TestString(t *testing.T) { + cases := []struct { + name string + obj fmt.Stringer + }{ + { + name: "json", + obj: jsn, + }, + { + name: "contents", + obj: c, + }, + { + name: "jokes", + obj: js, + }, + { + name: "joke", + obj: j, + }, + } + + for _, tc := range cases { + t.Run(tc.name, func(t *testing.T) { + assert.Equal(t, tc.obj.String(), s) + }) + } +} From 889f1ca66d97994f8f1b0bf720fa84e8646987c2 Mon Sep 17 00:00:00 2001 From: shanedabes Date: Mon, 24 Feb 2020 17:31:50 +0000 Subject: [PATCH 2/2] prepare 1.4.0 release --- CHANGELOG.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 54cbc6d..74c13b6 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,7 @@ +## 1.4.0 + +- Add jokes plugin + ## 1.3.0 - Add random fact plugin