Skip to content

Commit

Permalink
struct file / test
Browse files Browse the repository at this point in the history
  • Loading branch information
sh-cho committed Jun 27, 2023
1 parent f41a3ba commit 4c99349
Show file tree
Hide file tree
Showing 5 changed files with 101 additions and 35 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/check-and-send-message.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ jobs:
- uses: actions/setup-go@v4
with:
go-version: '1.20'
- run: go run main.go
- run: go run .
env:
TELEGRAM_APITOKEN: ${{ secrets.TELEGRAM_APITOKEN }}
CHATID: ${{ secrets.CHATID }}
Expand Down
16 changes: 16 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
name: Check and send message

on:
push:
branches:
- main

jobs:
check:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/setup-go@v4
with:
go-version: '1.20'
- run: go test
42 changes: 42 additions & 0 deletions fgi.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package main

import "fmt"

type VVT struct {
Value int
ValueText string
}

type FgiResult struct {
LastUpdated struct {
EpochUnixSeconds int
HumanDate string
}
Fgi Fgi // XXX: Why just Fgi is not working?
}

type Fgi struct {
Now VVT
PreviousClose VVT
OneWeekAgo VVT
OneMonthAgo VVT
OneYearAgo VVT
}

func (v VVT) toString() string {
return fmt.Sprintf("%d (%s)", v.Value, v.ValueText)
}

func (fr FgiResult) toString() string {
return fmt.Sprintf(`[lastUpdate: %s]
- now: %s
- prev: %s
- 1w ago: %s
- 1m ago: %s
- 1y ago: %s`, fr.LastUpdated.HumanDate,
fr.Fgi.Now.toString(),
fr.Fgi.PreviousClose.toString(),
fr.Fgi.OneWeekAgo.toString(),
fr.Fgi.OneMonthAgo.toString(),
fr.Fgi.OneYearAgo.toString())
}
42 changes: 42 additions & 0 deletions fgi_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package main

import (
"encoding/json"
"testing"
)

func TestFgiParse(t *testing.T) {
res := `{
"lastUpdated": {
"epochUnixSeconds": 1687874279,
"humanDate": "2023-06-27T13:57:59.000Z"
},
"fgi": {
"now": {
"value": 73,
"valueText": "Greed"
},
"previousClose": {
"value": 71,
"valueText": "Greed"
},
"oneWeekAgo": {
"value": 78,
"valueText": "Extreme Greed"
},
"oneMonthAgo": {
"value": 69,
"valueText": "Greed"
},
"oneYearAgo": {
"value": 30,
"valueText": "Fear"
}
}
}`
r := FgiResult{}
err := json.Unmarshal([]byte(res), &r)
if err != nil {
t.Errorf("parse failed")
}
}
34 changes: 0 additions & 34 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package main

import (
"encoding/json"
"fmt"
tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5"
"io"
"net/http"
Expand All @@ -14,39 +13,6 @@ const (
FgiApi = "https://fear-and-greed-index.p.rapidapi.com/v1/fgi"
)

type VVT struct {
Value int
ValueText string
}

type FgiResult struct {
LastUpdated struct {
EpochUnixSeconds int
HumanDate string
}
Fgi struct {
Now VVT
PreviousClose VVT
OneWeekAgo VVT
OneMonthAgo VVT
OneYearAgo VVT
}
}

func (fr FgiResult) toString() string {
return fmt.Sprintf(`[lastUpdate: %s]
- now: %d (%s)
- prev: %d (%s)
- 1w ago: %d (%s)
- 1m ago: %d (%s)
- 1y ago: %d (%s)`, fr.LastUpdated.HumanDate,
fr.Fgi.Now.Value, fr.Fgi.Now.ValueText,
fr.Fgi.PreviousClose.Value, fr.Fgi.PreviousClose.ValueText,
fr.Fgi.OneWeekAgo.Value, fr.Fgi.OneWeekAgo.ValueText,
fr.Fgi.OneMonthAgo.Value, fr.Fgi.OneMonthAgo.ValueText,
fr.Fgi.OneYearAgo.Value, fr.Fgi.OneYearAgo.ValueText)
}

func main() {
res := getFearAndGreedIndex()
sendMesage(res)
Expand Down

0 comments on commit 4c99349

Please sign in to comment.