Updated API & Rides to be more useful

This commit is contained in:
Justin Foell
2013-04-07 17:56:07 -05:00
parent 380b73b7c7
commit 52da31b091
8 changed files with 149 additions and 148 deletions
+49 -83
View File
@@ -3,99 +3,65 @@
* Rides is a class wrapper for the Strava REST API functions.
*/
class WPStrava_Rides {
public $stravaRides;
public $feedback;
public function getRideDetails($rideId, $systemOfMeasurement) {
$url = preg_replace('/:id/', $rideId, $this->rideUrl);
$json = file_get_contents($url);
if($json) {
$strava_ride = json_decode($json);
//Transform data to a ready to be displayed format
$startDate = date("F j, Y - H:i a", strtotime($strava_ride->ride->startDateLocal));
$elapsedTime = date("H:i:s", mktime(0, 0, $strava_ride->ride->elapsedTime));
$movingTime = date("H:i:s", mktime(0, 0, $strava_ride->ride->movingTime));
if ($systemOfMeasurement == "metric") {
//To km
$distance = number_format($strava_ride->ride->distance/1000, 2);
//To km/h
$averageSpeed = number_format($strava_ride->ride->averageSpeed * 3.6, 2);
//To km/h
// Removed on version 2 of the Strava API
//$maximumSpeed = number_format($strava_ride->ride->maximumSpeed/1000, 2);
//It is already in meters
$elevationGain = number_format($strava_ride->ride->elevationGain, 2);
} elseif ($systemOfMeasurement == "english") {
//To miles
$distance = number_format($strava_ride->ride->distance/1609.34, 2);
//To miles/h
$averageSpeed = number_format($strava_ride->ride->averageSpeed * 2.2369, 2);
//To miles/h
// Removed on version 2 of the Strava API
//$maximumSpeed = number_format($strava_ride->ride->maximumSpeed/1609.34, 2);
//To foot
$elevationGain = number_format($strava_ride->ride->elevationGain/0.3048, 2);
}
$ride_details = array(
'id' => $rideId,
'name' => $strava_ride->ride->name,
'athleteId' => $strava_ride->ride->athlete->id,
'athleteName' => $strava_ride->ride->athlete->name,
'athleteUserName' => $strava_ride->ride->athlete->username,
'startDate' => $startDate,
'elapsedTime' => $elapsedTime,
'movingTime' => $movingTime,
'distance' => $distance,
'averageSpeed' => $averageSpeed,
//'maximumSpeed' => $maximumSpeed,
'elevationGain' => $elevationGain
);
return $ride_details;
} else {
$this->feedback .= _e("Could not get information from strava.com for the ride id: ") . $stravaRide->id;
return false;
}
} // getRideDetails
const RIDES_URL = "http://app.strava.com/rides/";
const ATHLETES_URL = "http://app.strava.com/athletes/";
public function getRidesDetails($systemOfMeasurement) {
if($this->stravaRides) {
$rides_details = array();
foreach($this->stravaRides as $stravaRide) {
$rides_details[] = $this->getRideDetails($stravaRide->id, $systemOfMeasurement);
}
return $rides_details;
} else {
$this->feedback .= _e("Please provide the rides array to be processed.", "wp-strava");
return false;
public function getRideDetails( $rideId ) {
return WPStrava::get_instance()->api->get( "rides/{$rideId}" );
} // getRideDetails
public function getRidesDetails( $rides ) {
$rides_details = array();
foreach ( $rides as $stravaRide ) {
$detail = $this->getRideDetails( $stravaRide->id );
if ( is_wp_error( $detail ) )
return $detail;
$rides_details[] = $detail;
}
return $rides_details;
} // getRidesDetails
public function getLatestRides($searchOption, $searchId, $quantity) {
$url = $this->ridesUrl;
public function getRidesSimple( $searchOption, $searchId ) {
$api = WPStrava::get_instance()->api;
$data = NULL;
//Get the json results using the constructor specified values.
if($searchOption == "athlete") {
if(is_numeric($searchId)) {
$json = file_get_contents($url . '?athleteId=' . urlencode($searchId));
if ( $searchOption == 'athlete' ) {
if ( is_numeric( $searchId ) ) {
$data = $api->get( 'rides', array( 'athleteId' => $searchId ), 1 );
} else {
$json = file_get_contents($url . '?athleteName=' . urlencode($searchId));
$data = $api->get( 'rides', array( 'athleteName' => $searchId ), 1 );
}
} elseif ($searchOption == "club" AND is_numeric($searchId)) {
$json = file_get_contents($url . '?clubId=' . urlencode($searchId));
} elseif ($searchOption == 'club' && is_numeric($searchId)) {
$data = $api->get( 'rides', array( 'clubId' => $searchId ), 1 );
} else {
$this->feedback .= _e("There's an error on the widget options combination.", "wp-strava");
return new WP_Error( 'wp-strava_options', __("There's an error in your simple options.", 'wp-strava') );
}
if($json) {
$strava_rides = json_decode($json);
$this->stravaRides = array_slice($strava_rides->rides, 0, $quantity);
} else {
$this->feedback .= _e("There was an error pulling data of strava.com.", "wp-strava");
return false;
}
} // getLatestRides
if ( is_wp_error( $data ) )
return $data;
if ( isset( $data->rides ) )
return $data->rides;
return array();
} // getRidesSimple
public function getRidesAdvanced( $params ) {
$data = WPStrava::get_instance()->api->get( 'rides', explode( "\n", $params ), 1 ); //version 1
if ( is_wp_error( $data ) )
return $data;
if ( isset( $data->rides ) )
return $data->rides;
return array();
}
public function getRideMap($rideId, $token, $efforts, $threshold) {
if($rideId != 0 AND $token != "") {