Unexported unnecessary members

This commit is contained in:
Artyom Belousov 2021-02-07 15:58:50 +03:00
parent 4da8e94bcc
commit d8a295be75
14 changed files with 134 additions and 115 deletions

View file

@ -5,26 +5,22 @@ import (
"strings" "strings"
) )
type CardPrice interface { type scgCardPrice struct {
Format() string price string
edition string
link string
} }
type ScgCardPrice struct { func (s *scgCardPrice) format() string {
Price string return fmt.Sprintf("%v: %v\n%v\n", s.edition, s.price, s.link)
Edition string
Link string
} }
func (s *ScgCardPrice) Format() string { type card struct {
return fmt.Sprintf("%v: %v\n%v\n", s.Edition, s.Price, s.Link)
}
type Card struct {
Name string `json:"name"` Name string `json:"name"`
Layout string `json:"layout"` Layout string `json:"layout"`
} }
func (c *Card) getName() string { func (c *card) getName() string {
if c.Layout == "transform" { if c.Layout == "transform" {
return strings.Replace(c.Name, "//", "|", 1) return strings.Replace(c.Name, "//", "|", 1)
} }

View file

@ -4,11 +4,19 @@ import (
"fmt" "fmt"
) )
func (f *Fetcher) FormatCardPrices(name string, prices []CardPrice) string { func (f *Fetcher) GetFormattedCardPrices(name string) (string, error) {
prices, err := f.getPrices(name)
if err != nil {
return "", err
}
return f.formatCardPrices(name, prices), nil
}
func (f *Fetcher) formatCardPrices(name string, prices []scgCardPrice) 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 {
message += fmt.Sprintf("%v. %v", i+1, v.Format()) message += fmt.Sprintf("%v. %v", i+1, v.format())
} }
return message return message
} }

View file

