-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
121 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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, | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) | ||
}) | ||
} | ||
} |