forked from saniales/go-hitbtc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathticker.go
73 lines (64 loc) · 1.47 KB
/
ticker.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
package hitbtc
import (
"encoding/json"
"time"
)
//Ticker represents a Ticker from hitbtc API.
type Ticker struct {
Ask float64 `json:"ask,string"`
Bid float64 `json:"bid,string"`
Last float64 `json:"last,string"`
Open float64 `json:"open,string"`
Low float64 `json:"low,string"`
High float64 `json:"high,string"`
Volume float64 `json:"volume,string"`
VolumeQuote float64 `json:"volumeQuote,string"`
Timestamp time.Time `json:"timestamp"`
Symbol string `json:"symbol"`
}
// Tickers rapresents a set of a valid Tickers struct
type Tickers []Ticker
func (t *Ticker) UnmarshalJSON(data []byte) error {
var err error
type Alias Ticker
aux := &struct {
Timestamp string `json:"timestamp"`
High string `json:"high"`
Low string `json:"low"`
*Alias
}{
Alias: (*Alias)(t),
}
if aux.High == "" {
aux.High = "0"
}
if aux.Low == "" {
aux.Low = "0"
}
if err = json.Unmarshal(data, &aux); err != nil {
return err
}
t.Timestamp, err = time.Parse("2006-01-02T15:04:05.999Z", aux.Timestamp)
if err != nil {
return err
}
return nil
}
func (ts Tickers) UnmarshalJSON(data []byte) error {
var arr []json.RawMessage
if err := json.Unmarshal(data, &arr); err != nil {
return err
}
for _, v := range arr {
var ticker Ticker
err := json.Unmarshal(v, &ticker)
if ticker.Volume == 0 {
continue
}
if err != nil {
return err
}
ts = append(ts, ticker)
}
return nil
}