@ -2,16 +2,39 @@ package cardsinfo
import ( import (
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
"gopkg.in/h2non/gock.v1"
"net/http"
"os"
"testing" "testing"
) )
func TestFetcher_GetFormattedCardPrices_Error(t *testing.T) {
defer gock.Off()
gock.New(scgSearchUrlTemplate + "card").Reply(http.StatusInternalServerError)
f := &Fetcher{}
_, err := f.GetFormattedCardPrices("card")
assert.NotNil(t, err)
}
func TestFetcher_GetFormattedCardPrices_Empty(t *testing.T) {
defer gock.Off()
resp, _ := os.Open("test_data/EmptyTest.html")
gock.New(scgSearchUrlTemplate + "card").Reply(http.StatusOK).Body(resp)
f := &Fetcher{}
msg, err := f.GetFormattedCardPrices("card")
assert.Nil(t, err)
assert.Equal(t, "Оригинальное название: card\nРезультатов: 0\n", msg)
}
func TestFormatCardPrices(t *testing.T) { func TestFormatCardPrices(t *testing.T) {
f := &Fetcher{} f := &Fetcher{}
formatted := f.FormatCardPrices("card", []CardPrice{ formatted := f.formatCardPrices("card", []scgCardPrice{
&ScgCardPrice{ {
Price: "1.5$", price: "1.5$",
Edition: "ED", edition: "ED",
Link: "scg.com", link: "scg.com",
}, },
}) })
assert.Equal(t, "Оригинальное название: card\nРезультатов: 1\n1. ED: 1.5$\nscg.com\n", formatted) assert.Equal(t, "Оригинальное название: card\nРезультатов: 1\n1. ED: 1.5$\nscg.com\n", formatted)

View file

@ -9,26 +9,26 @@ import (
"strings" "strings"
) )
const ScryfallUrl = "https://api.scryfall.com" const scryfallUrl = "https://api.scryfall.com"
func (f *Fetcher) 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.
*/ */
path := ScryfallUrl + "/cards/" + strings.ToLower(set) + "/" + number path := scryfallUrl + "/cards/" + strings.ToLower(set) + "/" + number
return GetCardByUrl(path) return getCardByUrl(path)
} }
func (f *Fetcher) GetOriginalName(name string) 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 == "" && f.Dict != nil { if result == "" && f.Dict != nil {
result, _ = dicttranslate.FindFromReader(name, f.Dict, 5) result, _ = dicttranslate.FindFromReader(name, f.Dict, 5)
} }
return result return result
} }
func ApplyFilters(name string) string { func applyFilters(name string) string {
/* /*
Despite of the rules of Russian language, letter ё is replaced with e on cards Despite of the rules of Russian language, letter ё is replaced with e on cards
Sometimes it leads to wrong search results Sometimes it leads to wrong search results
@ -37,13 +37,13 @@ func ApplyFilters(name string) string {
return url.QueryEscape(name) return url.QueryEscape(name)
} }
func GetCardByUrl(path string) string { func getCardByUrl(path string) string {
response, _ := http.Get(path) response, _ := http.Get(path)
defer func() { defer func() {
_ = response.Body.Close() _ = response.Body.Close()
}() }()
data, _ := ioutil.ReadAll(response.Body) data, _ := ioutil.ReadAll(response.Body)
var v Card var v card
err := json.Unmarshal(data, &v) err := json.Unmarshal(data, &v)
if err != nil { if err != nil {
return "" return ""

View file

@ -12,7 +12,7 @@ import (
func TestGetNameByCardId(t *testing.T) { func TestGetNameByCardId(t *testing.T) {
defer gock.Off() defer gock.Off()
gock.New(ScryfallUrl + "/set/1").Reply(http.StatusOK).JSON(Card{ gock.New(scryfallUrl + "/set/1").Reply(http.StatusOK).JSON(card{
Name: "card", Name: "card",
}) })
f := &Fetcher{} f := &Fetcher{}
@ -23,7 +23,7 @@ func TestGetNameByCardId(t *testing.T) {
func TestGetOriginalName_Scryfall(t *testing.T) { func TestGetOriginalName_Scryfall(t *testing.T) {
defer gock.Off() defer gock.Off()
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",
}) })
f := &Fetcher{} f := &Fetcher{}
@ -34,7 +34,7 @@ func TestGetOriginalName_Scryfall(t *testing.T) {
func TestGetOriginalName_Dict(t *testing.T) { func TestGetOriginalName_Dict(t *testing.T) {
defer gock.Off() defer gock.Off()
gock.New(ScryfallUrl + "/cards/named?fuzzy=card").Reply(http.StatusOK).JSON(Card{}) gock.New(scryfallUrl + "/cards/named?fuzzy=card").Reply(http.StatusOK).JSON(card{})
serialized, _ := json.Marshal(map[string]string{ serialized, _ := json.Marshal(map[string]string{
"card": "Card", "card": "Card",
}) })
@ -49,7 +49,7 @@ func TestGetOriginalName_Dict(t *testing.T) {
func TestGetOriginalName_BadJson(t *testing.T) { 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("}")
f := &Fetcher{} f := &Fetcher{}
name := f.GetOriginalName("card") name := f.GetOriginalName("card")
assert.Equal(t, "", name) assert.Equal(t, "", name)
@ -58,7 +58,7 @@ func TestGetOriginalName_BadJson(t *testing.T) {
func TestGetOriginalName_DoubleSide(t *testing.T) { func TestGetOriginalName_DoubleSide(t *testing.T) {
defer gock.Off() defer gock.Off()
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: "Legion's Landing // Adanto, the First Fort", Name: "Legion's Landing // Adanto, the First Fort",
Layout: "transform", Layout: "transform",
}) })

View file

@ -10,8 +10,8 @@ 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 (f *Fetcher) GetPrices(name string) ([]CardPrice, error) { func (f *Fetcher) getPrices(name string) ([]scgCardPrice, error) {
prices, err := GetPricesScg(name) prices, err := getPricesScg(name)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -21,7 +21,7 @@ func (f *Fetcher) GetPrices(name string) ([]CardPrice, error) {
return prices, nil return prices, nil
} }
func GetPricesScg(name string) ([]CardPrice, error) { func getPricesScg(name string) ([]scgCardPrice, error) {
escapedName := url.QueryEscape(name) escapedName := url.QueryEscape(name)
searchUrl := scgSearchUrlTemplate + escapedName searchUrl := scgSearchUrlTemplate + escapedName
node, err := htmlquery.LoadURL(searchUrl) node, err := htmlquery.LoadURL(searchUrl)
@ -29,23 +29,23 @@ func GetPricesScg(name string) ([]CardPrice, error) {
return nil, errors.Wrap(err, "cannot load url") return nil, errors.Wrap(err, "cannot load url")
} }
blocks := htmlquery.Find(node, "//div[@class=\"hawk-results-item\"]") blocks := htmlquery.Find(node, "//div[@class=\"hawk-results-item\"]")
var results []CardPrice var results []scgCardPrice
for _, block := range blocks { for _, block := range blocks {
price := &ScgCardPrice{} price := scgCardPrice{}
linkNode := htmlquery.FindOne(block, "//h2/a") linkNode := htmlquery.FindOne(block, "//h2/a")
for _, attr := range linkNode.Attr { for _, attr := range linkNode.Attr {
if attr.Key == "href" { if attr.Key == "href" {
price.Link = scgDomain + attr.Val price.link = scgDomain + attr.Val
break break
} }
} }
editionNode := htmlquery.FindOne(block, "//p[@class=\"hawk-results-item__category\"]/a") editionNode := htmlquery.FindOne(block, "//p[@class=\"hawk-results-item__category\"]/a")
if editionNode.FirstChild != nil { if editionNode.FirstChild != nil {
price.Edition = editionNode.FirstChild.Data price.edition = editionNode.FirstChild.Data
} }
priceNode := htmlquery.FindOne(block, "//span[@class='hawk-old-price']|//div[contains(concat(' ',normalize-space(@class),' '),' hawk-results-item__options-table-cell--price ')]") priceNode := htmlquery.FindOne(block, "//span[@class='hawk-old-price']|//div[contains(concat(' ',normalize-space(@class),' '),' hawk-results-item__options-table-cell--price ')]")
if priceNode.FirstChild != nil { if priceNode.FirstChild != nil {
price.Price = priceNode.FirstChild.Data price.price = priceNode.FirstChild.Data
} }
results = append(results, price) results = append(results, price)
} }

View file

@ -15,33 +15,33 @@ 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)
f := &Fetcher{} f := &Fetcher{}
prices, err := f.GetPrices("card") prices, err := f.getPrices("card")
assert.Nil(t, err) assert.Nil(t, err)
assert.Equal(t, []CardPrice{ assert.Equal(t, []scgCardPrice{
&ScgCardPrice{ {
Price: "$6.99", price: "$6.99",
Edition: "Double Masters", edition: "Double Masters",
Link: "https://starcitygames.com/academy-ruins-sgl-mtg-2xm-309-enn/?sku=SGL-MTG-2XM-309-ENN1", link: "https://starcitygames.com/academy-ruins-sgl-mtg-2xm-309-enn/?sku=SGL-MTG-2XM-309-ENN1",
}, },
&ScgCardPrice{ {
Price: "$9.99", price: "$9.99",
Edition: "Double Masters (Foil)", edition: "Double Masters (Foil)",
Link: "https://starcitygames.com/academy-ruins-sgl-mtg-2xm-309-enf/?sku=SGL-MTG-2XM-309-ENF1", link: "https://starcitygames.com/academy-ruins-sgl-mtg-2xm-309-enf/?sku=SGL-MTG-2XM-309-ENF1",
}, },
&ScgCardPrice{ {
Price: "$11.99", price: "$11.99",
Edition: "Double Masters - Variants", edition: "Double Masters - Variants",
Link: "https://starcitygames.com/academy-ruins-sgl-mtg-2xm2-369-enn/?sku=SGL-MTG-2XM2-369-ENN1", link: "https://starcitygames.com/academy-ruins-sgl-mtg-2xm2-369-enn/?sku=SGL-MTG-2XM2-369-ENN1",
}, },
&ScgCardPrice{ {
Price: "$14.99", price: "$14.99",
Edition: "Double Masters - Variants (Foil)", edition: "Double Masters - Variants (Foil)",
Link: "https://starcitygames.com/academy-ruins-sgl-mtg-2xm2-369-enf/?sku=SGL-MTG-2XM2-369-ENF1", link: "https://starcitygames.com/academy-ruins-sgl-mtg-2xm2-369-enf/?sku=SGL-MTG-2XM2-369-ENF1",
}, },
&ScgCardPrice{ {
Price: "$7.99", price: "$7.99",
Edition: "Modern Masters: 2013 Edition", edition: "Modern Masters: 2013 Edition",
Link: "https://starcitygames.com/academy-ruins-sgl-mtg-mma-219-enn/?sku=SGL-MTG-MMA-219-ENN1", link: "https://starcitygames.com/academy-ruins-sgl-mtg-mma-219-enn/?sku=SGL-MTG-MMA-219-ENN1",
}, },
}, prices) }, prices)
} }
@ -51,7 +51,7 @@ func TestGetPrices_Unavailable(t *testing.T) {
gock.New(scgSearchUrlTemplate + "card").Reply(http.StatusBadGateway) gock.New(scgSearchUrlTemplate + "card").Reply(http.StatusBadGateway)
f := &Fetcher{} f := &Fetcher{}
_, err := f.GetPrices("card") _, err := f.getPrices("card")
assert.NotNil(t, err) assert.NotNil(t, err)
} }
@ -61,7 +61,7 @@ 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)
f := &Fetcher{} f := &Fetcher{}
prices, err := f.GetPrices("card") prices, err := f.getPrices("card")
assert.Nil(t, err) assert.Nil(t, err)
assert.Nil(t, prices) assert.Nil(t, prices)
} }

View file

@ -119,7 +119,7 @@
<div class="hawk-results-item"> <div class="hawk-results-item">
<div class="hawk-results-item__header" > <div class="hawk-results-item__header" >
<div class="hawk-headerDiv"> <div class="hawk-headerDiv">
<h2 class="hawk-results-item__title"><a href="/academy-ruins-sgl-mtg-2xm-309-enn/?sku=SGL-MTG-2XM-309-ENN1" id="ctl00_SearchBody_ProductResults_lvItems_ctrl0_ctl00_item_lnk2" onclick="return HawkSearch.link(event,&#39;41010ff5-0527-4462-8791-3c9dfdc5ab80&#39;,1,&#39;510346&#39;,0);">Academy Ruins</a></h2> <h2 class="hawk-results-item__title"><a href="/academy-ruins-sgl-mtg-2xm-309-enn/?sku=SGL-MTG-2XM-309-ENN1" id="ctl00_SearchBody_ProductResults_lvItems_ctrl0_ctl00_item_lnk2" onclick="return HawkSearch.link(event,&#39;41010ff5-0527-4462-8791-3c9dfdc5ab80&#39;,1,&#39;510346&#39;,0)">Academy Ruins</a></h2>
<p class="hawk-results-item__category"><a href="/shop/singles/double-masters/?finish=Non-foil" id="ctl00_SearchBody_ProductResults_lvItems_ctrl0_ctl00_item_setLink">Double Masters</a></p> <p class="hawk-results-item__category"><a href="/shop/singles/double-masters/?finish=Non-foil" id="ctl00_SearchBody_ProductResults_lvItems_ctrl0_ctl00_item_setLink">Double Masters</a></p>
</div> </div>
<div class="hawk-wishListDiv"> <div class="hawk-wishListDiv">
@ -248,7 +248,7 @@
<div class="hawk-results-item"> <div class="hawk-results-item">
<div class="hawk-results-item__header" > <div class="hawk-results-item__header" >
<div class="hawk-headerDiv"> <div class="hawk-headerDiv">
<h2 class="hawk-results-item__title"><a href="/academy-ruins-sgl-mtg-2xm-309-enf/?sku=SGL-MTG-2XM-309-ENF1" id="ctl00_SearchBody_ProductResults_lvItems_ctrl0_ctl01_item_lnk2" onclick="return HawkSearch.link(event,&#39;41010ff5-0527-4462-8791-3c9dfdc5ab80&#39;,2,&#39;510667&#39;,0);">Academy Ruins</a></h2> <h2 class="hawk-results-item__title"><a href="/academy-ruins-sgl-mtg-2xm-309-enf/?sku=SGL-MTG-2XM-309-ENF1" id="ctl00_SearchBody_ProductResults_lvItems_ctrl0_ctl01_item_lnk2" onclick="return HawkSearch.link(event,&#39;41010ff5-0527-4462-8791-3c9dfdc5ab80&#39;,2,&#39;510667&#39;,0)">Academy Ruins</a></h2>
<p class="hawk-results-item__category"><a href="/shop/singles/double-masters/?finish=Foil" id="ctl00_SearchBody_ProductResults_lvItems_ctrl0_ctl01_item_setLink">Double Masters (Foil)</a></p> <p class="hawk-results-item__category"><a href="/shop/singles/double-masters/?finish=Foil" id="ctl00_SearchBody_ProductResults_lvItems_ctrl0_ctl01_item_setLink">Double Masters (Foil)</a></p>
</div> </div>
<div class="hawk-wishListDiv"> <div class="hawk-wishListDiv">
@ -377,7 +377,7 @@
<div class="hawk-results-item"> <div class="hawk-results-item">
<div class="hawk-results-item__header" > <div class="hawk-results-item__header" >
<div class="hawk-headerDiv"> <div class="hawk-headerDiv">
<h2 class="hawk-results-item__title"><a href="/academy-ruins-sgl-mtg-2xm2-369-enn/?sku=SGL-MTG-2XM2-369-ENN1" id="ctl00_SearchBody_ProductResults_lvItems_ctrl0_ctl02_item_lnk2" onclick="return HawkSearch.link(event,&#39;41010ff5-0527-4462-8791-3c9dfdc5ab80&#39;,3,&#39;510376&#39;,0);">Academy Ruins (Borderless)</a></h2> <h2 class="hawk-results-item__title"><a href="/academy-ruins-sgl-mtg-2xm2-369-enn/?sku=SGL-MTG-2XM2-369-ENN1" id="ctl00_SearchBody_ProductResults_lvItems_ctrl0_ctl02_item_lnk2" onclick="return HawkSearch.link(event,&#39;41010ff5-0527-4462-8791-3c9dfdc5ab80&#39;,3,&#39;510376&#39;,0)">Academy Ruins (Borderless)</a></h2>
<p class="hawk-results-item__category"><a href="/shop/singles/double-masters-variants/?finish=Non-foil" id="ctl00_SearchBody_ProductResults_lvItems_ctrl0_ctl02_item_setLink">Double Masters - Variants</a></p> <p class="hawk-results-item__category"><a href="/shop/singles/double-masters-variants/?finish=Non-foil" id="ctl00_SearchBody_ProductResults_lvItems_ctrl0_ctl02_item_setLink">Double Masters - Variants</a></p>
</div> </div>
<div class="hawk-wishListDiv"> <div class="hawk-wishListDiv">
@ -506,7 +506,7 @@
<div class="hawk-results-item"> <div class="hawk-results-item">
<div class="hawk-results-item__header" > <div class="hawk-results-item__header" >
<div class="hawk-headerDiv"> <div class="hawk-headerDiv">
<h2 class="hawk-results-item__title"><a href="/academy-ruins-sgl-mtg-2xm2-369-enf/?sku=SGL-MTG-2XM2-369-ENF1" id="ctl00_SearchBody_ProductResults_lvItems_ctrl0_ctl03_item_lnk2" onclick="return HawkSearch.link(event,&#39;41010ff5-0527-4462-8791-3c9dfdc5ab80&#39;,4,&#39;511225&#39;,0);">Academy Ruins (Borderless)</a></h2> <h2 class="hawk-results-item__title"><a href="/academy-ruins-sgl-mtg-2xm2-369-enf/?sku=SGL-MTG-2XM2-369-ENF1" id="ctl00_SearchBody_ProductResults_lvItems_ctrl0_ctl03_item_lnk2" onclick="return HawkSearch.link(event,&#39;41010ff5-0527-4462-8791-3c9dfdc5ab80&#39;,4,&#39;511225&#39;,0)">Academy Ruins (Borderless)</a></h2>
<p class="hawk-results-item__category"><a href="/shop/singles/double-masters-variants/?finish=Foil" id="ctl00_SearchBody_ProductResults_lvItems_ctrl0_ctl03_item_setLink">Double Masters - Variants (Foil)</a></p> <p class="hawk-results-item__category"><a href="/shop/singles/double-masters-variants/?finish=Foil" id="ctl00_SearchBody_ProductResults_lvItems_ctrl0_ctl03_item_setLink">Double Masters - Variants (Foil)</a></p>
</div> </div>
<div class="hawk-wishListDiv"> <div class="hawk-wishListDiv">
@ -578,7 +578,7 @@
<div class="hawk-results-item"> <div class="hawk-results-item">
<div class="hawk-results-item__header" > <div class="hawk-results-item__header" >
<div class="hawk-headerDiv"> <div class="hawk-headerDiv">
<h2 class="hawk-results-item__title"><a href="/academy-ruins-sgl-mtg-mma-219-enn/?sku=SGL-MTG-MMA-219-ENN1" id="ctl00_SearchBody_ProductResults_lvItems_ctrl1_ctl00_item_lnk2" onclick="return HawkSearch.link(event,&#39;41010ff5-0527-4462-8791-3c9dfdc5ab80&#39;,5,&#39;240756&#39;,0);">Academy Ruins</a></h2> <h2 class="hawk-results-item__title"><a href="/academy-ruins-sgl-mtg-mma-219-enn/?sku=SGL-MTG-MMA-219-ENN1" id="ctl00_SearchBody_ProductResults_lvItems_ctrl1_ctl00_item_lnk2" onclick="return HawkSearch.link(event,&#39;41010ff5-0527-4462-8791-3c9dfdc5ab80&#39;,5,&#39;240756&#39;,0)">Academy Ruins</a></h2>
<p class="hawk-results-item__category"><a href="/shop/singles/modern-masters-2013-edition/?finish=Non-foil" id="ctl00_SearchBody_ProductResults_lvItems_ctrl1_ctl00_item_setLink">Modern Masters: 2013 Edition</a></p> <p class="hawk-results-item__category"><a href="/shop/singles/modern-masters-2013-edition/?finish=Non-foil" id="ctl00_SearchBody_ProductResults_lvItems_ctrl1_ctl00_item_setLink">Modern Masters: 2013 Edition</a></p>
</div> </div>
<div class="hawk-wishListDiv"> <div class="hawk-wishListDiv">
@ -789,7 +789,7 @@
<div class="hawk-results-item"> <div class="hawk-results-item">
<div class="hawk-results-item__header" > <div class="hawk-results-item__header" >
<div class="hawk-headerDiv"> <div class="hawk-headerDiv">
<h2 class="hawk-results-item__title"><a href="/academy-ruins-sgl-mtg-mma-219-enf/?sku=SGL-MTG-MMA-219-ENF1" id="ctl00_SearchBody_ProductResults_lvItems_ctrl1_ctl01_item_lnk2" onclick="return HawkSearch.link(event,&#39;41010ff5-0527-4462-8791-3c9dfdc5ab80&#39;,6,&#39;240755&#39;,0);">Academy Ruins</a></h2> <h2 class="hawk-results-item__title"><a href="/academy-ruins-sgl-mtg-mma-219-enf/?sku=SGL-MTG-MMA-219-ENF1" id="ctl00_SearchBody_ProductResults_lvItems_ctrl1_ctl01_item_lnk2" onclick="return HawkSearch.link(event,&#39;41010ff5-0527-4462-8791-3c9dfdc5ab80&#39;,6,&#39;240755&#39;,0)">Academy Ruins</a></h2>
<p class="hawk-results-item__category"><a href="/shop/singles/modern-masters-2013-edition/?finish=Foil" id="ctl00_SearchBody_ProductResults_lvItems_ctrl1_ctl01_item_setLink">Modern Masters: 2013 Edition (Foil)</a></p> <p class="hawk-results-item__category"><a href="/shop/singles/modern-masters-2013-edition/?finish=Foil" id="ctl00_SearchBody_ProductResults_lvItems_ctrl1_ctl01_item_setLink">Modern Masters: 2013 Edition (Foil)</a></p>
</div> </div>
<div class="hawk-wishListDiv"> <div class="hawk-wishListDiv">
@ -918,7 +918,7 @@
<div class="hawk-results-item"> <div class="hawk-results-item">
<div class="hawk-results-item__header" > <div class="hawk-results-item__header" >
<div class="hawk-headerDiv"> <div class="hawk-headerDiv">
<h2 class="hawk-results-item__title"><a href="/academy-ruins-sgl-mtg-tsp-269-enn/?sku=SGL-MTG-TSP-269-ENN1" id="ctl00_SearchBody_ProductResults_lvItems_ctrl1_ctl02_item_lnk2" onclick="return HawkSearch.link(event,&#39;41010ff5-0527-4462-8791-3c9dfdc5ab80&#39;,7,&#39;116634&#39;,0);">Academy Ruins</a></h2> <h2 class="hawk-results-item__title"><a href="/academy-ruins-sgl-mtg-tsp-269-enn/?sku=SGL-MTG-TSP-269-ENN1" id="ctl00_SearchBody_ProductResults_lvItems_ctrl1_ctl02_item_lnk2" onclick="return HawkSearch.link(event,&#39;41010ff5-0527-4462-8791-3c9dfdc5ab80&#39;,7,&#39;116634&#39;,0)">Academy Ruins</a></h2>
<p class="hawk-results-item__category"><a href="/shop/singles/time-spiral/?finish=Non-foil" id="ctl00_SearchBody_ProductResults_lvItems_ctrl1_ctl02_item_setLink">Time Spiral</a></p> <p class="hawk-results-item__category"><a href="/shop/singles/time-spiral/?finish=Non-foil" id="ctl00_SearchBody_ProductResults_lvItems_ctrl1_ctl02_item_setLink">Time Spiral</a></p>
</div> </div>
<div class="hawk-wishListDiv"> <div class="hawk-wishListDiv">
@ -2115,7 +2115,7 @@
<div class="hawk-results-item"> <div class="hawk-results-item">
<div class="hawk-results-item__header" > <div class="hawk-results-item__header" >
<div class="hawk-headerDiv"> <div class="hawk-headerDiv">
<h2 class="hawk-results-item__title"><a href="/academy-ruins-sgl-mtg-tsp-269-enf/?sku=SGL-MTG-TSP-269-ENF1" id="ctl00_SearchBody_ProductResults_lvItems_ctrl1_ctl03_item_lnk2" onclick="return HawkSearch.link(event,&#39;41010ff5-0527-4462-8791-3c9dfdc5ab80&#39;,8,&#39;116633&#39;,0);">Academy Ruins</a></h2> <h2 class="hawk-results-item__title"><a href="/academy-ruins-sgl-mtg-tsp-269-enf/?sku=SGL-MTG-TSP-269-ENF1" id="ctl00_SearchBody_ProductResults_lvItems_ctrl1_ctl03_item_lnk2" onclick="return HawkSearch.link(event,&#39;41010ff5-0527-4462-8791-3c9dfdc5ab80&#39;,8,&#39;116633&#39;,0)">Academy Ruins</a></h2>
<p class="hawk-results-item__category"><a href="/shop/singles/time-spiral/?finish=Foil" id="ctl00_SearchBody_ProductResults_lvItems_ctrl1_ctl03_item_setLink">Time Spiral (Foil)</a></p> <p class="hawk-results-item__category"><a href="/shop/singles/time-spiral/?finish=Foil" id="ctl00_SearchBody_ProductResults_lvItems_ctrl1_ctl03_item_setLink">Time Spiral (Foil)</a></p>
</div> </div>
<div class="hawk-wishListDiv"> <div class="hawk-wishListDiv">

View file

@ -225,12 +225,12 @@
<div class="grid_3 " > <div class="grid_3 " >
<div class="itemWrapper hawk-itemWrapper"> <div class="itemWrapper hawk-itemWrapper">
<a href="/rathi-dragon-sgl-mtg-9ed-210-jaf/" id="ctl00_BannerBottom2_FeaturedBottom2_lvItems_ctrl0_ctl00_item_lnk1" class="itemLink" onclick="return HawkSearch.link(event,&#39;54966a4b-5aca-48a6-bdc6-634cac18c27b&#39;,1,&#39;99998&#39;,0);"> <a href="/rathi-dragon-sgl-mtg-9ed-210-jaf/" id="ctl00_BannerBottom2_FeaturedBottom2_lvItems_ctrl0_ctl00_item_lnk1" class="itemLink" onclick="return HawkSearch.link(event,&#39;54966a4b-5aca-48a6-bdc6-634cac18c27b&#39;,1,&#39;99998&#39;,0)">
<img class="itemImage hawk-itemImage" src="" alt="" style="height:190px;" /> <img class="itemImage hawk-itemImage" src="" alt="" style="height:190px;" />
</a> </a>
<h3 class="itemTitle"> <h3 class="itemTitle">
<em style="display: block;"></em> <em style="display: block;"></em>
<a href="/rathi-dragon-sgl-mtg-9ed-210-jaf/" id="ctl00_BannerBottom2_FeaturedBottom2_lvItems_ctrl0_ctl00_item_lnk2" onclick="return HawkSearch.link(event,&#39;54966a4b-5aca-48a6-bdc6-634cac18c27b&#39;,1,&#39;99998&#39;,0);">Rathi Dragon</a> <a href="/rathi-dragon-sgl-mtg-9ed-210-jaf/" id="ctl00_BannerBottom2_FeaturedBottom2_lvItems_ctrl0_ctl00_item_lnk2" onclick="return HawkSearch.link(event,&#39;54966a4b-5aca-48a6-bdc6-634cac18c27b&#39;,1,&#39;99998&#39;,0)">Rathi Dragon</a>
</h3> </h3>
<span class="itemSku">SGL-MTG-9ED-210-JAF</span> <span class="itemSku">SGL-MTG-9ED-210-JAF</span>
<p class="itemDesc"></p> <p class="itemDesc"></p>
@ -244,7 +244,7 @@
<div class="clearfix"> <div class="clearfix">
<div class="itemButtons clearfix"> <div class="itemButtons clearfix">
<a href="/rathi-dragon-sgl-mtg-9ed-210-jaf/" id="ctl00_BannerBottom2_FeaturedBottom2_lvItems_ctrl0_ctl00_item_lnk3" class="btnWrapper" onclick="return HawkSearch.link(event,&#39;54966a4b-5aca-48a6-bdc6-634cac18c27b&#39;,1,&#39;99998&#39;,0);"><span class="btn">View Details</span></a> <a href="/rathi-dragon-sgl-mtg-9ed-210-jaf/" id="ctl00_BannerBottom2_FeaturedBottom2_lvItems_ctrl0_ctl00_item_lnk3" class="btnWrapper" onclick="return HawkSearch.link(event,&#39;54966a4b-5aca-48a6-bdc6-634cac18c27b&#39;,1,&#39;99998&#39;,0)"><span class="btn">View Details</span></a>
</div> </div>
@ -257,12 +257,12 @@
<div class="grid_3 " > <div class="grid_3 " >
<div class="itemWrapper hawk-itemWrapper"> <div class="itemWrapper hawk-itemWrapper">
<a href="/rathi-dragon-sgl-mtg-9ed-210-zsf/" id="ctl00_BannerBottom2_FeaturedBottom2_lvItems_ctrl0_ctl01_item_lnk1" class="itemLink" onclick="return HawkSearch.link(event,&#39;54966a4b-5aca-48a6-bdc6-634cac18c27b&#39;,2,&#39;99996&#39;,0);"> <a href="/rathi-dragon-sgl-mtg-9ed-210-zsf/" id="ctl00_BannerBottom2_FeaturedBottom2_lvItems_ctrl0_ctl01_item_lnk1" class="itemLink" onclick="return HawkSearch.link(event,&#39;54966a4b-5aca-48a6-bdc6-634cac18c27b&#39;,2,&#39;99996&#39;,0)">
<img class="itemImage hawk-itemImage" src="" alt="" style="height:190px;" /> <img class="itemImage hawk-itemImage" src="" alt="" style="height:190px;" />
</a> </a>
<h3 class="itemTitle"> <h3 class="itemTitle">
<em style="display: block;"></em> <em style="display: block;"></em>
<a href="/rathi-dragon-sgl-mtg-9ed-210-zsf/" id="ctl00_BannerBottom2_FeaturedBottom2_lvItems_ctrl0_ctl01_item_lnk2" onclick="return HawkSearch.link(event,&#39;54966a4b-5aca-48a6-bdc6-634cac18c27b&#39;,2,&#39;99996&#39;,0);">Rathi Dragon</a> <a href="/rathi-dragon-sgl-mtg-9ed-210-zsf/" id="ctl00_BannerBottom2_FeaturedBottom2_lvItems_ctrl0_ctl01_item_lnk2" onclick="return HawkSearch.link(event,&#39;54966a4b-5aca-48a6-bdc6-634cac18c27b&#39;,2,&#39;99996&#39;,0)">Rathi Dragon</a>
</h3> </h3>
<span class="itemSku">SGL-MTG-9ED-210-ZSF</span> <span class="itemSku">SGL-MTG-9ED-210-ZSF</span>
<p class="itemDesc"></p> <p class="itemDesc"></p>
@ -276,7 +276,7 @@
<div class="clearfix"> <div class="clearfix">
<div class="itemButtons clearfix"> <div class="itemButtons clearfix">
<a href="/rathi-dragon-sgl-mtg-9ed-210-zsf/" id="ctl00_BannerBottom2_FeaturedBottom2_lvItems_ctrl0_ctl01_item_lnk3" class="btnWrapper" onclick="return HawkSearch.link(event,&#39;54966a4b-5aca-48a6-bdc6-634cac18c27b&#39;,2,&#39;99996&#39;,0);"><span class="btn">View Details</span></a> <a href="/rathi-dragon-sgl-mtg-9ed-210-zsf/" id="ctl00_BannerBottom2_FeaturedBottom2_lvItems_ctrl0_ctl01_item_lnk3" class="btnWrapper" onclick="return HawkSearch.link(event,&#39;54966a4b-5aca-48a6-bdc6-634cac18c27b&#39;,2,&#39;99996&#39;,0)"><span class="btn">View Details</span></a>
</div> </div>
@ -289,12 +289,12 @@
<div class="grid_3 " > <div class="grid_3 " >
<div class="itemWrapper hawk-itemWrapper"> <div class="itemWrapper hawk-itemWrapper">
<a href="/rathi-dragon-sgl-mtg-9ed-210-enn/" id="ctl00_BannerBottom2_FeaturedBottom2_lvItems_ctrl0_ctl02_item_lnk1" class="itemLink" onclick="return HawkSearch.link(event,&#39;54966a4b-5aca-48a6-bdc6-634cac18c27b&#39;,3,&#39;99995&#39;,0);"> <a href="/rathi-dragon-sgl-mtg-9ed-210-enn/" id="ctl00_BannerBottom2_FeaturedBottom2_lvItems_ctrl0_ctl02_item_lnk1" class="itemLink" onclick="return HawkSearch.link(event,&#39;54966a4b-5aca-48a6-bdc6-634cac18c27b&#39;,3,&#39;99995&#39;,0)">
<img class="itemImage hawk-itemImage" src="https&#58;&#47;&#47;cdn11.bigcommerce.com&#47;s-3b5vpig99v&#47;products&#47;99995&#47;images&#47;402488&#47;RathiDragon__87752.1590611958.386.513.jpg&#63;c&#61;2" alt="" style="height:190px;" /> <img class="itemImage hawk-itemImage" src="https&#58;&#47;&#47;cdn11.bigcommerce.com&#47;s-3b5vpig99v&#47;products&#47;99995&#47;images&#47;402488&#47;RathiDragon__87752.1590611958.386.513.jpg&#63;c&#61;2" alt="" style="height:190px;" />
</a> </a>
<h3 class="itemTitle"> <h3 class="itemTitle">
<em style="display: block;"></em> <em style="display: block;"></em>
<a href="/rathi-dragon-sgl-mtg-9ed-210-enn/" id="ctl00_BannerBottom2_FeaturedBottom2_lvItems_ctrl0_ctl02_item_lnk2" onclick="return HawkSearch.link(event,&#39;54966a4b-5aca-48a6-bdc6-634cac18c27b&#39;,3,&#39;99995&#39;,0);">Rathi Dragon</a> <a href="/rathi-dragon-sgl-mtg-9ed-210-enn/" id="ctl00_BannerBottom2_FeaturedBottom2_lvItems_ctrl0_ctl02_item_lnk2" onclick="return HawkSearch.link(event,&#39;54966a4b-5aca-48a6-bdc6-634cac18c27b&#39;,3,&#39;99995&#39;,0)">Rathi Dragon</a>
</h3> </h3>
<span class="itemSku">SGL-MTG-9ED-210-ENN</span> <span class="itemSku">SGL-MTG-9ED-210-ENN</span>
<p class="itemDesc"></p> <p class="itemDesc"></p>
@ -308,7 +308,7 @@
<div class="clearfix"> <div class="clearfix">
<div class="itemButtons clearfix"> <div class="itemButtons clearfix">
<a href="/rathi-dragon-sgl-mtg-9ed-210-enn/" id="ctl00_BannerBottom2_FeaturedBottom2_lvItems_ctrl0_ctl02_item_lnk3" class="btnWrapper" onclick="return HawkSearch.link(event,&#39;54966a4b-5aca-48a6-bdc6-634cac18c27b&#39;,3,&#39;99995&#39;,0);"><span class="btn">View Details</span></a> <a href="/rathi-dragon-sgl-mtg-9ed-210-enn/" id="ctl00_BannerBottom2_FeaturedBottom2_lvItems_ctrl0_ctl02_item_lnk3" class="btnWrapper" onclick="return HawkSearch.link(event,&#39;54966a4b-5aca-48a6-bdc6-634cac18c27b&#39;,3,&#39;99995&#39;,0)"><span class="btn">View Details</span></a>
</div> </div>

View file

@ -7,28 +7,26 @@ import (
"strings" "strings"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
"gitlab.com/flygrounder/go-mtg-vk/internal/cardsinfo"
) )
type Handler struct { type Handler struct {
Sender Sender Sender sender
Logger *log.Logger Logger *log.Logger
SecretKey string SecretKey string
GroupId int64 GroupId int64
ConfirmationString string ConfirmationString string
DictPath string DictPath string
Cache CardCache Cache cardCache
InfoFetcher CardInfoFetcher InfoFetcher cardInfoFetcher
} }
type CardInfoFetcher interface { type cardInfoFetcher interface {
GetPrices(name string) ([]cardsinfo.CardPrice, error) GetFormattedCardPrices(name string) (string, error)
FormatCardPrices(name string, prices []cardsinfo.CardPrice) string
GetNameByCardId(set string, number string) string GetNameByCardId(set string, number string) string
GetOriginalName(name string) string GetOriginalName(name string) string
} }
type CardCache interface { type cardCache interface {
Get(cardName string) (string, error) Get(cardName string) (string, error)
Set(cardName string, message string) Set(cardName string, message string)
} }
@ -69,19 +67,19 @@ func (h *Handler) HandleMessage(c *gin.Context) {
func (h *Handler) handleSearch(req *messageRequest) { func (h *Handler) handleSearch(req *messageRequest) {
cardName, err := h.getCardNameByCommand(req.Object.Body) cardName, err := h.getCardNameByCommand(req.Object.Body)
if err != nil { if err != nil {
h.Sender.Send(req.Object.UserId, incorrectMessage) h.Sender.send(req.Object.UserId, incorrectMessage)
h.Logger.Printf("[info] Not correct command. Message: %s user input: %s", err.Error(), req.Object.Body) h.Logger.Printf("[info] Not correct command. Message: %s user input: %s", err.Error(), req.Object.Body)
} else if cardName == "" { } else if cardName == "" {
h.Sender.Send(req.Object.UserId, cardNotFoundMessage) h.Sender.send(req.Object.UserId, cardNotFoundMessage)
h.Logger.Printf("[info] Could not find card. User input: %s", req.Object.Body) h.Logger.Printf("[info] Could not find card. User input: %s", req.Object.Body)
} else { } else {
message, err := h.getMessage(cardName) message, err := h.getMessage(cardName)
if err != nil { if err != nil {
h.Sender.Send(req.Object.UserId, pricesUnavailableMessage) h.Sender.send(req.Object.UserId, pricesUnavailableMessage)
h.Logger.Printf("[error] Could not find SCG prices. Message: %s card name: %s", err.Error(), cardName) h.Logger.Printf("[error] Could not find SCG prices. Message: %s card name: %s", err.Error(), cardName)
return return
} }
h.Sender.Send(req.Object.UserId, message) h.Sender.send(req.Object.UserId, message)
} }
} }
@ -94,11 +92,10 @@ func (h *Handler) handleConfirmation(c *gin.Context, req *messageRequest) {
func (h *Handler) getMessage(cardName string) (string, error) { func (h *Handler) getMessage(cardName string) (string, error) {
val, err := h.Cache.Get(cardName) val, err := h.Cache.Get(cardName)
if err != nil { if err != nil {
prices, err := h.InfoFetcher.GetPrices(cardName) message, err := h.InfoFetcher.GetFormattedCardPrices(cardName)
if err != nil { if err != nil {
return "", err return "", err
} }
message := h.InfoFetcher.FormatCardPrices(cardName, prices)
h.Cache.Set(cardName, message) h.Cache.Set(cardName, message)
return message, nil return message, nil
} }

View file

@ -11,10 +11,10 @@ import (
"strings" "strings"
) )
const SendMessageUrl = "https://api.vk.com/method/messages.send" const sendMessageUrl = "https://api.vk.com/method/messages.send"
type Sender interface { type sender interface {
Send(userId int64, message string) send(userId int64, message string)
} }
type ApiSender struct { type ApiSender struct {
@ -31,7 +31,7 @@ type errorResponse struct {
ErrorMsg string `json:"error_msg"` ErrorMsg string `json:"error_msg"`
} }
func (s *ApiSender) Send(userId int64, message string) { func (s *ApiSender) send(userId int64, message string) {
randomId := rand.Int63() randomId := rand.Int63()
params := []string{ params := []string{
"access_token=" + s.Token, "access_token=" + s.Token,
@ -41,7 +41,7 @@ func (s *ApiSender) Send(userId int64, message string) {
"random_id=" + strconv.FormatInt(randomId, 10), "random_id=" + strconv.FormatInt(randomId, 10),
} }
joined := strings.Join(params, "&") joined := strings.Join(params, "&")
reqUrl := SendMessageUrl + "?" + joined reqUrl := sendMessageUrl + "?" + joined
resp, err := http.Get(reqUrl) resp, err := http.Get(reqUrl)
if err != nil || resp.StatusCode != http.StatusOK { if err != nil || resp.StatusCode != http.StatusOK {
s.Logger.Printf("[error] Could not Send message. User: %d", userId) s.Logger.Printf("[error] Could not Send message. User: %d", userId)

View file

@ -13,7 +13,7 @@ import (
func TestApiSender_Send_OK(t *testing.T) { func TestApiSender_Send_OK(t *testing.T) {
defer gock.Off() defer gock.Off()
gock.New(SendMessageUrl).MatchParams( gock.New(sendMessageUrl).MatchParams(
map[string]string{ map[string]string{
"access_token": "token", "access_token": "token",
"peer_id": "1", "peer_id": "1",
@ -23,28 +23,28 @@ func TestApiSender_Send_OK(t *testing.T) {
).ParamPresent("random_id").Reply(http.StatusOK) ).ParamPresent("random_id").Reply(http.StatusOK)
sender := ApiSender{Token: "token"} sender := ApiSender{Token: "token"}
sender.Send(1, "msg") sender.send(1, "msg")
assert.False(t, gock.HasUnmatchedRequest()) assert.False(t, gock.HasUnmatchedRequest())
} }
func TestApiSender_Send_NotOK(t *testing.T) { func TestApiSender_Send_NotOK(t *testing.T) {
defer gock.Off() defer gock.Off()
gock.New(SendMessageUrl).Reply(http.StatusInternalServerError) gock.New(sendMessageUrl).Reply(http.StatusInternalServerError)
b := &bytes.Buffer{} b := &bytes.Buffer{}
sender := ApiSender{ sender := ApiSender{
Token: "token", Token: "token",
Logger: log.New(b, "", 0), Logger: log.New(b, "", 0),
} }
sender.Send(1, "msg") sender.send(1, "msg")
assert.True(t, strings.Contains(b.String(), "[error]")) assert.True(t, strings.Contains(b.String(), "[error]"))
} }
func TestApiSender_Send_ErrorCode(t *testing.T) { func TestApiSender_Send_ErrorCode(t *testing.T) {
defer gock.Off() defer gock.Off()
gock.New(SendMessageUrl).Reply(http.StatusOK).JSON( gock.New(sendMessageUrl).Reply(http.StatusOK).JSON(
map[string]interface{}{ map[string]interface{}{
"error": map[string]interface{}{ "error": map[string]interface{}{
"error_code": 100, "error_code": 100,
@ -58,6 +58,6 @@ func TestApiSender_Send_ErrorCode(t *testing.T) {
Token: "token", Token: "token",
Logger: log.New(b, "", 0), Logger: log.New(b, "", 0),
} }
sender.Send(1, "msg") sender.send(1, "msg")
assert.True(t, strings.Contains(b.String(), "[error]")) assert.True(t, strings.Contains(b.String(), "[error]"))
} }

View file

@ -2,20 +2,15 @@ package vk
import ( import (
"errors" "errors"
"gitlab.com/flygrounder/go-mtg-vk/internal/cardsinfo"
) )
type testInfoFetcher struct{} type testInfoFetcher struct{}
func (t *testInfoFetcher) GetPrices(name string) ([]cardsinfo.CardPrice, error) { func (t *testInfoFetcher) GetFormattedCardPrices(name string) (string, error) {
if name == "good" || name == "uncached" { if name == "good" || name == "uncached" {
return nil, nil return name, nil
} }
return nil, errors.New("test") return "", errors.New("test")
}
func (t *testInfoFetcher) FormatCardPrices(name string, _ []cardsinfo.CardPrice) string {
return name
} }
func (t *testInfoFetcher) GetNameByCardId(_ string, _ string) string { func (t *testInfoFetcher) GetNameByCardId(_ string, _ string) string {

View file

@ -9,7 +9,7 @@ type testMessage struct {
message string message string
} }
func (s *testSender) Send(userId int64, message string) { func (s *testSender) send(userId int64, message string) {
s.sent = append(s.sent, testMessage{ s.sent = append(s.sent, testMessage{
userId: userId, userId: userId,
message: message, message: message,