-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgold.go
332 lines (260 loc) · 7.37 KB
/
gold.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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
// prices of gold calculated at NBP
//
// types: NBPGold, GoldRate
// public func: NewGold
// NBPGold methods:
// GoldRaw, GoldByDate, GoldLast,
// GetPriceToday, GetPriceCurrent, GetPriceByDate
// GetPrettyOutput, GetCSVOutput, GetRawOutput
package nbpapi
import (
"encoding/json"
"fmt"
"net/http"
"strconv"
"strings"
"text/tabwriter"
"time"
)
// NewGold - function creates new gold type
func NewGold() *NBPGold {
cli := &http.Client{
Timeout: time.Second * 10,
}
r := &NBPGold{
Client: cli,
}
return r
}
/*
GoldRaw - function downloads data in json or xml form
Function returns error or nil
Parameters:
date - date in the format: 'YYYY-MM-DD' (ISO 8601 standard),
or a range of dates in the format: 'YYYY-MM-DD:YYYY-MM-DD' or 'today'
(price for today) or 'current' - current gold price (last published)
last - as an alternative to date, the last <n> prices of gold
can be retrieved
format - 'json' or 'xml'
*/
func (g *NBPGold) GoldRaw(date string, last int, format string) error {
var err error
url := g.getGoldAddress(date, last)
g.result, err = g.getData(url, format)
if err != nil {
return err
}
return err
}
/*
GoldByDate - function downloads and writes data to goldRates slice,
raw data (json) still available in NBPGold.result field
Function returns error or nil
Parameters:
date - date in the format: 'YYYY-MM-DD' (ISO 8601 standard),
or a range of dates in the format: 'YYYY-MM-DD:YYYY-MM-DD' or 'today'
(price for today) or 'current' - current gold price (last published)
*/
func (g *NBPGold) GoldByDate(date string) error {
var err error
url := g.getGoldAddress(date, 0)
g.result, err = g.getData(url, "json")
if err != nil {
return err
}
err = json.Unmarshal(g.result, &g.GoldRates)
if err != nil {
return err
}
return err
}
/*
GoldLast - function downloads and writes data to GoldRates slice,
raw data (json) still available in NBPGold.result field
Function returns error or nil
Parameters:
last - as an alternative to date, the last <n> prices of gold
can be retrieved
*/
func (g *NBPGold) GoldLast(last int) error {
var err error
url := g.getGoldAddress("", last)
g.result, err = g.getData(url, "json")
if err != nil {
return err
}
err = json.Unmarshal(g.result, &g.GoldRates)
if err != nil {
return err
}
return err
}
/*
GetPriceToday - function downloads and returns today's gold price,
as GoldRate struct
*/
func (g *NBPGold) GetPriceToday() (GoldRate, error) {
var err error
url := g.getGoldAddress("today", 0)
g.result, err = g.getData(url, "json")
if err != nil {
return GoldRate{}, err
}
err = json.Unmarshal(g.result, &g.GoldRates)
if err != nil {
return GoldRate{}, err
}
return g.GoldRates[0], err
}
/*
GetPriceCurrent - function downloads and returns current gold price as
GoldRate struct
*/
func (g *NBPGold) GetPriceCurrent() (GoldRate, error) {
var err error
url := g.getGoldAddress("current", 0)
g.result, err = g.getData(url, "json")
if err != nil {
return GoldRate{}, err
}
err = json.Unmarshal(g.result, &g.GoldRates)
if err != nil {
return GoldRate{}, err
}
return g.GoldRates[0], err
}
/*
GetPriceByDate - function returns gold prices (as slice of struct),
by date ("YYYY-MM-DD") or range of dates ("YYYY-MM-DD:YYYY-MM-DD")
Parameters:
date - date in the format: 'YYYY-MM-DD' (ISO 8601 standard),
or a range of dates in the format: 'YYYY-MM-DD:YYYY-MM-DD' or 'today'
(price for today) or 'current' - current gold price (last published)
*/
func (g *NBPGold) GetPriceByDate(date string) ([]GoldRate, error) {
var err error
url := g.getGoldAddress(date, 0)
g.result, err = g.getData(url, "json")
if err != nil {
return nil, err
}
err = json.Unmarshal(g.result, &g.GoldRates)
if err != nil {
return nil, err
}
return g.GoldRates, err
}
/*
CreatePrettyOutput - function returns a formatted table of gold prices
Parameters:
lang - 'en' or 'pl'
*/
func (g *NBPGold) CreatePrettyOutput(lang string) string {
const padding = 3
var builder strings.Builder
var total float64
// output language
setLang(lang)
w := tabwriter.NewWriter(&builder, 0, 0, padding, ' ', tabwriter.Debug)
fmt.Fprintln(w)
fmt.Fprintln(w, l.Get("The price of 1g of gold (of 1000 millesimal fineness)"))
fmt.Fprintln(w)
fmt.Fprintln(w, l.Get("DATE \t PRICE (PLN)"))
fmt.Fprintln(w, l.Get("---- \t ----------- "))
for _, goldItem := range g.GoldRates {
total += goldItem.Price
goldValue := fmt.Sprintf("%.4f", goldItem.Price)
fmt.Fprintln(w, goldItem.Data+" \t "+goldValue)
}
if len(g.GoldRates) > 1 {
avg := total / float64(len(g.GoldRates))
fmt.Fprintln(w)
fmt.Fprintln(w, l.Get("The average of the last quotations is: "), fmt.Sprintf("%.4f", avg))
fmt.Fprintln(w)
}
w.Flush()
return builder.String()
}
/*
CreateCSVOutput - function returns prices of gold in CSV format
(comma separated data)
Parameters:
lang - 'en' or 'pl'
*/
func (g *NBPGold) CreateCSVOutput(lang string) string {
var output string = ""
// output language
setLang(lang)
output += fmt.Sprintln(l.Get("DATE,PRICE (PLN)"))
for _, goldItem := range g.GoldRates {
goldValue := fmt.Sprintf("%.4f", goldItem.Price)
output += fmt.Sprintln(goldItem.Data + "," + goldValue)
}
return output
}
// CreateRawOutput - function returns just result of request (json or xml)
func (g *NBPGold) CreateRawOutput() string {
return string(g.result)
}
// ------------------- private func -----------------------------------
/* getData - function that retrieves data from the NBP website
and returns them in the form of JSON / XML (or error), based on
the arguments provided:
url - NBP web api address
format - 'json' or 'xml'
*/
func (g *NBPGold) getData(url, format string) ([]byte, error) {
g.clearData()
return fetchData(g.Client, url, format)
}
// clearData - data cleaning
func (g *NBPGold) clearData() {
g.result = nil
g.GoldRates = nil
}
// getGoldAddress - build download address depending on previously
// verified input parameters (date or last)
func (g *NBPGold) getGoldAddress(date string, last int) string {
var address string
if last != 0 {
address = queryGoldLast(strconv.Itoa(last))
} else if date == "today" {
address = queryGoldToday()
} else if date == "current" {
address = queryGoldCurrent()
} else if len(date) == 10 {
address = queryGoldDate(date)
} else if len(date) == 21 {
address = queryGoldRange(date)
}
return address
}
// queryGoldToday - returns query: today's gold price
func queryGoldToday() string {
return fmt.Sprintf("%s/today", baseAddressGold)
}
// queryGoldCurrent - returns query: current gold price
// (last published price)
func queryGoldCurrent() string {
return baseAddressGold
}
// queryGoldLast - returns query: last <number> gold prices
func queryGoldLast(last string) string {
return fmt.Sprintf("%s/last/%s", baseAddressGold, last)
}
// queryGoldDay - function returns gold price on the given date (RRRR-MM-DD)
// in json/xml form, or error
func queryGoldDate(date string) string {
return fmt.Sprintf("%s/%s", baseAddressGold, date)
}
// queryGoldRange - returns query: gold prices within the given date range
// (RRRR-MM-DD:RRRR-MM-DD) in json/xml form, or error
func queryGoldRange(date string) string {
var startDate string
var stopDate string
temp := strings.Split(date, ":")
startDate = temp[0]
stopDate = temp[1]
address := fmt.Sprintf("%s/%s/%s", baseAddressGold, startDate, stopDate)
return address
}