Ejemplo n.º 1
0
func makeWeekBikeDurationsHandler(userToken auth.UserToken) func(http.ResponseWriter, *http.Request) {
	var countMiles = func(miles float32, workouts endpoint.JawboneResponseWorkouts) float32 {
		sortedItems := workouts.Data.Items
		sort.Sort(workoutByDateDesc(sortedItems))
		for _, workout := range sortedItems {
			if workout.SubType != 14 {
				continue
			}

			miles += workout.Details.DistanceKm
		}

		return miles
	}

	return func(resp http.ResponseWriter, req *http.Request) {
		startTime := time.Now()
		dayDiff := time.Sunday - startTime.Weekday()
		startTime = startTime.AddDate(0, 0, int(dayDiff))
		endTime := time.Now()
		workoutsRequest := endpoint.JawboneRequest{
			"@me",
			endpoint.JawboneWorkoutsEndpoint,
			url.Values{
				"start_time": {fmt.Sprintf("%d", startTime.Unix())},
				"end_time":   {fmt.Sprintf("%d", endTime.Unix())},
			},
			"",
		}

		workouts := endpoint.JawboneResponseWorkouts{}
		err := workoutsRequest.MakeRequest(userToken, &workouts, false)
		if err != nil {
			http.Error(resp, err.Error(), 500)
			return
		}

		miles := countMiles(0.0, workouts)
		if err != nil {
			http.Error(resp, err.Error(), 500)
			return
		}

		for workouts.HasNextPage() {
			workouts = endpoint.JawboneResponseWorkouts{}
			err = workoutsRequest.MakeRequest(userToken, &workouts, true)
			if err != nil {
				http.Error(resp, err.Error(), 500)
				return
			}

			miles = countMiles(miles, workouts)
			if err != nil {
				http.Error(resp, err.Error(), 500)
				return
			}
		}
		fmt.Fprintf(resp, "%f", miles)
	}
}
Ejemplo n.º 2
0
func makeWorkoutStreakHandler(userToken auth.UserToken) func(http.ResponseWriter, *http.Request) {
	var countStreak = func(streak int, lastDate time.Time, workouts endpoint.JawboneResponseWorkouts) (int, error) {
		sortedItems := workouts.Data.Items
		sort.Sort(workoutByDateDesc(sortedItems))
		for _, workout := range sortedItems {
			workoutDate, err := time.Parse("20060102", fmt.Sprintf("%d", workout.Date))
			if err != nil {
				return 0, err
			}

			if workout.SubType != 4 {
				continue
			}

			dateDiff := workoutDate.Sub(lastDate)
			lastDate = workoutDate

			if streak == 0 && dateDiff == 0 {
				streak = 1
				continue
			}

			if dateDiff < -24*time.Hour {
				break
			}

			streak++
		}

		return streak, nil
	}

	return func(resp http.ResponseWriter, req *http.Request) {
		workoutsRequest := endpoint.JawboneRequest{
			"@me",
			endpoint.JawboneWorkoutsEndpoint,
			url.Values{},
			"",
		}

		workouts := endpoint.JawboneResponseWorkouts{}
		err := workoutsRequest.MakeRequest(userToken, &workouts, false)
		if err != nil {
			http.Error(resp, err.Error(), 500)
			return
		}

		lastDate := time.Now().Truncate(24 * time.Hour)
		streak, err := countStreak(0, lastDate, workouts)
		if err != nil {
			http.Error(resp, err.Error(), 500)
			return
		}

		for workouts.HasNextPage() {
			workouts = endpoint.JawboneResponseWorkouts{}
			err = workoutsRequest.MakeRequest(userToken, &workouts, true)
			if err != nil {
				http.Error(resp, err.Error(), 500)
				return
			}

			streak, err = countStreak(streak, lastDate, workouts)
			if err != nil {
				http.Error(resp, err.Error(), 500)
				return
			}
		}
		fmt.Fprintf(resp, "%d", streak)
	}
}