Skip to content
This repository has been archived by the owner on Feb 14, 2023. It is now read-only.

Commit

Permalink
Starred Segments Update
Browse files Browse the repository at this point in the history
Add view any athletes and add effort PR data
  • Loading branch information
paulmach committed Jul 25, 2014
1 parent 3b03067 commit 17e909e
Show file tree
Hide file tree
Showing 10 changed files with 166 additions and 19 deletions.
45 changes: 45 additions & 0 deletions athletes.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,51 @@ func (c *AthletesGetCall) Do() (*AthleteSummary, error) {

/*********************************************************/

type AthletesListStarredSegmentsCall struct {
service *AthletesService
id int64
ops map[string]interface{}
}

func (s *AthletesService) ListStarredSegments(athleteId int64) *AthletesListStarredSegmentsCall {
return &AthletesListStarredSegmentsCall{
service: s,
id: athleteId,
ops: make(map[string]interface{}),
}
}

func (c *AthletesListStarredSegmentsCall) Page(page int) *AthletesListStarredSegmentsCall {
c.ops["page"] = page
return c
}

func (c *AthletesListStarredSegmentsCall) PerPage(perPage int) *AthletesListStarredSegmentsCall {
c.ops["per_page"] = perPage
return c
}

func (c *AthletesListStarredSegmentsCall) Do() ([]*PersonalSegmentSummary, error) {
data, err := c.service.client.run("GET", fmt.Sprintf("/athletes/%d/segments/starred", c.id), c.ops)
if err != nil {
return nil, err
}

segments := make([]*PersonalSegmentSummary, 0)
err = json.Unmarshal(data, &segments)
if err != nil {
return nil, err
}

for _, s := range segments {
s.postProcess()
}

return segments, nil
}

/*********************************************************/

type AthletesListFriendsCall struct {
service *AthletesService
id int64
Expand Down
64 changes: 64 additions & 0 deletions athletes_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,65 @@ func TestAthletesGet(t *testing.T) {
}
}

func TestCAthletesListStarredSegments(t *testing.T) {
client := newCassetteClient(testToken, "athlete_list_starred_segments")
segments, err := NewAthletesService(client).ListStarredSegments(3545423).Do()

if err != nil {
t.Fatalf("service error: %v", err)
}

expected := &PersonalSegmentSummary{}

expected.Id = 229781
expected.Name = "Hawk Hill"
expected.ActivityType = ActivityTypes.Ride
expected.Distance = 2684.82
expected.AverageGrade = 5.7
expected.MaximumGrade = 14.2
expected.ElevationHigh = 245.3
expected.ElevationLow = 92.4
expected.StartLocation = Location{37.8331119, -122.4834356}
expected.EndLocation = Location{37.8280722, -122.4981393}
expected.ClimbCategory = ClimbCategories.Category4
expected.City = "San Francisco"
expected.State = "CA"
expected.Country = "United States"
expected.Private = false
expected.Starred = true

expected.AthletePR.Id = 3439333050
expected.AthletePR.ElapsedTime = 550
expected.AthletePR.Distance = 2713.4

expected.AthletePR.StartDateString = "2013-01-21T19:05:07Z"
expected.AthletePR.StartDateLocalString = "2013-01-21T11:05:07Z"
expected.AthletePR.StartDate, _ = time.Parse(timeFormat, expected.AthletePR.StartDateString)
expected.AthletePR.StartDateLocal, _ = time.Parse(timeFormat, expected.AthletePR.StartDateLocalString)

expected.StarredDateString = "2014-07-24T23:23:24Z"
expected.StarredDate, _ = time.Parse(timeFormat, expected.StarredDateString)

if !reflect.DeepEqual(segments[0], expected) {
t.Errorf("should match\n%v\n%v", segments[0], expected)
}

// from here on out just check the request parameters
s := NewAthletesService(newStoreRequestClient())

// path
s.ListStarredSegments(123).Page(2).PerPage(3).Do()

transport := s.client.httpClient.Transport.(*storeRequestTransport)
if transport.request.URL.Path != "/api/v3/athletes/123/segments/starred" {
t.Errorf("request path incorrect, got %v", transport.request.URL.Path)
}

if transport.request.URL.RawQuery != "page=2&per_page=3" {
t.Errorf("request query incorrect, got %v", transport.request.URL.RawQuery)
}
}

func TestAthletesListFriends(t *testing.T) {
client := newCassetteClient(testToken, "athlete_list_friends")
friends, err := NewAthletesService(client).ListFriends(3545423).Do()
Expand Down Expand Up @@ -246,6 +305,11 @@ func TestAthletesBadJSON(t *testing.T) {
t.Error("should return a bad json error")
}

_, err = s.ListStarredSegments(123).Do()
if err == nil {
t.Error("should return a bad json error")
}

_, err = s.ListFriends(123).Do()
if err == nil {
t.Error("should return a bad json error")
Expand Down
1 change: 1 addition & 0 deletions cassettes/athlete_list_starred_segments.body
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[{"id":229781,"resource_state":2,"name":"Hawk Hill","activity_type":"Ride","distance":2684.82,"average_grade":5.7,"maximum_grade":14.2,"elevation_high":245.3,"elevation_low":92.4,"start_latlng":[37.8331119,-122.4834356],"end_latlng":[37.8280722,-122.4981393],"start_latitude":37.8331119,"start_longitude":-122.4834356,"end_latitude":37.8280722,"end_longitude":-122.4981393,"climb_category":1,"city":"San Francisco","state":"CA","country":"United States","private":false,"starred":true,"pr_time":550,"athlete_pr_effort":{"id":3439333050,"elapsed_time":550,"distance":2713.4,"start_date":"2013-01-21T19:05:07Z","start_date_local":"2013-01-21T11:05:07Z","is_kom":false},"starred_date":"2014-07-24T23:23:24Z"}]
1 change: 1 addition & 0 deletions cassettes/athlete_list_starred_segments.resp

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion cassettes/current_athlete_list_starred_segments.body
Original file line number Diff line number Diff line change
@@ -1 +1 @@
[{"id":5775164,"resource_state":2,"name":"DBC Junior Airport loop sprint orange flag","activity_type":"Ride","distance":809.9,"average_grade":-0.0,"maximum_grade":0.6,"elevation_high":28.3,"elevation_low":27.7,"start_latlng":[38.58258,-121.851906],"end_latlng":[38.589291,-121.854774],"start_latitude":38.58258,"start_longitude":-121.851906,"end_latitude":38.589291,"end_longitude":-121.854774,"climb_category":0,"city":"Davis","state":"CA","country":"United States","private":false,"pr_time":113,"pr_distance":805.6}]
[{"id":229781,"resource_state":2,"name":"Hawk Hill","activity_type":"Ride","distance":2684.82,"average_grade":5.7,"maximum_grade":14.2,"elevation_high":245.3,"elevation_low":92.4,"start_latlng":[37.8331119,-122.4834356],"end_latlng":[37.8280722,-122.4981393],"start_latitude":37.8331119,"start_longitude":-122.4834356,"end_latitude":37.8280722,"end_longitude":-122.4981393,"climb_category":1,"city":"San Francisco","state":"CA","country":"United States","private":false,"starred":true,"pr_time":550,"athlete_pr_effort":{"id":3439333050,"elapsed_time":550,"distance":2713.4,"start_date":"2013-01-21T19:05:07Z","start_date_local":"2013-01-21T11:05:07Z","is_kom":false},"starred_date":"2014-07-24T23:23:24Z"}]
2 changes: 1 addition & 1 deletion cassettes/current_athlete_list_starred_segments.resp

Large diffs are not rendered by default.

20 changes: 16 additions & 4 deletions current_athlete.go
Original file line number Diff line number Diff line change
Expand Up @@ -271,28 +271,40 @@ func (c *CurrentAthleteListClubsCall) Do() ([]*ClubSummary, error) {

type CurrentAthleteListStarredSegmentsCall struct {
service *CurrentAthleteService
ops map[string]interface{}
}

func (s *CurrentAthleteService) ListStarredSegments() *CurrentAthleteListStarredSegmentsCall {
return &CurrentAthleteListStarredSegmentsCall{
service: s,
ops: make(map[string]interface{}),
}
}

func (c *CurrentAthleteListStarredSegmentsCall) Do() ([]*SegmentSummary, error) {
data, err := c.service.client.run("GET", "/segments/starred", nil)
func (c *CurrentAthleteListStarredSegmentsCall) Page(page int) *CurrentAthleteListStarredSegmentsCall {
c.ops["page"] = page
return c
}

func (c *CurrentAthleteListStarredSegmentsCall) PerPage(perPage int) *CurrentAthleteListStarredSegmentsCall {
c.ops["per_page"] = perPage
return c
}

func (c *CurrentAthleteListStarredSegmentsCall) Do() ([]*PersonalSegmentSummary, error) {
data, err := c.service.client.run("GET", "/segments/starred", c.ops)
if err != nil {
return nil, err
}

segments := make([]*SegmentSummary, 0)
segments := make([]*PersonalSegmentSummary, 0)
err = json.Unmarshal(data, &segments)
if err != nil {
return nil, err
}

for _, c := range segments {
c.postProcessSummary()
c.postProcess()
}

return segments, nil
Expand Down
4 changes: 2 additions & 2 deletions current_athlete_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -278,14 +278,14 @@ func TestCurrentAthleteListStarredSegments(t *testing.T) {
s := NewCurrentAthleteService(newStoreRequestClient())

// path
s.ListStarredSegments().Do()
s.ListStarredSegments().Page(1).PerPage(2).Do()

transport := s.client.httpClient.Transport.(*storeRequestTransport)
if transport.request.URL.Path != "/api/v3/segments/starred" {
t.Errorf("request path incorrect, got %v", transport.request.URL.Path)
}

if transport.request.URL.RawQuery != "" {
if transport.request.URL.RawQuery != "page=1&per_page=2" {
t.Errorf("request query incorrect, got %v", transport.request.URL.RawQuery)
}
}
Expand Down
2 changes: 1 addition & 1 deletion segment_efforts.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ func (e *SegmentEffortDetailed) postProcessDetailed() {
}

func (e *SegmentEffortSummary) postProcessSummary() {
e.Segment.postProcessSummary()
e.Segment.postProcess()

e.StartDate, _ = time.Parse(timeFormat, e.StartDateString)
e.StartDateLocal, _ = time.Parse(timeFormat, e.StartDateLocalString)
Expand Down
44 changes: 34 additions & 10 deletions segments.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,22 @@ type SegmentSummary struct {
Starred bool `json:"starred"`
}

type PersonalSegmentSummary struct {
SegmentSummary
AthletePR struct {
Id int64 `json:"id"`
ElapsedTime int `json:"elapsed_time"`
Distance float64 `json:"distance"`
StartDate time.Time `json:"-"`
StartDateLocal time.Time `json:"-"`
StartDateString string `json:"start_date"`
StartDateLocalString string `json:"start_date_local"`
IsKOM bool `json:"is_kom"`
} `json:"athlete_pr_effort"`
StarredDate time.Time `json:"-"`
StarredDateString string `json:"starred_date"`
}

type SegmentLeaderboard struct {
EffortCount int `json:"effort_count"`
EntryCount int `json:"entry_count"`
Expand Down Expand Up @@ -146,10 +162,10 @@ func NewSegmentsService(client *Client) *SegmentsService {

type SegmentsGetCall struct {
service *SegmentsService
id int
id int64
}

func (s *SegmentsService) Get(segmentId int) *SegmentsGetCall {
func (s *SegmentsService) Get(segmentId int64) *SegmentsGetCall {
return &SegmentsGetCall{
service: s,
id: segmentId,
Expand All @@ -168,7 +184,7 @@ func (s *SegmentsGetCall) Do() (*SegmentDetailed, error) {
return nil, err
}

segment.postProcessDetailed()
segment.postProcess()

return &segment, nil
}
Expand All @@ -177,11 +193,11 @@ func (s *SegmentsGetCall) Do() (*SegmentDetailed, error) {

type SegmentsListEffortsCall struct {
service *SegmentsService
id int
id int64
ops map[string]interface{}
}

func (s *SegmentsService) ListEfforts(segmentId int) *SegmentsListEffortsCall {
func (s *SegmentsService) ListEfforts(segmentId int64) *SegmentsListEffortsCall {
return &SegmentsListEffortsCall{
service: s,
id: segmentId,
Expand Down Expand Up @@ -233,11 +249,11 @@ func (c *SegmentsListEffortsCall) Do() ([]*SegmentEffortSummary, error) {

type SegmentsGetLeaderboardCall struct {
service *SegmentsService
id int
id int64
ops map[string]interface{}
}

func (s *SegmentsService) GetLeaderboard(segmentId int) *SegmentsGetLeaderboardCall {
func (s *SegmentsService) GetLeaderboard(segmentId int64) *SegmentsGetLeaderboardCall {
return &SegmentsGetLeaderboardCall{
service: s,
id: segmentId,
Expand Down Expand Up @@ -358,14 +374,22 @@ func (c *SegmentsExplorerCall) Do() ([]*SegmentExplorerSegment, error) {

/*********************************************************/

func (s *SegmentDetailed) postProcessDetailed() {
func (s *PersonalSegmentSummary) postProcess() {
s.AthletePR.StartDate, _ = time.Parse(timeFormat, s.AthletePR.StartDateString)
s.AthletePR.StartDateLocal, _ = time.Parse(timeFormat, s.AthletePR.StartDateLocalString)
s.StarredDate, _ = time.Parse(timeFormat, s.StarredDateString)

s.SegmentSummary.postProcess()
}

func (s *SegmentDetailed) postProcess() {
s.CreatedAt, _ = time.Parse(timeFormat, s.CreatedAtString)
s.UpdatedAt, _ = time.Parse(timeFormat, s.UpdatedAtString)

s.postProcessSummary()
s.SegmentSummary.postProcess()
}

func (s *SegmentSummary) postProcessSummary() {
func (s *SegmentSummary) postProcess() {

}

Expand Down

0 comments on commit 17e909e

Please sign in to comment.