Skip to content

Commit

Permalink
Merge pull request #6 from sammcewan/master
Browse files Browse the repository at this point in the history
Adding food scraps support + fix Dockerfile
  • Loading branch information
rusq authored Feb 4, 2024
2 parents 32d03d1 + 1ed9f4c commit 418f385
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 51 deletions.
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ RUN go build -ldflags="-s -w" ./cmd/aklapi
FROM alpine:3.17
LABEL maintainer="github:@rusq"

RUN apk add --no-cache ca-certificates
RUN apk add --no-cache ca-certificates && apk --no-cache add tzdata


WORKDIR /app
Expand Down
16 changes: 9 additions & 7 deletions cmd/aklapi/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,11 @@ import (
const dateFmt = "2006-01-02"

type rrResponse struct {
Rubbish string `json:"rubbish,omitempty"`
Recycle string `json:"recycle,omitempty"`
Address string `json:"address,omitempty"`
Error string `json:"error,omitempty"`
Rubbish string `json:"rubbish,omitempty"`
Recycle string `json:"recycle,omitempty"`
FoodScraps string `json:"foodscraps,omitempty"`
Address string `json:"address,omitempty"`
Error string `json:"error,omitempty"`
}

func respond(w http.ResponseWriter, data interface{}, code int) {
Expand Down Expand Up @@ -58,9 +59,10 @@ func rrHandler(w http.ResponseWriter, r *http.Request) {
return
}
resp := rrResponse{
Recycle: res.NextRecycle().Format(dateFmt),
Rubbish: res.NextRubbish().Format(dateFmt),
Address: res.Address.Address,
Recycle: res.NextRecycle().Format(dateFmt),
Rubbish: res.NextRubbish().Format(dateFmt),
FoodScraps: res.NextFoodScraps().Format(dateFmt),
Address: res.Address.Address,
}
respond(w, resp, http.StatusOK)
}
Expand Down
41 changes: 24 additions & 17 deletions rubbish.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,11 @@ var errSkip = errors.New("skip this date")

// RubbishCollection contains the date and type of collection.
type RubbishCollection struct {
Day string
Date time.Time
Rubbish bool
Recycle bool
Day string
Date time.Time
Rubbish bool
Recycle bool
FoodScraps bool
}

// CollectionDayDetailResult contains the information about Rubbish and
Expand Down Expand Up @@ -60,6 +61,16 @@ func (res *CollectionDayDetailResult) NextRecycle() time.Time {
return time.Time{}
}

// NextFoodScraps returns the next food scraps collection date.
func (res *CollectionDayDetailResult) NextFoodScraps() time.Time {
for _, r := range res.Collections {
if r.FoodScraps {
return r.Date
}
}
return time.Time{}
}

// CollectionDayDetail returns a collection day details for the specified
// address as reported by the Auckland Council Website.
func CollectionDayDetail(addr string) (*CollectionDayDetailResult, error) {
Expand Down Expand Up @@ -143,26 +154,22 @@ func (p *refuseParser) parse(r io.Reader) ([]RubbishCollection, error) {
func (p *refuseParser) parseLinks(el int, sel *goquery.Selection) {
sel.Children().Each(func(n int, sel *goquery.Selection) {
switch n {
default:
p.Err = fmt.Errorf("parse error: sel.Text = %q, el = %d, n = %d", sel.Text(), el, n)
case 0:
if dow.FindString(sel.Text()) == "" {
log.Println("unable to detect day of week")
return
}
p.detail[el].Day = sel.Text()
case 1:
if sel.Text() != "Rubbish" {
p.Err = fmt.Errorf("unknown text in rubbish block: %s", sel.Text())
return
}
p.detail[el].Rubbish = true
case 2:
if sel.Text() != "Recycle" {
p.Err = fmt.Errorf("unknown text in rubbish block: %s", sel.Text())
return
default:
if sel.Text() == "Rubbish" {
p.detail[el].Rubbish = true
} else if sel.Text() == "Food scraps" {
p.detail[el].FoodScraps = true
} else if sel.Text() == "Recycle" {
p.detail[el].Recycle = true
} else {
p.Err = fmt.Errorf("parse error: sel.Text = %q, el = %d, n = %d", sel.Text(), el, n)
}
p.detail[el].Recycle = true
}
})
}
Expand Down
60 changes: 34 additions & 26 deletions rubbish_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,15 @@ func Test_parse(t *testing.T) {
&CollectionDayDetailResult{
Collections: []RubbishCollection{
{Day: "Tuesday 11 February",
Date: adjustYear(time.Date(0, 02, 11, 0, 0, 0, 0, defaultLoc)),
Rubbish: true,
Recycle: true},
Date: adjustYear(time.Date(0, 02, 11, 0, 0, 0, 0, defaultLoc)),
Rubbish: true,
Recycle: true,
FoodScraps: false},
{Day: "Tuesday 18 February",
Date: adjustYear(time.Date(0, 02, 18, 0, 0, 0, 0, defaultLoc)),
Rubbish: true,
Recycle: false},
Date: adjustYear(time.Date(0, 02, 18, 0, 0, 0, 0, defaultLoc)),
Rubbish: true,
Recycle: false,
FoodScraps: false},
},
Address: nil,
},
Expand Down Expand Up @@ -78,14 +80,16 @@ func TestCollectionDayDetailResult_NextRubbish(t *testing.T) {
fields{
Collections: []RubbishCollection{
{Day: "Someday",
Date: time.Date(2000, 2, 1, 0, 0, 0, 0, defaultLoc),
Rubbish: false,
Recycle: true,
Date: time.Date(2000, 2, 1, 0, 0, 0, 0, defaultLoc),
Rubbish: false,
Recycle: true,
FoodScraps: false,
},
{Day: "This is the day",
Date: time.Date(1991, 9, 16, 0, 0, 0, 0, defaultLoc),
Rubbish: true,
Recycle: false,
Date: time.Date(1991, 9, 16, 0, 0, 0, 0, defaultLoc),
Rubbish: true,
Recycle: false,
FoodScraps: false,
},
},
},
Expand Down Expand Up @@ -120,14 +124,16 @@ func TestCollectionDayDetailResult_NextRecycle(t *testing.T) {
fields{
Collections: []RubbishCollection{
{Day: "Someday",
Date: time.Date(2000, 2, 1, 0, 0, 0, 0, defaultLoc),
Rubbish: true,
Recycle: false,
Date: time.Date(2000, 2, 1, 0, 0, 0, 0, defaultLoc),
Rubbish: true,
Recycle: false,
FoodScraps: false,
},
{Day: "This is the day",
Date: time.Date(1991, 9, 16, 0, 0, 0, 0, defaultLoc),
Rubbish: true,
Recycle: true,
Date: time.Date(1991, 9, 16, 0, 0, 0, 0, defaultLoc),
Rubbish: true,
Recycle: true,
FoodScraps: false,
},
},
},
Expand Down Expand Up @@ -195,16 +201,18 @@ func TestCollectionDayDetail(t *testing.T) {
&CollectionDayDetailResult{
Collections: []RubbishCollection{
{
Day: "Tuesday 11 February",
Date: adjustYear(time.Date(0, 2, 11, 0, 0, 0, 0, defaultLoc)),
Rubbish: true,
Recycle: true,
Day: "Tuesday 11 February",
Date: adjustYear(time.Date(0, 2, 11, 0, 0, 0, 0, defaultLoc)),
Rubbish: true,
Recycle: true,
FoodScraps: false,
},
{
Day: "Tuesday 18 February",
Date: adjustYear(time.Date(0, 2, 18, 0, 0, 0, 0, defaultLoc)),
Rubbish: true,
Recycle: false,
Day: "Tuesday 18 February",
Date: adjustYear(time.Date(0, 2, 18, 0, 0, 0, 0, defaultLoc)),
Rubbish: true,
Recycle: false,
FoodScraps: false,
},
},
Address: &Address{
Expand Down

0 comments on commit 418f385

Please sign in to comment.