Extracted dict to Fetcher

This commit is contained in:
Artyom Belousov 2021-02-07 15:18:46 +03:00
parent 1f69282c73
commit 4da8e94bcc
7 changed files with 22 additions and 19 deletions

View file

@ -1,3 +1,7 @@
package cardsinfo
type Fetcher struct{}
import "io"
type Fetcher struct {
Dict io.Reader
}

View file

@ -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
}

View file

@ -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)
}

View file

@ -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
}

View file

@ -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
}