Browse Source

Name wikipedia query struct, message formatting, fix tests

pull/159/head
Richard Lewis 7 years ago
parent
commit
34c4874acd
  1. 11
      src/github.com/matrix-org/go-neb/services/wikipedia/wikipedia.go
  2. 25
      src/github.com/matrix-org/go-neb/services/wikipedia/wikipedia_test.go

11
src/github.com/matrix-org/go-neb/services/wikipedia/wikipedia.go

@ -7,7 +7,6 @@ import (
"io/ioutil"
"net/http"
"net/url"
"strconv"
"strings"
log "github.com/Sirupsen/logrus"
@ -23,9 +22,11 @@ const maxExtractLength = 1024 // Max length of extract string in bytes
var httpClient = &http.Client{}
type wikipediaSearchResults struct {
Query struct {
Pages map[string]wikipediaPage `json:"pages"`
} `json:"query"`
Query wikipediaQuery `json:"query"`
}
type wikipediaQuery struct {
Pages map[string]wikipediaPage `json:"pages"`
}
type wikipediaPage struct {
@ -98,7 +99,7 @@ func (s *Service) cmdWikipediaSearch(client *gomatrix.Client, roomID, userID str
}
// Add a link to the bottom of the extract
extractText += "\n" + "http://en.wikipedia.org/?curid=" + strconv.FormatInt(searchResultPage.PageID, 10)
extractText += fmt.Sprintf("\nhttp://en.wikipedia.org/?curid=%d", searchResultPage.PageID)
return gomatrix.TextMessage{
MsgType: "m.notice",

25
src/github.com/matrix-org/go-neb/services/wikipedia/wikipedia_test.go

@ -46,16 +46,21 @@ func TestCommand(t *testing.T) {
t.Fatalf("Bad search string: got \"%s\" (%d characters) ", searchString, searchStringLength)
}
resImage := wikipediaImage{
Width: 64,
Height: 64,
page := wikipediaPage{
PageID: 1,
NS: 1,
Title: "Test page",
Touched: "2017-02-21 00:00:00",
LastRevID: 1,
Extract: "Some extract text",
}
res := wikipediaSearchResult{
Title: "A Cat",
Link: "http://cat.com/cat.jpg",
Mime: "image/jpeg",
Image: resImage,
pages := map[string]wikipediaPage{
"1": page,
}
res := wikipediaSearchResults{
Query: wikipediaQuery{
Pages: pages,
},
}
b, err := json.Marshal(res)
@ -100,7 +105,7 @@ func TestCommand(t *testing.T) {
// Execute the matrix !command
cmds := wikipedia.Commands(matrixCli)
if len(cmds) != 3 {
if len(cmds) != 1 {
t.Fatalf("Unexpected number of commands: %d", len(cmds))
}
// cmd := cmds[0]

Loading…
Cancel
Save