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

Implement string #25

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions activities.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package strava
import (
"encoding/json"
"fmt"
"reflect"
"strings"
"time"
)

Expand Down Expand Up @@ -539,3 +541,27 @@ func (t ActivityType) String() string {
func (l Location) String() string {
return fmt.Sprintf("[%f, %f]", l[0], l[1])
}

func (a ActivitySummary) String() string {
template := ""
aValue := reflect.ValueOf(a)
typeOfA := aValue.Type()

for i := 0; i < aValue.NumField(); i++ {
line := ""
fieldName := typeOfA.Field(i).Name
fieldValue := aValue.Field(i).Interface()
if fieldName == "Map" {
continue
}
if fieldName == "Athlete" {
fieldValueString := strings.TrimSuffix(fmt.Sprintf("%v", fieldValue), "\n")
line = fmt.Sprintf("%s : {\n%v\n}\n", fieldName, fieldValueString)
} else {
line = fmt.Sprintf("%s : %v\n", fieldName, fieldValue)
}
template = template + line
}

return fmt.Sprintf(template)
}
140 changes: 140 additions & 0 deletions activities_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -829,3 +829,143 @@ func TestActivityType(t *testing.T) {
t.Errorf("activity type string incorrect, got %v", s)
}
}

func TestActivitySummaryString(t *testing.T) {
activity := new(ActivitySummary)
expected := `Id : 0
ExternalId :
UploadId : 0
Athlete : {
AthleteMeta : {0}
FirstName :
LastName :
ProfileMedium :
Profile :
City :
State :
Country :
Gender :
Friend :
Follower :
Premium : false
CreatedAt : 0001-01-01 00:00:00 +0000 UTC
UpdatedAt : 0001-01-01 00:00:00 +0000 UTC
ApproveFollowers : false
BadgeTypeId : 0
}
Name :
Distance : 0
MovingTime : 0
ElapsedTime : 0
TotalElevationGain : 0
Type : Activity
StartDate : 0001-01-01 00:00:00 +0000 UTC
StartDateLocal : 0001-01-01 00:00:00 +0000 UTC
TimeZone :
StartLocation : [0.000000, 0.000000]
EndLocation : [0.000000, 0.000000]
City :
State :
Country :
AchievementCount : 0
KudosCount : 0
CommentCount : 0
AthleteCount : 0
PhotoCount : 0
Trainer : false
Commute : false
Manual : false
Private : false
Flagged : false
GearId :
AverageSpeed : 0
MaximunSpeed : 0
AverageCadence : 0
AverageTemperature : 0
AveragePower : 0
WeightedAveragePower : 0
Kilojoules : 0
DeviceWatts : false
AverageHeartrate : 0
MaximumHeartrate : 0
Truncated : 0
HasKudoed : false
`

if activity.String() != expected {
t.Errorf("activity string incorrect, got:\n%v\nexpected\n%v", activity, expected)
}

client := newCassetteClient(testToken, "activity_get")
activityDetailed, err := NewActivitiesService(client).Get(103221154).Do()

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

activity = &activityDetailed.ActivitySummary
expected = `Id : 103221154
ExternalId : 2010-08-15-11-04-29.fit
UploadId : 112859609
Athlete : {
AthleteMeta : {227615}
FirstName : John
LastName : Applestrava
ProfileMedium : http://dgalywyr863hv.cloudfront.net/pictures/athletes/227615/41555/3/medium.jpg
Profile : http://dgalywyr863hv.cloudfront.net/pictures/athletes/227615/41555/3/large.jpg
City : San Francisco
State : CA
Country : United States
Gender : M
Friend : accepted
Follower : accepted
Premium : true
CreatedAt : 2012-01-18 18:20:37 +0000 UTC
UpdatedAt : 2014-01-21 06:23:32 +0000 UTC
ApproveFollowers : false
BadgeTypeId : 0
}
Name : 08/15/2010 Davis, CA
Distance : 20739.1
MovingTime : 2836
ElapsedTime : 3935
TotalElevationGain : 22
Type : Ride
StartDate : 2010-08-15 18:04:29 +0000 UTC
StartDateLocal : 2010-08-15 11:04:29 +0000 UTC
TimeZone :
StartLocation : [38.550000, -121.820000]
EndLocation : [38.560000, -121.780000]
City : Davis
State : CA
Country : United States
AchievementCount : 0
KudosCount : 1
CommentCount : 1
AthleteCount : 2
PhotoCount : 0
Trainer : false
Commute : true
Manual : false
Private : false
Flagged : false
GearId : b77076
AverageSpeed : 7.313
MaximunSpeed : 13.7
AverageCadence : 73.2
AverageTemperature : 27
AveragePower : 140.2
WeightedAveragePower : 202
Kilojoules : 397.5
DeviceWatts : true
AverageHeartrate : 104.4
MaximumHeartrate : 147
Truncated : 0
HasKudoed : false
`

if activity.String() != expected {
t.Errorf("activity string incorrect, got:\n%v\nexpected\n%v", activity, expected)
}

}
16 changes: 16 additions & 0 deletions athletes.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package strava
import (
"encoding/json"
"fmt"
"reflect"
"time"
)

