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

@ -7,28 +7,26 @@ import (
"strings"
"github.com/gin-gonic/gin"
"gitlab.com/flygrounder/go-mtg-vk/internal/cardsinfo"
)
type Handler struct {
Sender Sender
Sender sender
Logger *log.Logger
SecretKey string
GroupId int64
ConfirmationString string
DictPath string
Cache CardCache
InfoFetcher CardInfoFetcher
Cache cardCache
InfoFetcher cardInfoFetcher
}
type CardInfoFetcher interface {
GetPrices(name string) ([]cardsinfo.CardPrice, error)
FormatCardPrices(name string, prices []cardsinfo.CardPrice) string
type cardInfoFetcher interface {
GetFormattedCardPrices(name string) (string, error)
GetNameByCardId(set string, number string) string
GetOriginalName(name string) string
}
type CardCache interface {
type cardCache interface {
Get(cardName string) (string, error)
Set(cardName string, message string)
}
@ -69,19 +67,19 @@ func (h *Handler) HandleMessage(c *gin.Context) {
func (h *Handler) handleSearch(req *messageRequest) {
cardName, err := h.getCardNameByCommand(req.Object.Body)
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)
} 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)
} else {
message, err := h.getMessage(cardName)
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)
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) {
val, err := h.Cache.Get(cardName)
if err != nil {
prices, err := h.InfoFetcher.GetPrices(cardName)
message, err := h.InfoFetcher.GetFormattedCardPrices(cardName)
if err != nil {
return "", err
}
message := h.InfoFetcher.FormatCardPrices(cardName, prices)
h.Cache.Set(cardName, message)
return message, nil
}

View file

@ -11,10 +11,10 @@ import (
"strings"
)
const SendMessageUrl = "https://api.vk.com/method/messages.send"
const sendMessageUrl = "https://api.vk.com/method/messages.send"
type Sender interface {
Send(userId int64, message string)
type sender interface {
send(userId int64, message string)
}
type ApiSender struct {
@ -31,7 +31,7 @@ type errorResponse struct {
ErrorMsg string `json:"error_msg"`
}
func (s *ApiSender) Send(userId int64, message string) {
func (s *ApiSender) send(userId int64, message string) {
randomId := rand.Int63()
params := []string{
"access_token=" + s.Token,
@ -41,7 +41,7 @@ func (s *ApiSender) Send(userId int64, message string) {
"random_id=" + strconv.FormatInt(randomId, 10),
}
joined := strings.Join(params, "&")
reqUrl := SendMessageUrl + "?" + joined
reqUrl := sendMessageUrl + "?" + joined
resp, err := http.Get(reqUrl)
if err != nil || resp.StatusCode != http.StatusOK {
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) {
defer gock.Off()
gock.New(SendMessageUrl).MatchParams(
gock.New(sendMessageUrl).MatchParams(
map[string]string{
"access_token": "token",
"peer_id": "1",
@ -23,28 +23,28 @@ func TestApiSender_Send_OK(t *testing.T) {
).ParamPresent("random_id").Reply(http.StatusOK)
sender := ApiSender{Token: "token"}
sender.Send(1, "msg")
sender.send(1, "msg")
assert.False(t, gock.HasUnmatchedRequest())
}
func TestApiSender_Send_NotOK(t *testing.T) {
defer gock.Off()
gock.New(SendMessageUrl).Reply(http.StatusInternalServerError)
gock.New(sendMessageUrl).Reply(http.StatusInternalServerError)
b := &bytes.Buffer{}
sender := ApiSender{
Token: "token",
Logger: log.New(b, "", 0),
}
sender.Send(1, "msg")
sender.send(1, "msg")
assert.True(t, strings.Contains(b.String(), "[error]"))
}
func TestApiSender_Send_ErrorCode(t *testing.T) {
defer gock.Off()
gock.New(SendMessageUrl).Reply(http.StatusOK).JSON(
gock.New(sendMessageUrl).Reply(http.StatusOK).JSON(
map[string]interface{}{
"error": map[string]interface{}{
"error_code": 100,
@ -58,6 +58,6 @@ func TestApiSender_Send_ErrorCode(t *testing.T) {
Token: "token",
Logger: log.New(b, "", 0),
}
sender.Send(1, "msg")
sender.send(1, "msg")
assert.True(t, strings.Contains(b.String(), "[error]"))
}

View file

@ -2,20 +2,15 @@ package vk
import (
"errors"
"gitlab.com/flygrounder/go-mtg-vk/internal/cardsinfo"
)
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" {
return nil, nil
return name, nil
}
return nil, errors.New("test")
}
func (t *testInfoFetcher) FormatCardPrices(name string, _ []cardsinfo.CardPrice) string {
return name
return "", errors.New("test")
}
func (t *testInfoFetcher) GetNameByCardId(_ string, _ string) string {

View file

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