-
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.
Merge branch 'feature/qod' into develop
- Loading branch information
Showing
4 changed files
with
119 additions
and
1 deletion.
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
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,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, | ||
) | ||
} |
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,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) | ||
}) | ||
} | ||
} |