Skip to content

Commit

Permalink
add json backend
Browse files Browse the repository at this point in the history
  • Loading branch information
schachmat committed Apr 1, 2016
1 parent 00299f5 commit b96c85d
Showing 1 changed file with 40 additions and 0 deletions.
40 changes: 40 additions & 0 deletions backends/json.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package backends

import (
"encoding/json"
"io/ioutil"
"log"

"github.com/schachmat/wego/iface"
)

type jsnConfig struct {
}

func (c *jsnConfig) Setup() {
}

// Fetch will try to open the file specified in the location string argument and
// read it as json content to fill the data. The numdays argument will only work
// to further limit the amount of days in the output. It obviously cannot
// produce more data than is available in the file.
func (c *jsnConfig) Fetch(loc string, numdays int) (ret iface.Data) {
b, err := ioutil.ReadFile(loc)
if err != nil {
log.Fatal(err)
}

err = json.Unmarshal(b, &ret)
if err != nil {
log.Fatal(err)
}

if len(ret.Forecast) > numdays {
ret.Forecast = ret.Forecast[:numdays]
}
return
}

func init() {
iface.AllBackends["json"] = &jsnConfig{}
}

0 comments on commit b96c85d

Please sign in to comment.