diff --git a/README.md b/README.md index 8d9ac50..bf8fb7a 100644 --- a/README.md +++ b/README.md @@ -84,7 +84,48 @@ print_r($foods); echo ''; ``` +## Time Series Data ## + +Time series data can be colected as described [http://wiki.fitbit.com/display/API/API-Get-Time-Series](http://wiki.fitbit.com/display/API/API-Get-Time-Series) + +The base class, TimeSeriesGateway, uses the magic __call method to map methods calls to resource endpoints. The method name is converted to the last segment of the URI, the rest of the path is handled by the class. + +```php +$sleep_time_series_gateway = $factory->getTimeSeriesGateway(); +$minutes_asleep = $sleep_time_series_gateway->getMinutesAsleep(); +``` + +A method call without parameters will default to a time series of today/1d. A period, baseDate and endDate can be passed to specify the time series. *Note: period will override endDate. + +```php +//past seven days +$minutes_asleep = $sleep_time_series_gateway->getMinutesAsleep('today','7d'); + +//seven days before the new year (*Note you can also pass Datetime objects for baseDate and endDate) +$minutes_asleep = $sleep_time_series_gateway->getMinutesAsleep('2014-01-01','7d'); + +//first week of new year +$minutes_asleep = $sleep_time_series_gateway->getMinutesAsleep('2014-01-01', null, '2014-01-07'); +``` + +The Activities Time Series Resource allows you to limit the data collected to that logged only by the tracker. To do so pass true|false (default false) as the first argument in activites timeseries calls + +```php +$activities_time_series_gateway = $factory->getActivitiesSeriesGateway(); +//get the minutes of very active activity from the tracker only for the previous 7 days +$tracker_minutes_very_active = $activities_time_series_gateway->getMinutesVeryActive(true, 'today', '7d'); +``` +*Note: the activities/caloriesBMR resource does not appear to have a corresponding activities/tracker/caloriesBMR. The tracker parameter for getCaloriesBMR(true) will automatically be set to false. + + ## Notes ## * By default, all requests assume you want data for the authorized user (viewer). There are, however, several endpoints you can use to access the data of other Fitbit users, given that you have permission to access their data. This is accomplished by setting the Fitbit User ID with the `setUserID` method available on `ApiGatewayFactory` and the Endpoint Gateways (e.g. `UserGateway`, `FoodGateway`). * *Subscriptions*: this library has some basic methods to add/delete subscriptions, but it's your responsibility to track the list and maintain server endpoints to receive notifications from Fitbit, as well as register them at [http://dev.fitbit.com](http://dev.fitbit.com). See [Subscriptions API](https://wiki.fitbit.com/display/API/Fitbit+Subscriptions+API) for more information. + + ## Known Issues ## + +At the time of writing, the following resources do not currently work, or are documented incorrectly by Fitbit's documentation. + +* activities/floors +* activities/elevation diff --git a/composer.json b/composer.json index 71f8378..7037f94 100644 --- a/composer.json +++ b/composer.json @@ -8,7 +8,7 @@ "license" : "Apache-2.0", "authors": [ { - "name": "Ryan Martinsen", + "name": ["Ryan Martinsen","Juni Samos"], "email": "ryan@ryanware.com", "homepage": "http://ryanmartinsen.com" }, @@ -19,7 +19,7 @@ ], "require": { "php": ">=5.3.0", - "lusitanian/oauth": "~0.2" + "lusitanian/oauth": "0.2.5" }, "autoload": { "psr-0": { diff --git a/lib/Fitbit/ActivityTimeSeriesGateway.php b/lib/Fitbit/ActivityTimeSeriesGateway.php new file mode 100644 index 0000000..4fc4ecd --- /dev/null +++ b/lib/Fitbit/ActivityTimeSeriesGateway.php @@ -0,0 +1,55 @@ +trackerOnlyFragment($fragment) : $fragment; + return parent::get($fragment, $baseDate, $period, $endDate); + } + + /** + * extended call, to ensure methods without tracker + * have tracker set to false + * + * {@inheritdoc} + */ + public function __call($method, $parameters) + { + if (in_array($method, array('getCaloriesBMR'))) $parameters[0] = false; + return parent::__call($method, $parameters); + } + + +} diff --git a/lib/Fitbit/ApiGatewayFactory.php b/lib/Fitbit/ApiGatewayFactory.php index 7ac2078..105b9b9 100644 --- a/lib/Fitbit/ApiGatewayFactory.php +++ b/lib/Fitbit/ApiGatewayFactory.php @@ -167,6 +167,13 @@ public function getActivityGateway() return $gateway; } + public function getActivityTimeSeriesGateway() + { + $gateway = new ActivityTimeSeriesGateway; + $this->injectGatewayDependencies($gateway); + return $gateway; + } + public function getBodyGateway() { $gateway = new BodyGateway; @@ -174,6 +181,13 @@ public function getBodyGateway() return $gateway; } + public function getBodyTimeSeriesGateway() + { + $gateway = new BodyTimeSeriesGateway; + $this->injectGatewayDependencies($gateway); + return $gateway; + } + public function getFoodGateway() { $gateway = new FoodGateway; @@ -181,6 +195,13 @@ public function getFoodGateway() return $gateway; } + public function getFoodTimeSeriesGateway() + { + $gateway = new FoodTimeSeriesGateway; + $this->injectGatewayDependencies($gateway); + return $gateway; + } + public function getSleepGateway() { $gateway = new SleepGateway; @@ -188,6 +209,13 @@ public function getSleepGateway() return $gateway; } + public function getSleepTimeSeriesGateway() + { + $gateway = new SleepTimeSeriesGateway; + $this->injectGatewayDependencies($gateway); + return $gateway; + } + public function getTimeGateway() { $gateway = new TimeGateway; diff --git a/lib/Fitbit/BodyTimeSeriesGateway.php b/lib/Fitbit/BodyTimeSeriesGateway.php new file mode 100644 index 0000000..faee522 --- /dev/null +++ b/lib/Fitbit/BodyTimeSeriesGateway.php @@ -0,0 +1,15 @@ +format("Y-m-d"); + + if ($date2 instanceof Datetime) + $date2 = $date2->format("Y-m-d"); + + $endpoint = sprintf('user/%s/%s/%s/%s', $this->userID, $fragment, $date1, $date2); + return $this->makeApiRequest($endpoint); + } + + /** + * Dynamically pass methods to get. + * + * @param string $method + * @param array $parameters + * @return mixed + */ + public function __call($method, $parameters) + { + $fragment = $this->fragment($method); + array_unshift($parameters, $fragment); + return call_user_func_array(array($this, 'get'), $parameters); + } + +}