Skip to content
This repository has been archived by the owner on Aug 23, 2023. It is now read-only.

Commit

Permalink
Merge pull request #13 from jasonBirchall/change-json
Browse files Browse the repository at this point in the history
Change json graph dataset
  • Loading branch information
jasonBirchall authored Jan 14, 2021
2 parents 6c75aed + 0e89193 commit 48b216d
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 39 deletions.
73 changes: 35 additions & 38 deletions cmd/graph.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,39 +16,18 @@ limitations under the License.
package cmd

import (
"encoding/json"
"fmt"
"math"
"reflect"
"strconv"

"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
Expand All @@ -73,39 +52,57 @@ var graphCmd = &cobra.Command{
},
}

// 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 p Price
var arr []float64

data, err := api.Query(coin)
if err != nil {
return arr, err
}

err = njson.Unmarshal([]byte(data), &c)
err = json.Unmarshal(data, &p)
if err != nil {
return arr, err
}

fields := reflect.TypeOf(c)
values := reflect.ValueOf(c)
num := fields.NumField()
// Iterate over all strings in struct and convert to float64, which
// is requried by the asciigraph package.
for _, v := range p.DataPoints {
s, err := strconv.ParseFloat(v, 64)
if err != nil {
return arr, err
}

for i := 0; i < num; i++ {
value := values.Field(i)
v := value.Interface().(float64)
c := math.Round(v*100) / 100
r := math.Round(s*100) / 100

arr = append(arr, c)
arr = append(arr, r)
}

return arr, nil
}

func (p *Price) UnmarshalJSON(data []byte) error {
var v struct {
Data struct {
Coins []struct {
History []string `json:"history"`
} `json:"coins"`
} `json:"data"`
}

if err := json.Unmarshal(data, &v); err != nil {
return err
}

if len(v.Data.Coins) > 0 {
p.DataPoints = v.Data.Coins[0].History
}

return nil
}

func init() {
rootCmd.AddCommand(graphCmd)

Expand Down
2 changes: 1 addition & 1 deletion internal/version/version.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package version

var VERSION = "0.1.8"
var VERSION = "0.1.9"
var REVISION = "unknown"
17 changes: 17 additions & 0 deletions snapcraft.yaml
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 48b216d

Please sign in to comment.