Skip to content

Commit

Permalink
Merge pull request #3 from chrisvogt/add/api_stats_resource
Browse files Browse the repository at this point in the history
Connects the WakaTime stats() resource
  • Loading branch information
Mario Bašić committed Nov 12, 2015
2 parents 1665c89 + 0c2451b commit e159ab0
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 24 deletions.
56 changes: 32 additions & 24 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,19 @@ If you are using [Laravel](http://laravel.com/) check out [WakaTime Reports and

Add to your `composer.json`:

```
```php
"mabasic/wakatime-php-api": "~1.0"
```

and run `composer update` or type this from command line:
and run `composer update` or type this from command line:

```
```php
composer require "mabasic/wakatime-php-api=~1.0"
```

## Usage

```
```php
<?php

use GuzzleHttp\Client as Guzzle;
Expand All @@ -39,45 +39,53 @@ $wakaTime->setApiKey($your_api_key_for_wakatime);
```

You can get your Api Key from your [settings page](https://wakatime.com/settings).

> Be sure to set your Api Key before using any of the methods because you will get an Exception.
## Methods

### Official methods

#### currentUser

```
```php
$wakatime->currentUser()
```

See: https://wakatime.com/api#users-current for details.

#### dailySummary

```
```php
$wakatime->dailySummary($startDate, $endDate, $project = null)
```

See: https://wakatime.com/api#summary-daily for details.

#### stats

```php
$wakatime->stats($range, $project = null)
```

See: https://wakatime.com/developers#stats for details.

### Additional methods

#### getHoursLoggedFor

```
```php
$wakatime->getHoursLoggedFor($startDate, $endDate, $project = null)
```

Calculates hours logged for a specific period.
Calculates hours logged for a specific period.
_You can optionally specify a project._

> `$startDate` must be lower than `$endDate`
**Example:**

```
```php
$startDate = '11/21/2014';
$endDate = '12/21/2014';

Expand All @@ -86,49 +94,49 @@ $hours = $wakaTime->getHoursLoggedFor($startDate, $endDate);

#### getHoursLoggedForLast

```
```php
public function getHoursLoggedForLast($period, $project = null)
```

Calculates hours logged in last xy days, months.
Calculates hours logged in last xy days, months.
_You can optionally specify a project._

**Example:**

```
```php
$hours = $wakaTime->getHoursLoggedForLast('7 days');
```

#### getHoursLoggedForToday

```
```php
public function getHoursLoggedForToday($project = null)
```

Returns hours logged today.
Returns hours logged today.
_You can optionally specify a project._

#### getHoursLoggedForYesterday

```
```php
public function getHoursLoggedForYesterday($project = null)
```

Returns hours logged yesterday.
Returns hours logged yesterday.
_You can optionally specify a project._

#### getHoursLoggedForLast7Days

```
```php
public function getHoursLoggedForLast7Days($project = null)
```

Basic users can only see data for maximum 7 days. Become a Premium user to preserve all data history.
Basic users can only see data for maximum 7 days. Become a Premium user to preserve all data history.
_You can still use any method as long as it is under 7 days._

#### getHoursLoggedForLast30Days

```
```php
public function getHoursLoggedForLast30Days($project = null)
```

Expand All @@ -137,7 +145,7 @@ _You can optionally specify a project._

#### getHoursLoggedForThisMonth

```
```php
public function getHoursLoggedForThisMonth($project = null)
```

Expand All @@ -146,7 +154,7 @@ _You can optionally specify a project._

#### getHoursLoggedForLastMonth

```
```php
public function getHoursLoggedForLastMonth($project = null)
```

Expand All @@ -173,4 +181,4 @@ export WAKATIME_API_KEY=xyz
export WAKATIME_PROJECT=xyz
```

_Of course replace `xyz` with correct values._
_Of course replace `xyz` with correct values._
14 changes: 14 additions & 0 deletions src/WakaTime.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,20 @@ public function dailySummary($startDate, $endDate, $project = null)
return $this->guzzle->get("{$this->url}/summaries?start={$startDate}&end={$endDate}&api_key={$this->getApiKey()}" . $project)->json();
}

/**
* See: https://wakatime.com/developers#stats for details.
*
* @param $range
* @param null $project
* @return mixed
*/
public function stats($range, $project = null)
{
if ($project !== null) $project = "&project={$project}";

return $this->guzzle->get("{$this->url}/users/current/stats/{$range}?api_key={$this->getApiKey()}" . $project)->json();
}

/**
* Calculates hours logged for a specific period.
* You can optionally specify a project.
Expand Down
22 changes: 22 additions & 0 deletions tests/WakaTimeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,28 @@ public function it_returns_the_currently_logged_in_user()
$this->assertInternalType('array', $response);
}

/** @test */
public function it_returns_user_stats_for_range()
{
// Act
$range = 'last_30_days';
$response = $this->wakaTime->stats($range, $this->project);

// Assert
$this->assertInternalType('array', $response);
}

/** @test */
public function it_returns_user_stats_for_range_and_project()
{
// Act
$range = 'last_30_days';
$response = $this->wakaTime->stats($range, $this->project);

// Assert
$this->assertInternalType('array', $response);
}

/** @test */
public function it_returns_the_daily_summary_for_period()
{
Expand Down

0 comments on commit e159ab0

Please sign in to comment.