Skip to content

Commit 88e3dc6

Browse files
authored
Merge pull request #6 from renoki-co/feature/collection
[feature] Collection
2 parents 0dcdda5 + 42c1743 commit 88e3dc6

File tree

5 files changed

+154
-84
lines changed

5 files changed

+154
-84
lines changed

README.md

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,25 @@ $subreddit = Reddit::subreddit(
3131
$app
3232
);
3333

34-
$posts = $subreddit->get()['data']['children'] ?? [];
34+
$posts = $subreddit->get();
35+
36+
foreach ($posts as $post) {
37+
$id = $post['id'];
38+
}
39+
```
40+
41+
When retrieving posts, the results are wrapped in a `Rennokki\RedditApi\RedditList` class. This class is based on Laravel Collection and you can pipeline actions on it more easily. Please see [Laravel Collections documentantion](https://laravel.com/docs/master/collections).
42+
43+
## Pagination
44+
45+
For pagination purposes, you shall call `nextPage()` from the previous `$posts`:
46+
47+
```php
48+
$subreddit = Reddit::subreddit('funny', $app);
49+
50+
$posts = $subreddit->get();
51+
52+
$nextPageOfPosts = $posts->nextPage();
3553
```
3654

3755
## Sorting
@@ -81,18 +99,6 @@ $subreddit = Reddit::subreddit('funny', $app);
8199
$subreddit->setLimit(100);
82100
```
83101

84-
## Before & After tokens
85-
86-
For paginating, you shall specify before & after from previous requests:
87-
88-
```php
89-
$subreddit = Reddit::subreddit('funny', $app);
90-
91-
$subreddit->before(...);
92-
93-
$subreddit->after(...);
94-
```
95-
96102
## Debugging
97103

