Skip to content

Commit

Permalink
Clean-up
Browse files Browse the repository at this point in the history
  • Loading branch information
thoeni committed Apr 29, 2017
1 parent cba7f55 commit 3de5753
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 60 deletions.
2 changes: 1 addition & 1 deletion circle.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ test:
- go get golang.org/x/tools/cmd/cover
- go get github.com/mattn/goveralls
override:
- go test -v -covermode=count -coverprofile=coverage.out
- go test ./... -v -covermode=count -coverprofile=coverage.out
post:
- goveralls -coverprofile=coverage.out -service=circle-ci -repotoken $COVERALLS_REPO_TOKEN

Expand Down
24 changes: 5 additions & 19 deletions handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,15 @@ package main
import (
"encoding/json"
"fmt"
"log"
"net/http"
"strings"
"time"

"github.com/gorilla/mux"
"github.com/gorilla/schema"
"github.com/thoeni/go-tfl"
"github.com/thoeni/slack-tube-service/service"
"log"
"net/http"
"strings"
)

var client tfl.Client = &service.InMemoryCachedClient{
tfl.NewClient(),
[]tfl.Report{},
time.Now().Add(-121 * time.Second),
float64(120),
}
var tubeService = service.TubeService{client}
var tubeService = service.TubeService{tflClient}

func lineStatusHandler(w http.ResponseWriter, r *http.Request) {

Expand All @@ -29,14 +20,12 @@ func lineStatusHandler(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
tubeLine, _ := vars["line"]

log.Printf("Line requested: %s", tubeLine)
var lines []string
if tubeLine != "" {
lines = []string{tubeLine}
}

reportsMap, err := tubeService.GetStatusFor(lines)
log.Printf("Client address is %p", &(tubeService.Client))
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
if err := json.NewEncoder(w).Encode("There was an error getting information from TFL"); err != nil {
Expand All @@ -51,8 +40,6 @@ func lineStatusHandler(w http.ResponseWriter, r *http.Request) {
return
}

log.Printf("Status size is: %d", len(reportsMap))

w.WriteHeader(http.StatusOK)
if err := json.NewEncoder(w).Encode(reportsMap); err != nil {
log.Panic(err)
Expand Down Expand Up @@ -84,15 +71,14 @@ func slackRequestHandler(w http.ResponseWriter, r *http.Request) {

w.WriteHeader(http.StatusOK)
slackResp.ResponseType = "ephemeral"
slackResp.Text = fmt.Sprintf("Slack Tube Service - last updated at %s", lastStatusCheck.Format("15:04:05"))
slackResp.Text = fmt.Sprintf("Slack Tube Service")

var lines []string
if tubeLine != "" {
lines = []string{tubeLine}
}

reportsMap, err := tubeService.GetStatusFor(lines)
log.Printf("Client address is %p", &(tubeService.Client))

if err != nil {
w.WriteHeader(http.StatusInternalServerError)
Expand Down
31 changes: 0 additions & 31 deletions handlers_test.go

This file was deleted.

9 changes: 9 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import (

"github.com/boltdb/bolt"
"github.com/rs/cors"
"github.com/thoeni/go-tfl"
"github.com/thoeni/slack-tube-service/service"
)

var tokenStore tokenStorer
Expand All @@ -18,6 +20,13 @@ var listenPort = os.Getenv("PORT")

const defaultPort = "1123"

var tflClient = &service.InMemoryCachedClient{
tfl.NewClient(),
[]tfl.Report{},
time.Now().Add(-121 * time.Second),
float64(120),
}

func init() {

if listenPort == "" {
Expand Down
9 changes: 0 additions & 9 deletions service/tube-service.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package service

import (
"github.com/thoeni/go-tfl"
"log"
"strings"
"time"
)
Expand All @@ -19,15 +18,12 @@ type InMemoryCachedClient struct {
}

func (c *InMemoryCachedClient) GetTubeStatus() ([]tfl.Report, error) {
log.Printf("Called GetTubeStatus on InMemoryCachedClient!")
if time.Since(c.LastUpdated).Seconds() > c.InvalidateIntervalInSeconds {
log.Printf("Calling TFL...")
r, e := c.Client.GetTubeStatus()
c.TubeStatus = r
c.LastUpdated = time.Now()
return c.TubeStatus, e
}
log.Printf("Returning cached value...")
return c.TubeStatus, nil
}

Expand All @@ -40,16 +36,11 @@ func (s TubeService) GetStatusFor(lines []string) (map[string]tfl.Report, error)
if err != nil {
return nil, err
}
log.Printf("Retrieved %d reports!", len(reports))
reportsMap := tfl.ReportArrayToMap(reports)
log.Printf("Mapped %d out of %d reports retrieved.", len(reportsMap), len(reports))
log.Printf("Lines requested %d", len(lines))
if len(lines) == 0 {
log.Printf("Returning %d reports from the service.", len(reportsMap))
return reportsMap, nil
} else {
filteredReportsMap := filter(reportsMap, lines)
log.Printf("Returning %d filtered reports from the service.", len(filteredReportsMap))
return filteredReportsMap, nil
}
}
Expand Down

0 comments on commit 3de5753

Please sign in to comment.