parse ntg jobs and strigify them

This commit is contained in:
Bel LaPointe
2022-01-09 21:56:01 -05:00
parent bf3c382877
commit ad9af0a5db
7 changed files with 236 additions and 2 deletions

81
broker/ntgvision.go Normal file
View File

@@ -0,0 +1,81 @@
package broker
import (
"encoding/json"
"errors"
"fmt"
"io"
"local/truckstop/config"
"time"
)
type NTGVision struct {
searcher interface {
search(states []config.State) (io.ReadCloser, error)
}
}
type ntgVisionJob struct {
ID int64 `json:"id"`
PickupDate string `json:"sDate"`
PickupCity string `json:"sCity"`
PickupState string `json:"sState"`
DropoffDate string `json:"cDate"`
DropoffCity string `json:"cCity"`
DropoffState string `json:"cState"`
Weight int `json:"weight"`
Equipment string `json:"equip"`
Temp string `json:"temp"`
}
func (ntgJob ntgVisionJob) Job() Job {
pickup, _ := time.ParseInLocation("01/02/06", ntgJob.PickupDate, time.Local)
dropoff, _ := time.ParseInLocation("01/02/06", ntgJob.DropoffDate, time.Local)
return Job{
ID: fmt.Sprintf("ntg-%d", ntgJob.ID),
Pickup: JobLocation{
Date: pickup,
City: ntgJob.PickupCity,
State: ntgJob.PickupState,
},
Dropoff: JobLocation{
Date: dropoff,
City: ntgJob.DropoffCity,
State: ntgJob.DropoffState,
},
Weight: ntgJob.Weight,
Meta: fmt.Sprintf("equipment:%s", ntgJob.Equipment),
}
}
func NewNTGVision() NTGVision {
ntgv := NTGVision{}
ntgv.searcher = ntgv
return ntgv
}
func (ntg NTGVision) WithMock() NTGVision {
ntg.searcher = NewNTGVisionMock()
return ntg
}
func (ntg NTGVision) search(states []config.State) (io.ReadCloser, error) {
return nil, errors.New("not impl: ntg.search")
}
func (ntg NTGVision) Search(states []config.State) ([]Job, error) {
rc, err := ntg.searcher.search(states)
if err != nil {
return nil, err
}
defer rc.Close()
var ntgjobs []ntgVisionJob
err = json.NewDecoder(rc).Decode(&ntgjobs)
jobs := make([]Job, len(ntgjobs))
for i := range jobs {
jobs[i] = ntgjobs[i].Job()
}
return jobs, err
}