Skip to content
This repository was archived by the owner on Feb 2, 2025. It is now read-only.

Commit e159ab0

Browse files
author
Mario Bašić
committed
Merge pull request #3 from chrisvogt/add/api_stats_resource
Connects the WakaTime stats() resource
2 parents 1665c89 + 0c2451b commit e159ab0

File tree

3 files changed

+68
-24
lines changed

3 files changed

+68
-24
lines changed

README.md

Lines changed: 32 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -16,19 +16,19 @@ If you are using [Laravel](http://laravel.com/) check out [WakaTime Reports and
1616

1717
Add to your `composer.json`:
1818

19-
```
19+
```php
2020
"mabasic/wakatime-php-api": "~1.0"
2121
```
2222

23-
and run `composer update` or type this from command line:
23+
and run `composer update` or type this from command line:
2424

25-
```
25+
```php
2626
composer require "mabasic/wakatime-php-api=~1.0"
2727
```
2828

2929
## Usage
3030

31-
```
31+
```php
3232
<?php
3333

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

4141
You can get your Api Key from your [settings page](https://wakatime.com/settings).
42-
42+
4343
> Be sure to set your Api Key before using any of the methods because you will get an Exception.
44-
44+
4545
## Methods
4646

4747
### Official methods
4848

4949
#### currentUser
5050

51-
```
51+
```php
5252
$wakatime->currentUser()
5353
```
5454

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

5757
#### dailySummary
5858

59-
```
59+
```php
6060
$wakatime->dailySummary($startDate, $endDate, $project = null)
6161
```
6262

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

65+
#### stats
66+
67+
```php
68+
$wakatime->stats($range, $project = null)
69+
```
70+
71+
See: https://wakatime.com/developers#stats for details.
72+
6573
### Additional methods
6674

6775
#### getHoursLoggedFor
6876

69-
```
77+
```php
7078
$wakatime->getHoursLoggedFor($startDate, $endDate, $project = null)
7179
```
7280

73-
Calculates hours logged for a specific period.
81+
Calculates hours logged for a specific period.
7482
_You can optionally specify a project._
7583

7684
> `$startDate` must be lower than `$endDate`
7785
7886
**Example:**
7987

80-
```
88+
```php
8189
$startDate = '11/21/2014';
8290
$endDate = '12/21/2014';
8391

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

8795
#### getHoursLoggedForLast
8896

89-
```
97+
```php
9098
public function getHoursLoggedForLast($period, $project = null)
9199
```
92100

93-
Calculates hours logged in last xy days, months.
101+
Calculates hours logged in last xy days, months.
94102
_You can optionally specify a project._
95103

96104
**Example:**
97105

98-
```
106+
```php
99107
$hours = $wakaTime->getHoursLoggedForLast('7 days');
100108
```
101109

102110
#### getHoursLoggedForToday
103111

104-
```
112+
```php
105113
public function getHoursLoggedForToday($project = null)
106114
```
107115

108-
Returns hours logged today.
116+
Returns hours logged today.
109117
_You can optionally specify a project._
110118

111119
#### getHoursLoggedForYesterday
112120

113-
```
121+
```php
114122
public function getHoursLoggedForYesterday($project = null)
115123
```
116124

117-
Returns hours logged yesterday.
125+
Returns hours logged yesterday.
118126
_You can optionally specify a project._
119127

120128
#### getHoursLoggedForLast7Days
121129

122-
```
130+
```php
123131
public function getHoursLoggedForLast7Days($project = null)
124132
```
125133

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

129137
#### getHoursLoggedForLast30Days
130138

131-
```
139+
```php
132140
public function getHoursLoggedForLast30Days($project = null)
133141
```
134142

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

138146
#### getHoursLoggedForThisMonth
139147

140-
```
148+
```php
141149
public function getHoursLoggedForThisMonth($project = null)
142150
```
143151

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

147155
#### getHoursLoggedForLastMonth
148156

149-
```
157+
```php
150158
public function getHoursLoggedForLastMonth($project = null)
151159
```
152160

@@ -173,4 +181,4 @@ export WAKATIME_API_KEY=xyz
173181
export WAKATIME_PROJECT=xyz
174182
```
175183

176-
_Of course replace `xyz` with correct values._
184+
_Of course replace `xyz` with correct values._

src/WakaTime.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,20 @@ public function dailySummary($startDate, $endDate, $project = null)
6666
return $this->guzzle->get("{$this->url}/summaries?start={$startDate}&end={$endDate}&api_key={$this->getApiKey()}" . $project)->json();
6767
}
6868

69+
/**
70+
* See: https://wakatime.com/developers#stats for details.
71+
*
72+
* @param $range
73+
* @param null $project
74+
* @return mixed
75+
*/
76+
public function stats($range, $project = null)
77+
{
78+
if ($project !== null) $project = "&project={$project}";
79+
80+
return $this->guzzle->get("{$this->url}/users/current/stats/{$range}?api_key={$this->getApiKey()}" . $project)->json();
81+
}
82+
6983
/**
7084
* Calculates hours logged for a specific period.
7185
* You can optionally specify a project.

tests/WakaTimeTest.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,28 @@ public function it_returns_the_currently_logged_in_user()
150150
$this->assertInternalType('array', $response);
151151
}
152152

153+
/** @test */
154+
public function it_returns_user_stats_for_range()
155+
{
156+
// Act
157+
$range = 'last_30_days';
158+
$response = $this->wakaTime->stats($range, $this->project);
159+
160+
// Assert
161+
$this->assertInternalType('array', $response);
162+
}
163+
164+
/** @test */
165+
public function it_returns_user_stats_for_range_and_project()
166+
{
167+
// Act
168+
$range = 'last_30_days';
169+
$response = $this->wakaTime->stats($range, $this->project);
170+
171+
// Assert
172+
$this->assertInternalType('array', $response);
173+
}
174+
153175
/** @test */
154176
public function it_returns_the_daily_summary_for_period()
155177
{

0 commit comments

Comments
 (0)