Extracted dict to Fetcher
This commit is contained in:
parent
1f69282c73
commit
4da8e94bcc
7 changed files with 22 additions and 19 deletions
|
|
@ -19,6 +19,7 @@ func main() {
|
|||
r := gin.Default()
|
||||
|
||||
groupId, _ := strconv.ParseInt(os.Getenv("VK_GROUP_ID"), 10, 64)
|
||||
dict, _ := os.Open("./assets/additional_cards.json")
|
||||
handler := vk.Handler{
|
||||
Sender: &vk.ApiSender{
|
||||
Token: os.Getenv("VK_TOKEN"),
|
||||
|
|
@ -27,9 +28,10 @@ func main() {
|
|||
SecretKey: os.Getenv("VK_SECRET_KEY"),
|
||||
GroupId: groupId,
|
||||
ConfirmationString: os.Getenv("VK_CONFIRMATION_STRING"),
|
||||
DictPath: "./assets/additional_cards.json",
|
||||
Cache: caching.NewClient("redis:6379", "", time.Hour*24, 0),
|
||||
InfoFetcher: &cardsinfo.Fetcher{},
|
||||
InfoFetcher: &cardsinfo.Fetcher{
|
||||
Dict: dict,
|
||||
},
|
||||
}
|
||||
|
||||
r.POST("callback/message", handler.HandleMessage)
|
||||
|
|
|
|||
|
|
@ -1,3 +1,7 @@
|
|||
package cardsinfo
|
||||
|
||||
type Fetcher struct{}
|
||||
import "io"
|
||||
|
||||
type Fetcher struct {
|
||||
Dict io.Reader
|
||||
}
|
||||
|
|
|
|||
|
|
@ -3,7 +3,6 @@ package cardsinfo
|
|||
import (
|
||||
"encoding/json"
|
||||
"gitlab.com/flygrounder/go-mtg-vk/internal/dicttranslate"
|
||||
"io"
|
||||
"io/ioutil"
|
||||
"net/http"
|
||||
"net/url"
|
||||
|
|
@ -20,11 +19,11 @@ func (f *Fetcher) GetNameByCardId(set string, number string) string {
|
|||
return GetCardByUrl(path)
|
||||
}
|
||||
|
||||
func (f *Fetcher) GetOriginalName(name string, dict io.Reader) string {
|
||||
func (f *Fetcher) GetOriginalName(name string) string {
|
||||
path := ScryfallUrl + "/cards/named?fuzzy=" + ApplyFilters(name)
|
||||
result := GetCardByUrl(path)
|
||||
if result == "" && dict != nil {
|
||||
result, _ = dicttranslate.FindFromReader(name, dict, 5)
|
||||
if result == "" && f.Dict != nil {
|
||||
result, _ = dicttranslate.FindFromReader(name, f.Dict, 5)
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
|
|
|||
|
|
@ -27,7 +27,7 @@ func TestGetOriginalName_Scryfall(t *testing.T) {
|
|||
Name: "Result Card",
|
||||
})
|
||||
f := &Fetcher{}
|
||||
name := f.GetOriginalName("card", nil)
|
||||
name := f.GetOriginalName("card")
|
||||
assert.Equal(t, "Result Card", name)
|
||||
}
|
||||
|
||||
|
|
@ -39,8 +39,10 @@ func TestGetOriginalName_Dict(t *testing.T) {
|
|||
"card": "Card",
|
||||
})
|
||||
dict := strings.NewReader(string(serialized))
|
||||
f := &Fetcher{}
|
||||
name := f.GetOriginalName("card", dict)
|
||||
f := &Fetcher{
|
||||
Dict: dict,
|
||||
}
|
||||
name := f.GetOriginalName("card")
|
||||
assert.Equal(t, "Card", name)
|
||||
}
|
||||
|
||||
|
|
@ -49,7 +51,7 @@ func TestGetOriginalName_BadJson(t *testing.T) {
|
|||
|
||||
gock.New(ScryfallUrl + "/cards/named?fuzzy=card").Reply(http.StatusOK).BodyString("}")
|
||||
f := &Fetcher{}
|
||||
name := f.GetOriginalName("card", nil)
|
||||
name := f.GetOriginalName("card")
|
||||
assert.Equal(t, "", name)
|
||||
}
|
||||
|
||||
|
|
@ -61,6 +63,6 @@ func TestGetOriginalName_DoubleSide(t *testing.T) {
|
|||
Layout: "transform",
|
||||
})
|
||||
f := &Fetcher{}
|
||||
name := f.GetOriginalName("card", nil)
|
||||
name := f.GetOriginalName("card")
|
||||
assert.Equal(t, "Legion's Landing | Adanto, the First Fort", name)
|
||||
}
|
||||
|
|
|
|||
|
|
@ -2,10 +2,8 @@ package vk
|
|||
|
||||
import (
|
||||
"errors"
|
||||
"io"
|
||||
"log"
|
||||
"net/http"
|
||||
"os"
|
||||
"strings"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
|
|
@ -27,7 +25,7 @@ type CardInfoFetcher interface {
|
|||
GetPrices(name string) ([]cardsinfo.CardPrice, error)
|
||||
FormatCardPrices(name string, prices []cardsinfo.CardPrice) string
|
||||
GetNameByCardId(set string, number string) string
|
||||
GetOriginalName(name string, dict io.Reader) string
|
||||
GetOriginalName(name string) string
|
||||
}
|
||||
|
||||
type CardCache interface {
|
||||
|
|
@ -119,8 +117,7 @@ func (h *Handler) getCardNameByCommand(command string) (string, error) {
|
|||
number := split[2]
|
||||
name = h.InfoFetcher.GetNameByCardId(set, number)
|
||||
default:
|
||||
dict, _ := os.Open(h.DictPath)
|
||||
name = h.InfoFetcher.GetOriginalName(command, dict)
|
||||
name = h.InfoFetcher.GetOriginalName(command)
|
||||
}
|
||||
return name, nil
|
||||
}
|
||||
|
|
|
|||
|
|
@ -3,7 +3,6 @@ package vk
|
|||
import (
|
||||
"errors"
|
||||
"gitlab.com/flygrounder/go-mtg-vk/internal/cardsinfo"
|
||||
"io"
|
||||
)
|
||||
|
||||
type testInfoFetcher struct{}
|
||||
|
|
@ -23,7 +22,7 @@ func (t *testInfoFetcher) GetNameByCardId(_ string, _ string) string {
|
|||
return "good"
|
||||
}
|
||||
|
||||
func (t *testInfoFetcher) GetOriginalName(name string, _ io.Reader) string {
|
||||
func (t *testInfoFetcher) GetOriginalName(name string) string {
|
||||
if name == "good" || name == "bad" || name == "uncached" {
|
||||
return name
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue