Skip to content

Commit

Permalink
Merge pull request #6 from jdheyburn/add_single_fare_finder
Browse files Browse the repository at this point in the history
Add single fare finder
  • Loading branch information
jdheyburn authored Aug 13, 2020
2 parents d63a096 + d72a2c1 commit e3de962
Show file tree
Hide file tree
Showing 8 changed files with 1,167 additions and 3 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
# go-tflapi

Interface for querying TFL Unified API via golang

20 changes: 19 additions & 1 deletion client.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ const (
toPath string = "to"
stopPointPath string = "StopPoint"
searchPath string = "Search"
fareToPath string = "FareTo"
)

// Option is a functional option for configuring the API client
Expand Down Expand Up @@ -61,7 +62,8 @@ type Api interface {
SearchStopPoints(string) (*[]EntityMatchedStop, error)
SearchStopPointsWithModes(string, []string) (*[]EntityMatchedStop, error)
GetStopPointForID(string) (*StopPointAPIResponse, error)
GetJourneyPlannerItinerary(JourneyPlannerQuery) (*JourneyPlannerItineraryResult, error)
GetJourneyPlannerItinerary(JourneyPlannerQuery) (*JourneyPlannerItineraryResult, error)
SingleFareFinder(SingleFareFinderInput) (*FaresSection, error)
}

// Client holds information necessary to make a request to your API
Expand Down Expand Up @@ -217,3 +219,19 @@ func (c *TflClient) GetJourneyPlannerItinerary(query JourneyPlannerQuery) (*Jour

return &resp, nil
}

// SingleFareFinder retrieves a single fare cost between two stations
// It queries the endpoint /StopPoint/{from}/FareTo/{to}
func (c *TflClient) SingleFareFinder(input SingleFareFinderInput) (*[]FaresSection, error) {

pathParams := []string{stopPointPath, input.From, fareToPath, input.To}
queryParams := &map[string]string{}
url := c.buildURLWithQueryParams(pathParams, queryParams)

resp := []FaresSection{}
if err := c.getJSON(url, &resp); err != nil {
return nil, err
}

return &resp, nil
}
53 changes: 51 additions & 2 deletions client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import (
"path/filepath"
"reflect"
"testing"

"github.com/stretchr/testify/assert"
)

const (
Expand Down Expand Up @@ -50,6 +52,8 @@ func TflAPIClientStub() func() {
resp = getTestDataFileContents("Should_retrieve_no_matches_for_invalid_searchTerm.json")
case fmt.Sprintf("/Journey/JourneyResults/1001089/to/1000173?app_id=%s&app_key=%s&date=20190401&mode=%s&time=0715", appID, appKey, "national-rail%2Ctube"):
resp = getTestDataFileContents("Should_retrieve_journey_planner_itinerary_for_valid_search.json")
case fmt.Sprintf("/StopPoint/940GZZLUCYF/FareTo/910GPURLEYO?app_id=%s&app_key=%s", appID, appKey):
resp = getTestDataFileContents("single_fare_finder.json")
}

w.Write(resp)
Expand Down Expand Up @@ -253,7 +257,7 @@ func TestTflAPIClient_SearchStopPointsWithModes(t *testing.T) {
api: client,
args: args{
searchTerm: "London Bridge",
modes: []string{"national-rail", "tube"},
modes: []string{"national-rail", "tube"},
},
want: &expected,
},
Expand All @@ -272,7 +276,6 @@ func TestTflAPIClient_SearchStopPointsWithModes(t *testing.T) {
}
}


func TestTflAPIClient_GetJourneyPlannerItinerary(t *testing.T) {

expected := JourneyPlannerItineraryResult{}
Expand Down Expand Up @@ -316,3 +319,49 @@ func TestTflAPIClient_GetJourneyPlannerItinerary(t *testing.T) {
})
}
}

func TestTflClient_SingleFareFinder(t *testing.T) {

expected := []FaresSection{}
json.Unmarshal(getTestDataFileContents("single_fare_finder.json"), &expected)

type args struct {
input SingleFareFinderInput
}
tests := []struct {
name string
api *TflClient
args args
want *[]FaresSection
wantErr error
}{
{
name: "should retrieve single fare finder",
api: client,
args: args{
input: SingleFareFinderInput{
From: "940GZZLUCYF",
To: "910GPURLEYO",
},
},
want: &expected,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := tt.api.SingleFareFinder(tt.args.input)
if err != nil && tt.wantErr == nil {
assert.Fail(t, fmt.Sprintf(
"Error not expected but got one:\n"+
"error: %q", err),
)
return
}
if tt.wantErr != nil {
assert.EqualError(t, err, tt.wantErr.Error())
return
}
assert.Equal(t, tt.want, got)
})
}
}
5 changes: 5 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module go-tflapi

go 1.14

require github.com/stretchr/testify v1.6.1
10 changes: 10 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.6.1 h1:hDPOHmpOpP40lSULcqw7IrRb/u7w6RpDC9399XyoNd0=
github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
Loading

0 comments on commit e3de962

Please sign in to comment.