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