98104
If you wish to inspect the URL that is being called, you ca do so:

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@
1616
],
1717
"require": {
1818
"php": "^7.0",
19-
"guzzlehttp/guzzle": "^6.5"
19+
"guzzlehttp/guzzle": "^6.5",
20+
"illuminate/support": "^5.5|^6.0|^7.0"
2021
},
2122
"require-dev": {
2223
"orchestra/database": "^5.0",

src/RedditList.php

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
<?php
2+
3+
namespace Rennokki\RedditApi;
4+
5+
use Illuminate\Support\Collection;
6+
7+
class RedditList extends Collection
8+
{
9+
/**
10+
* The Subreddit instance from which
11+
* the content was filled.
12+
*
13+
* @var \Rennokki\RedditApi\Subreddit
14+
*/
15+
protected $subreddit;
16+
17+
/**
18+
* The `after` token for pagination.
19+
*
20+
* @return string|null
21+
*/
22+
protected $after = null;
23+
24+
/**
25+
* Set the Subreddit instance from which
26+
* the content was filled.
27+
*
28+
* @param \Rennokki\RedditApi\Subreddit $subreddit
29+
* @return $this
30+
*/
31+
public function setSubreddit(Subreddit $subreddit)
32+
{
33+
$this->subreddit = $subreddit;
34+
35+
return $this;
36+
}
37+
38+
/**
39+
* Set the after token from the request.
40+
*
41+
* @param string|null $after
42+
* @return $this
43+
*/
44+
public function setAfter($after)
45+
{
46+
$this->after = $after;
47+
48+
return $this;
49+
}
50+
51+
/**
52+
* Get the next page of results.
53+
*
54+
* @return \Rennokki\RedditApi\RedditList
55+
*/
56+
public function nextPage()
57+
{
58+
return $this->subreddit
59+
->after($this->getAfter())
60+
->get();
61+
}
62+
63+
/**
64+
* Get the after token.
65+
*
66+
* @return string|null
67+
*/
68+
public function getAfter()
69+
{
70+
return $this->after;
71+
}
72+
}

src/Subreddit.php

Lines changed: 11 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,6 @@ class Subreddit
2121
*/
2222
protected $after = null;
2323

24-
/**
25-
* The `before` token for pagination.
26-
*
27-
* @return string|null
28-
*/
29-
protected $before = null;
30-
3124
/**
3225
* The limit on each page.
3326
* An `after` is passed by the API
@@ -84,29 +77,16 @@ public function __construct(string $subreddit, App $app)
8477
/**
8578
* Set the after token.
8679
*
87-
* @param string $before
80+
* @param string|null $after
8881
* @return $this
8982
*/
90-
public function after(string $after)
83+
public function after($after)
9184
{
9285
$this->after = $after;
9386

9487
return $this;
9588
}
9689

97-
/**
98-
* Set the before token.
99-
*
100-
* @param string $before
101-
* @return $this
102-
*/
103-
public function before(string $before)
104-
{
105-
$this->before = $before;
106-
107-
return $this;
108-
}
109-
11090
/**
11191
* Set the limit of posts.
11292
*
@@ -173,7 +153,15 @@ public function get()
173153
);
174154
}
175155

176-
return @json_decode($request->getBody(), true);
156+
$response = @json_decode($request->getBody(), true);
157+
158+
$posts = collect($response['data']['children'])
159+
->pluck('data')
160+
->toArray();
161+
162+
return (new RedditList($posts))
163+
->setSubreddit($this)
164+
->setAfter($response['data']['after'] ?? null);
177165
}
178166

179167
/**
@@ -203,10 +191,6 @@ public function getCallableUrl(): string
203191
$parameters['after'] = $this->after;
204192
}
205193

206-
if ($this->before) {
207-
$parameters['before'] = $this->before;
208-
}
209-
210194
$query = http_build_query($parameters);
211195

212196
return "{$url}.json?{$query}";

tests/RedditTest.php

Lines changed: 50 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22

33
namespace Rennokki\RedditApi\Test;
44

5-
use Exception;
65
use Rennokki\RedditApi\Reddit;
6+
use Rennokki\RedditApi\RedditList;
77
use Rennokki\RedditApi\Subreddit;
88

99
class RedditTest extends TestCase
@@ -64,15 +64,6 @@ public function test_after()
6464
);
6565
}
6666

67-
public function test_before()
68-
{
69-
$this->api->before('111');
70-
71-
$this->assertStringContainsString(
72-
'before=111', $this->api->getCallableUrl()
73-
);
74-
}
75-
7667
public function test_sort_filtering()
7768
{
7869
foreach (Subreddit::$sorts as $sort) {
@@ -128,50 +119,66 @@ public function test_time_filtering()
128119

129120
public function test_top()
130121
{
131-
try {
132-
$data = $this
133-
->api
134-
->sort('hot')
135-
->get();
136-
} catch (Exception $e) {
137-
return $this->assertFalse(true);
138-
}
122+
$posts = $this
123+
->api
124+
->sort('hot')
125+
->get();
139126

140-
$this->assertTrue(
141-
count($data['data']['children']) > 1
142-
);
127+
$this->assertInstanceOf(RedditList::class, $posts);
128+
129+
$this->assertCount(22, $posts);
130+
131+
foreach ($posts as $post) {
132+
$this->assertNotNull($post['permalink'] ?? null);
133+
}
143134
}
144135

145136
public function test_hot()
146137
{
147-
try {
148-
$data = $this
149-
->api
150-
->sort('top')
151-
->time('all')
152-
->get();
153-
} catch (Exception $e) {
154-
return $this->assertFalse(true);
155-
}
138+
$posts = $this
139+
->api
140+
->sort('top')
141+
->time('all')
142+
->get();
156143

157-
$this->assertTrue(
158-
count($data['data']['children']) > 1
159-
);
144+
$this->assertInstanceOf(RedditList::class, $posts);
145+
146+
$this->assertCount(20, $posts);
147+
148+
foreach ($posts as $post) {
149+
$this->assertNotNull($post['permalink'] ?? null);
150+
}
160151
}
161152

162153
public function test_new()
163154
{
164-
try {
165-
$data = $this
166-
->api
167-
->sort('new')
168-
->get();
169-
} catch (Exception $e) {
170-
return $this->assertFalse(true);
155+
$posts = $this
156+
->api
157+
->sort('new')
158+
->get();
159+
160+
$this->assertInstanceOf(RedditList::class, $posts);
161+
162+
$this->assertCount(20, $posts);
163+
164+
foreach ($posts as $post) {
165+
$this->assertNotNull($post['permalink'] ?? null);
171166
}
167+
}
172168

173-
$this->assertTrue(
174-
count($data['data']['children']) > 1
175-
);
169+
public function test_next_page()
170+
{
171+
$posts = $this
172+
->api
173+
->sort('new')
174+
->get();
175+
176+
$firstPageAfter = $posts->getAfter();
177+
178+
$posts = $posts->nextPage();
179+
180+
$secondPageAfter = $posts->getAfter();
181+
182+
$this->assertNotEquals($firstPageAfter, $secondPageAfter);
176183
}
177184
}

0 commit comments

Comments
 (0)