Browse Source

Name wikipedia query struct, message formatting, fix tests

pull/159/head
Richard Lewis 8 years ago
parent
commit
34c4874acd
  1. 9
      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

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

@ -7,7 +7,6 @@ import (
"io/ioutil" "io/ioutil"
"net/http" "net/http"
"net/url" "net/url"
"strconv"
"strings" "strings"
log "github.com/Sirupsen/logrus" log "github.com/Sirupsen/logrus"
@ -23,9 +22,11 @@ const maxExtractLength = 1024 // Max length of extract string in bytes
var httpClient = &http.Client{} var httpClient = &http.Client{}
type wikipediaSearchResults struct { type wikipediaSearchResults struct {
Query struct {
Query wikipediaQuery `json:"query"`
}
type wikipediaQuery struct {
Pages map[string]wikipediaPage `json:"pages"` Pages map[string]wikipediaPage `json:"pages"`
} `json:"query"`
} }
type wikipediaPage struct { 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 // 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{ return gomatrix.TextMessage{
MsgType: "m.notice", 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) 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) b, err := json.Marshal(res)
@ -100,7 +105,7 @@ func TestCommand(t *testing.T) {
// Execute the matrix !command // Execute the matrix !command
cmds := wikipedia.Commands(matrixCli) cmds := wikipedia.Commands(matrixCli)
if len(cmds) != 3 {
if len(cmds) != 1 {
t.Fatalf("Unexpected number of commands: %d", len(cmds)) t.Fatalf("Unexpected number of commands: %d", len(cmds))
} }
// cmd := cmds[0] // cmd := cmds[0]

Loading…
Cancel
Save