Added Fetcher
This commit is contained in:
parent
b823cffe69
commit
40f1687972
10 changed files with 36 additions and 19 deletions
|
|
@ -1,6 +1,7 @@
|
||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"gitlab.com/flygrounder/go-mtg-vk/internal/cardsinfo"
|
||||||
"log"
|
"log"
|
||||||
"math/rand"
|
"math/rand"
|
||||||
"os"
|
"os"
|
||||||
|
|
@ -28,6 +29,7 @@ func main() {
|
||||||
ConfirmationString: os.Getenv("VK_CONFIRMATION_STRING"),
|
ConfirmationString: os.Getenv("VK_CONFIRMATION_STRING"),
|
||||||
DictPath: "./assets/additional_cards.json",
|
DictPath: "./assets/additional_cards.json",
|
||||||
Cache: caching.GetClient(),
|
Cache: caching.GetClient(),
|
||||||
|
InfoFetcher: &cardsinfo.Fetcher{},
|
||||||
}
|
}
|
||||||
|
|
||||||
r.POST("callback/message", handler.HandleMessage)
|
r.POST("callback/message", handler.HandleMessage)
|
||||||
|
|
|
||||||
|
|
@ -10,6 +10,14 @@ import (
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
func TestGetClient(t *testing.T) {
|
||||||
|
c := GetClient()
|
||||||
|
assert.Equal(t, CacheExpiration, c.Expiration)
|
||||||
|
assert.Equal(t, 0, c.Storage.Options().DB)
|
||||||
|
assert.Equal(t, HostName, c.Storage.Options().Addr)
|
||||||
|
assert.Equal(t, Password, c.Storage.Options().Password)
|
||||||
|
}
|
||||||
|
|
||||||
func TestGetSet(t *testing.T) {
|
func TestGetSet(t *testing.T) {
|
||||||
client, s := getTestClient()
|
client, s := getTestClient()
|
||||||
defer s.Close()
|
defer s.Close()
|
||||||
|
|
|
||||||
|
|
@ -10,13 +10,8 @@ type CacheClient struct {
|
||||||
Expiration time.Duration
|
Expiration time.Duration
|
||||||
}
|
}
|
||||||
|
|
||||||
var client *CacheClient
|
|
||||||
|
|
||||||
func GetClient() *CacheClient {
|
func GetClient() *CacheClient {
|
||||||
if client != nil {
|
client := new(CacheClient)
|
||||||
return client
|
|
||||||
}
|
|
||||||
client = new(CacheClient)
|
|
||||||
client.Init()
|
client.Init()
|
||||||
return client
|
return client
|
||||||
}
|
}
|
||||||
|
|
|
||||||
3
internal/cardsinfo/fetcher.go
Normal file
3
internal/cardsinfo/fetcher.go
Normal file
|
|
@ -0,0 +1,3 @@
|
||||||
|
package cardsinfo
|
||||||
|
|
||||||
|
type Fetcher struct{}
|
||||||
|
|
@ -4,7 +4,7 @@ import (
|
||||||
"fmt"
|
"fmt"
|
||||||
)
|
)
|
||||||
|
|
||||||
func FormatCardPrices(name string, prices []CardPrice) string {
|
func (f *Fetcher) FormatCardPrices(name string, prices []CardPrice) string {
|
||||||
message := fmt.Sprintf("Оригинальное название: %v\n", name)
|
message := fmt.Sprintf("Оригинальное название: %v\n", name)
|
||||||
message += fmt.Sprintf("Результатов: %v\n", len(prices))
|
message += fmt.Sprintf("Результатов: %v\n", len(prices))
|
||||||
for i, v := range prices {
|
for i, v := range prices {
|
||||||
|
|
|
||||||
|
|
@ -6,7 +6,8 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestFormatCardPrices(t *testing.T) {
|
func TestFormatCardPrices(t *testing.T) {
|
||||||
formatted := FormatCardPrices("card", []CardPrice{
|
f := &Fetcher{}
|
||||||
|
formatted := f.FormatCardPrices("card", []CardPrice{
|
||||||
&ScgCardPrice{
|
&ScgCardPrice{
|
||||||
Price: "1.5$",
|
Price: "1.5$",
|
||||||
Edition: "ED",
|
Edition: "ED",
|
||||||
|
|
|
||||||
|
|
@ -12,7 +12,7 @@ import (
|
||||||
|
|
||||||
const ScryfallUrl = "https://api.scryfall.com"
|
const ScryfallUrl = "https://api.scryfall.com"
|
||||||
|
|
||||||
func GetNameByCardId(set string, number string) string {
|
func (f *Fetcher) GetNameByCardId(set string, number string) string {
|
||||||
/*
|
/*
|
||||||
Note: number is string because some cards contain letters in their numbers.
|
Note: number is string because some cards contain letters in their numbers.
|
||||||
*/
|
*/
|
||||||
|
|
@ -20,7 +20,7 @@ func GetNameByCardId(set string, number string) string {
|
||||||
return GetCardByUrl(path)
|
return GetCardByUrl(path)
|
||||||
}
|
}
|
||||||
|
|
||||||
func GetOriginalName(name string, dict io.Reader) string {
|
func (f *Fetcher) GetOriginalName(name string, dict io.Reader) 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 == "" && dict != nil {
|
||||||
|
|
|
||||||
|
|
@ -15,7 +15,8 @@ func TestGetNameByCardId(t *testing.T) {
|
||||||
gock.New(ScryfallUrl + "/set/1").Reply(http.StatusOK).JSON(Card{
|
gock.New(ScryfallUrl + "/set/1").Reply(http.StatusOK).JSON(Card{
|
||||||
Name: "card",
|
Name: "card",
|
||||||
})
|
})
|
||||||
name := GetNameByCardId("set", "1")
|
f := &Fetcher{}
|
||||||
|
name := f.GetNameByCardId("set", "1")
|
||||||
assert.Equal(t, "card", name)
|
assert.Equal(t, "card", name)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -25,7 +26,8 @@ func TestGetOriginalName_Scryfall(t *testing.T) {
|
||||||
gock.New(ScryfallUrl + "/cards/named?fuzzy=card").Reply(http.StatusOK).JSON(Card{
|
gock.New(ScryfallUrl + "/cards/named?fuzzy=card").Reply(http.StatusOK).JSON(Card{
|
||||||
Name: "Result Card",
|
Name: "Result Card",
|
||||||
})
|
})
|
||||||
name := GetOriginalName("card", nil)
|
f := &Fetcher{}
|
||||||
|
name := f.GetOriginalName("card", nil)
|
||||||
assert.Equal(t, "Result Card", name)
|
assert.Equal(t, "Result Card", name)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -37,7 +39,8 @@ func TestGetOriginalName_Dict(t *testing.T) {
|
||||||
"card": "Card",
|
"card": "Card",
|
||||||
})
|
})
|
||||||
dict := strings.NewReader(string(serialized))
|
dict := strings.NewReader(string(serialized))
|
||||||
name := GetOriginalName("card", dict)
|
f := &Fetcher{}
|
||||||
|
name := f.GetOriginalName("card", dict)
|
||||||
assert.Equal(t, "Card", name)
|
assert.Equal(t, "Card", name)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -45,7 +48,8 @@ func TestGetOriginalName_BadJson(t *testing.T) {
|
||||||
defer gock.Off()
|
defer gock.Off()
|
||||||
|
|
||||||
gock.New(ScryfallUrl + "/cards/named?fuzzy=card").Reply(http.StatusOK).BodyString("}")
|
gock.New(ScryfallUrl + "/cards/named?fuzzy=card").Reply(http.StatusOK).BodyString("}")
|
||||||
name := GetOriginalName("card", nil)
|
f := &Fetcher{}
|
||||||
|
name := f.GetOriginalName("card", nil)
|
||||||
assert.Equal(t, "", name)
|
assert.Equal(t, "", name)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -56,6 +60,7 @@ func TestGetOriginalName_DoubleSide(t *testing.T) {
|
||||||
Name: "Legion's Landing // Adanto, the First Fort",
|
Name: "Legion's Landing // Adanto, the First Fort",
|
||||||
Layout: "transform",
|
Layout: "transform",
|
||||||
})
|
})
|
||||||
name := GetOriginalName("card", nil)
|
f := &Fetcher{}
|
||||||
|
name := f.GetOriginalName("card", nil)
|
||||||
assert.Equal(t, "Legion's Landing | Adanto, the First Fort", name)
|
assert.Equal(t, "Legion's Landing | Adanto, the First Fort", name)
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -10,7 +10,7 @@ import (
|
||||||
const scgDomain = "https://starcitygames.com"
|
const scgDomain = "https://starcitygames.com"
|
||||||
const scgSearchUrlTemplate = "https://starcitygames.hawksearch.com/sites/starcitygames/?search_query="
|
const scgSearchUrlTemplate = "https://starcitygames.hawksearch.com/sites/starcitygames/?search_query="
|
||||||
|
|
||||||
func GetPrices(name string) ([]CardPrice, error) {
|
func (f *Fetcher) GetPrices(name string) ([]CardPrice, error) {
|
||||||
prices, err := GetPricesScg(name)
|
prices, err := GetPricesScg(name)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
|
|
|
||||||
|
|
@ -14,7 +14,8 @@ func TestGetPrices_Ok(t *testing.T) {
|
||||||
|
|
||||||
file, _ := os.Open("test_data/AcademyRuinsTest.html")
|
file, _ := os.Open("test_data/AcademyRuinsTest.html")
|
||||||
gock.New(scgSearchUrlTemplate + "card").Reply(http.StatusOK).Body(file)
|
gock.New(scgSearchUrlTemplate + "card").Reply(http.StatusOK).Body(file)
|
||||||
prices, err := GetPrices("card")
|
f := &Fetcher{}
|
||||||
|
prices, err := f.GetPrices("card")
|
||||||
assert.Nil(t, err)
|
assert.Nil(t, err)
|
||||||
assert.Equal(t, []CardPrice{
|
assert.Equal(t, []CardPrice{
|
||||||
&ScgCardPrice{
|
&ScgCardPrice{
|
||||||
|
|
@ -49,7 +50,8 @@ func TestGetPrices_Unavailable(t *testing.T) {
|
||||||
defer gock.Off()
|
defer gock.Off()
|
||||||
|
|
||||||
gock.New(scgSearchUrlTemplate + "card").Reply(http.StatusBadGateway)
|
gock.New(scgSearchUrlTemplate + "card").Reply(http.StatusBadGateway)
|
||||||
_, err := GetPrices("card")
|
f := &Fetcher{}
|
||||||
|
_, err := f.GetPrices("card")
|
||||||
assert.NotNil(t, err)
|
assert.NotNil(t, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -58,7 +60,8 @@ func TestGetPrices_Empty(t *testing.T) {
|
||||||
|
|
||||||
file, _ := os.Open("test_data/EmptyTest.html")
|
file, _ := os.Open("test_data/EmptyTest.html")
|
||||||
gock.New(scgSearchUrlTemplate + "card").Reply(http.StatusOK).Body(file)
|
gock.New(scgSearchUrlTemplate + "card").Reply(http.StatusOK).Body(file)
|
||||||
prices, err := GetPrices("card")
|
f := &Fetcher{}
|
||||||
|
prices, err := f.GetPrices("card")
|
||||||
assert.Nil(t, err)
|
assert.Nil(t, err)
|
||||||
assert.Nil(t, prices)
|
assert.Nil(t, prices)
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue