Added Fetcher

This commit is contained in:
Artyom Belousov 2021-02-07 10:33:16 +03:00
parent b823cffe69
commit 40f1687972
10 changed files with 36 additions and 19 deletions

View file

@ -0,0 +1,3 @@
package cardsinfo
type Fetcher struct{}

View file

@ -4,7 +4,7 @@ import (
"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", len(prices))
for i, v := range prices {

View file

@ -6,7 +6,8 @@ import (
)
func TestFormatCardPrices(t *testing.T) {
formatted := FormatCardPrices("card", []CardPrice{
f := &Fetcher{}
formatted := f.FormatCardPrices("card", []CardPrice{
&ScgCardPrice{
Price: "1.5$",
Edition: "ED",

View file

@ -12,7 +12,7 @@ import (
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.
*/
@ -20,7 +20,7 @@ func GetNameByCardId(set string, number string) string {
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)
result := GetCardByUrl(path)
if result == "" && dict != nil {

View file

@ -15,7 +15,8 @@ func TestGetNameByCardId(t *testing.T) {
gock.New(ScryfallUrl + "/set/1").Reply(http.StatusOK).JSON(Card{
Name: "card",
})
name := GetNameByCardId("set", "1")
f := &Fetcher{}
name := f.GetNameByCardId("set", "1")
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{
Name: "Result Card",
})
name := GetOriginalName("card", nil)
f := &Fetcher{}
name := f.GetOriginalName("card", nil)
assert.Equal(t, "Result Card", name)
}
@ -37,7 +39,8 @@ func TestGetOriginalName_Dict(t *testing.T) {
"card": "Card",
})
dict := strings.NewReader(string(serialized))
name := GetOriginalName("card", dict)
f := &Fetcher{}
name := f.GetOriginalName("card", dict)
assert.Equal(t, "Card", name)
}
@ -45,7 +48,8 @@ func TestGetOriginalName_BadJson(t *testing.T) {
defer gock.Off()
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)
}
@ -56,6 +60,7 @@ func TestGetOriginalName_DoubleSide(t *testing.T) {
Name: "Legion's Landing // Adanto, the First Fort",
Layout: "transform",
})
name := GetOriginalName("card", nil)
f := &Fetcher{}
name := f.GetOriginalName("card", nil)
assert.Equal(t, "Legion's Landing | Adanto, the First Fort", name)
}

View file

@ -10,7 +10,7 @@ import (
const scgDomain = "https://starcitygames.com"
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)
if err != nil {
return nil, err

View file

@ -14,7 +14,8 @@ func TestGetPrices_Ok(t *testing.T) {
file, _ := os.Open("test_data/AcademyRuinsTest.html")
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.Equal(t, []CardPrice{
&ScgCardPrice{
@ -49,7 +50,8 @@ func TestGetPrices_Unavailable(t *testing.T) {
defer gock.Off()
gock.New(scgSearchUrlTemplate + "card").Reply(http.StatusBadGateway)
_, err := GetPrices("card")
f := &Fetcher{}
_, err := f.GetPrices("card")
assert.NotNil(t, err)
}
@ -58,7 +60,8 @@ func TestGetPrices_Empty(t *testing.T) {
file, _ := os.Open("test_data/EmptyTest.html")
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, prices)
}