From 0c52477b59b98d22f4c228d068320780b9df5835 Mon Sep 17 00:00:00 2001 From: jasonBirchall Date: Thu, 14 Jan 2021 18:20:55 +0000 Subject: [PATCH 1/3] Make iterating over json objects easier This means we don't have a plethora of datapoints to dig. --- cmd/graph.go | 73 +++++++++++++++++++++------------------------------- 1 file changed, 29 insertions(+), 44 deletions(-) diff --git a/cmd/graph.go b/cmd/graph.go index 55a1972..c6131ca 100644 --- a/cmd/graph.go +++ b/cmd/graph.go @@ -16,39 +16,15 @@ limitations under the License. package cmd import ( + "encoding/json" "fmt" - "math" - "reflect" - "github.com/guptarohit/asciigraph" api "github.com/jasonbirchall/crypto-tracker/internal/api" - "github.com/m7shapan/njson" "github.com/spf13/cobra" ) type Price struct { - DataPoint5 float64 `njson:"data.coins.0.history.5"` - DataPoint6 float64 `njson:"data.coins.0.history.6"` - DataPoint7 float64 `njson:"data.coins.0.history.7"` - DataPoint8 float64 `njson:"data.coins.0.history.8"` - DataPoint9 float64 `njson:"data.coins.0.history.9"` - DataPoint10 float64 `njson:"data.coins.0.history.10"` - DataPoint11 float64 `njson:"data.coins.0.history.11"` - DataPoint12 float64 `njson:"data.coins.0.history.12"` - DataPoint13 float64 `njson:"data.coins.0.history.13"` - DataPoint14 float64 `njson:"data.coins.0.history.14"` - DataPoint15 float64 `njson:"data.coins.0.history.15"` - DataPoint16 float64 `njson:"data.coins.0.history.16"` - DataPoint17 float64 `njson:"data.coins.0.history.17"` - DataPoint18 float64 `njson:"data.coins.0.history.18"` - DataPoint19 float64 `njson:"data.coins.0.history.19"` - DataPoint20 float64 `njson:"data.coins.0.history.20"` - DataPoint21 float64 `njson:"data.coins.0.history.21"` - DataPoint22 float64 `njson:"data.coins.0.history.22"` - DataPoint23 float64 `njson:"data.coins.0.history.23"` - DataPoint24 float64 `njson:"data.coins.0.history.24"` - DataPoint25 float64 `njson:"data.coins.0.history.25"` - DataPoint26 float64 `njson:"data.coins.0.history.26"` + DataPoints []string `json:"datapoints"` } var coin string @@ -65,45 +41,54 @@ var graphCmd = &cobra.Command{ return err } - graph := asciigraph.Plot(data, asciigraph.Height(height)) + // graph := asciigraph.Plot(data, asciigraph.Height(height)) - fmt.Println(graph) + fmt.Println(data) return nil }, } -// getCoin takes a string as an argument, this is passed as a flag by the +// getData takes a string as an argument, this is passed as a flag by the // graph command. It queries the API package and collates a collection of // float64 data type. This collection is then returned back to the command // function. -func getData(coin string) ([]float64, error) { - var c Price - var arr []float64 +func getData(coin string) ([]string, error) { + var p Price + var arr []string data, err := api.Query(coin) + + err = json.Unmarshal(data, &p) if err != nil { return arr, err } - err = njson.Unmarshal([]byte(data), &c) - if err != nil { - return arr, err + for _, v := range p.DataPoints { + fmt.Println(v) } - fields := reflect.TypeOf(c) - values := reflect.ValueOf(c) - num := fields.NumField() + return arr, nil +} - for i := 0; i < num; i++ { - value := values.Field(i) - v := value.Interface().(float64) - c := math.Round(v*100) / 100 +func (p *Price) UnmarshalJSON(data []byte) error { + var v struct { + Data struct { + Coins []struct { + History []string `json:"history"` + } `json:"coins"` + } `json:"data"` + } - arr = append(arr, c) + if err := json.Unmarshal(data, &v); err != nil { + return err } - return arr, nil + if len(v.Data.Coins) > 0 { + p.DataPoints = v.Data.Coins[0].History + } + + return nil } func init() { From c280ca630f6d209dd85ad9227e93b878ceb15f47 Mon Sep 17 00:00:00 2001 From: jasonBirchall Date: Thu, 14 Jan 2021 18:43:57 +0000 Subject: [PATCH 2/3] Convert the string values in float64 for asciigraph The graph requires the input data type to float64, which requires me to first convert and then round to two decimal places. --- cmd/graph.go | 22 +++++++++++++++++----- 1 file changed, 17 insertions(+), 5 deletions(-) diff --git a/cmd/graph.go b/cmd/graph.go index c6131ca..1621a10 100644 --- a/cmd/graph.go +++ b/cmd/graph.go @@ -18,7 +18,10 @@ package cmd import ( "encoding/json" "fmt" + "math" + "strconv" + "github.com/guptarohit/asciigraph" api "github.com/jasonbirchall/crypto-tracker/internal/api" "github.com/spf13/cobra" ) @@ -41,9 +44,9 @@ var graphCmd = &cobra.Command{ return err } - // graph := asciigraph.Plot(data, asciigraph.Height(height)) + graph := asciigraph.Plot(data, asciigraph.Height(height)) - fmt.Println(data) + fmt.Println(graph) return nil }, @@ -53,9 +56,9 @@ var graphCmd = &cobra.Command{ // graph command. It queries the API package and collates a collection of // float64 data type. This collection is then returned back to the command // function. -func getData(coin string) ([]string, error) { +func getData(coin string) ([]float64, error) { var p Price - var arr []string + var arr []float64 data, err := api.Query(coin) @@ -64,8 +67,17 @@ func getData(coin string) ([]string, error) { return arr, err } + // Iterate over all strings in struct and convert to float64, which + // is requried by the asciigraph package. for _, v := range p.DataPoints { - fmt.Println(v) + s, err := strconv.ParseFloat(v, 64) + if err != nil { + return arr, err + } + + r := math.Round(s*100) / 100 + + arr = append(arr, r) } return arr, nil From 0e891936e55ef15914b8032e1a0935a20fa2cf25 Mon Sep 17 00:00:00 2001 From: jasonBirchall Date: Thu, 14 Jan 2021 18:46:31 +0000 Subject: [PATCH 3/3] Bump version to 0.1.9 --- internal/version/version.go | 2 +- snapcraft.yaml | 17 +++++++++++++++++ 2 files changed, 18 insertions(+), 1 deletion(-) create mode 100644 snapcraft.yaml diff --git a/internal/version/version.go b/internal/version/version.go index 8d5c939..265e98c 100644 --- a/internal/version/version.go +++ b/internal/version/version.go @@ -1,4 +1,4 @@ package version -var VERSION = "0.1.8" +var VERSION = "0.1.9" var REVISION = "unknown" diff --git a/snapcraft.yaml b/snapcraft.yaml new file mode 100644 index 0000000..5159631 --- /dev/null +++ b/snapcraft.yaml @@ -0,0 +1,17 @@ +name: crypto +version: git +summary: Command line crytocurrency tracker +description: | + Crypto-tracker is a simple, robust command-line application that displays the price of popular cryptocurrencies. +confinement: devmode +base: core20 +parts: + crypto: + plugin: go + source: . + source-type: git + build-packages: + - gcc +# apps: +# crypto-tracker: +# command: crypto