Expand Down Expand Up @@ -40,6 +41,21 @@ type AthleteSummary struct {
BadgeTypeId int `json:"badge_type_id"`
}

func (a AthleteSummary) String() string {
template := ""
aValue := reflect.ValueOf(a)
typeOfA := aValue.Type()

for i := 0; i < aValue.NumField(); i++ {
fieldName := typeOfA.Field(i).Name
fieldValue := aValue.Field(i).Interface()
line := fmt.Sprintf("%s : %v\n", fieldName, fieldValue)
template = template + line
}

return fmt.Sprintf(template)
}

type AthleteMeta struct {
Id int64 `json:"id"`
}
Expand Down
55 changes: 55 additions & 0 deletions athletes_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -385,3 +385,58 @@ func TestAthletesBadJSON(t *testing.T) {
t.Error("should return a bad json error")
}
}

func TestAthleteSummaryString(t *testing.T) {
athlete := new(AthleteSummary)
expected := `AthleteMeta : {0}
FirstName :
LastName :
ProfileMedium :
Profile :
City :
State :
Country :
Gender :
Friend :
Follower :
Premium : false
CreatedAt : 0001-01-01 00:00:00 +0000 UTC
UpdatedAt : 0001-01-01 00:00:00 +0000 UTC
ApproveFollowers : false
BadgeTypeId : 0
`

if athlete.String() != expected {
t.Errorf("athlete summary incorrect, got:\n%v\nexpected\n%v", athlete, expected)
}

client := newCassetteClient(testToken, "athlete_get")
athlete, err := NewAthletesService(client).Get(3545423).Do()

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

expected = `AthleteMeta : {3545423}
FirstName : Strava
LastName : Testing
ProfileMedium : avatar/athlete/medium.png
Profile : avatar/athlete/large.png
City : Palo Alto
State : CA
Country : United States
Gender : M
Friend : accepted
Follower : accepted
Premium : false
CreatedAt : 2013-12-26 19:19:36 +0000 UTC
UpdatedAt : 2014-01-12 00:20:58 +0000 UTC
ApproveFollowers : false
BadgeTypeId : 0
`

if athlete.String() != expected {
t.Errorf("athlete summary string incorrect, got:\n%v\nexpected\n%v", athlete, expected)
}

}
16 changes: 16 additions & 0 deletions segments.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package strava
import (
"encoding/json"
"fmt"
"reflect"
"time"
)

Expand Down Expand Up @@ -42,6 +43,21 @@ type SegmentSummary struct {
Starred bool `json:"starred"`
}

func (s SegmentSummary) String() string {
template := ""
sValue := reflect.ValueOf(s)
typeOfS := sValue.Type()

for i := 0; i < sValue.NumField(); i++ {
fieldName := typeOfS.Field(i).Name
fieldValue := sValue.Field(i).Interface()
line := fmt.Sprintf("%s : %v\n", fieldName, fieldValue)
template = template + line
}

return fmt.Sprintf(template)
}

type PersonalSegmentSummary struct {
SegmentSummary
AthletePR struct {
Expand Down
55 changes: 55 additions & 0 deletions segments_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -378,3 +378,58 @@ func TestClimbCategory(t *testing.T) {
t.Errorf("climb category string incorrect, got %v", s)
}
}

func TestSegmentSummaryString(t *testing.T) {
segment := new(SegmentSummary)
expected := `Id : 0
Name :
ActivityType : Activity
Distance : 0
AverageGrade : 0
MaximumGrade : 0
ElevationHigh : 0
ElevationLow : 0
ClimbCategory : Not Categorized
StartLocation : [0.000000, 0.000000]
EndLocation : [0.000000, 0.000000]
City :
State :
Country :
Private : false
Starred : false
`

if segment.String() != expected {
t.Errorf("segment summary string incorrect, got:\n%v\nexpected\n%v", segment, expected)
}

client := newCassetteClient(testToken, "segment_get")
segmentDetailed, err := NewSegmentsService(client).Get(229781).Do()
segment = &segmentDetailed.SegmentSummary

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

expected = `Id : 229781
Name : Hawk Hill
ActivityType : Ride
Distance : 2684.82
AverageGrade : 5.7
MaximumGrade : 14.2
ElevationHigh : 245.3
ElevationLow : 92.4
ClimbCategory : Category 4
StartLocation : [37.833112, -122.483436]
EndLocation : [37.828072, -122.498139]
City : San Francisco
State : CA
Country : United States
Private : false
Starred : false
`

if segment.String() != expected {
t.Errorf("segment summary string incorrect, got:\n%v\nexpected\n%v", segment, expected)
}
}