Skip to content
This repository has been archived by the owner on Dec 30, 2020. It is now read-only.

Commit

Permalink
Merge remote-tracking branch 'github/master'
Browse files Browse the repository at this point in the history
  • Loading branch information
seregazhuk committed Mar 23, 2017
2 parents bbda1f8 + f44927c commit 3875a23
Show file tree
Hide file tree
Showing 13 changed files with 164 additions and 51 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
# Change Log
All notable changes to this project will be documented in this file.

## v5.2.5 - 2017-03-18
### Fixed:
- Pagination fails on last response.

## v5.2.4 - 2017-03-16
### Added:
- Pinners *tried* pins.
Expand All @@ -10,6 +14,7 @@ All notable changes to this project will be documented in this file.
- Pins *activity()* and *tried()* methods always return Pagination object.
- Boards *forUser* method returns detailed information for every board. If no boards
are available it returns empty array.
- Pagination processes *getResponseData()* as array

## v5.2.3 - 2017-01-24
### Fixed:
Expand Down
20 changes: 12 additions & 8 deletions phpunit.xml
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,18 @@
<whitelist>
<directory suffix=".php">./src/</directory>
<exclude>
<file>/src/Api/Providers/Topics.php</file>
<file>/src/Api/Providers/Boards.php</file>
<file>/src/Api/Providers/Comments.php</file>
<file>/src/Api/Providers/Inbox.php</file>
<file>/src/Api/Providers/Pins.php</file>
<file>/src/Api/Providers/Pinners.php</file>
<file>/src/Api/Providers/Interests.php</file>
<file>/src/Api/Providers/User.php</file>
<file>./src/Api/Providers/Core/EntityProvider.php</file>
<file>./src/Api/Providers/Topics.php</file>
<file>./src/Api/Providers/Boards.php</file>
<file>./src/Api/Providers/Comments.php</file>
<file>./src/Api/Providers/Inbox.php</file>
<file>./src/Api/Providers/Pins.php</file>
<file>./src/Api/Providers/Pinners.php</file>
<file>./src/Api/Providers/Password.php</file>
<file>./src/Api/Providers/Interests.php</file>
<file>./src/Api/Providers/User.php</file>
<file>./src/Api/Providers/Auth.php</file>
<file>./src/Api/Providers/Keywords.php</file>
</exclude>
</whitelist>
</filter>
Expand Down
4 changes: 2 additions & 2 deletions src/Api/Providers/Auth.php
Original file line number Diff line number Diff line change
Expand Up @@ -176,9 +176,9 @@ protected function processLogin($username, $password)
'password' => $password,
];

$response = $this->post($credentials, UrlBuilder::RESOURCE_LOGIN, true);
$this->post($credentials, UrlBuilder::RESOURCE_LOGIN);

if ($response->hasErrors()) return false;
if ($this->response->hasErrors()) return false;

$this->request->login();

Expand Down
18 changes: 10 additions & 8 deletions src/Api/Providers/Core/Provider.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,17 +45,16 @@ public function __construct(Request $request, Response $response)
*
* @param array $requestOptions
* @param string $resourceUrl
* @param bool $returnResponse
*
* @return Response|bool
*/
protected function post($requestOptions, $resourceUrl, $returnResponse = false)
protected function post($requestOptions, $resourceUrl)
{
$postString = Request::createQuery($requestOptions);

$this->execute($resourceUrl, $postString);

return $returnResponse ? $this->response : $this->response->isOk();
return $this->response->isOk();

}

Expand All @@ -75,9 +74,7 @@ protected function get(array $requestOptions = [], $resourceUrl = '')

$this->execute($resourceUrl . '?' . $query);

return $this->response->hasBookmarks() ?
$this->response :
$this->response->getResponseData();
return $this->response->getResponseData();

}

Expand Down Expand Up @@ -122,12 +119,17 @@ public function isLoggedIn()
*/
protected function paginate($data, $resourceUrl, $limit = Pagination::DEFAULT_LIMIT)
{
return $this->paginateCustom(function() use ($data, $resourceUrl) {
return $this->get($data, $resourceUrl);
return $this
->paginateCustom(function () use ($data, $resourceUrl) {
$this->get($data, $resourceUrl);

return $this->response;
})->take($limit);
}

/**
* Accepts callback which should return PaginatedResponse object.
*
* @param callable $callback
* @param int $limit
* @return Pagination
Expand Down
2 changes: 1 addition & 1 deletion src/Api/Providers/Keywords.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public function recommendedFor($query)
'query' => $query,
];

$result = $this->get($requestOptions, UrlBuilder::getSearchUrl());
$result = $this->get($requestOptions, UrlBuilder::RESOURCE_SEARCH);

return $this->getKeywordsFromRequest($result);
}
Expand Down
2 changes: 1 addition & 1 deletion src/Api/Providers/Password.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public function reset($link, $newPassword)
'expiration' => $query['e'],
];

return $this->post($request, UrlBuilder::RESOURCE_RESET_PASSWORD_UPDATE, true);
return $this->post($request, UrlBuilder::RESOURCE_RESET_PASSWORD_UPDATE);
}

/**
Expand Down
12 changes: 6 additions & 6 deletions src/Api/Providers/Pins.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,9 +84,9 @@ public function create($imageUrl, $boardId, $description = '', $link = '')
'board_id' => $boardId,
];

return $this
->post($requestOptions, UrlBuilder::RESOURCE_CREATE_PIN, true)
->getResponseData();
$this->post($requestOptions, UrlBuilder::RESOURCE_CREATE_PIN);

return $this->response->getResponseData();
}

/**
Expand Down Expand Up @@ -140,9 +140,9 @@ public function repin($repinId, $boardId, $description = '')
'pin_id' => $repinId,
];

return $this
->post($requestOptions, UrlBuilder::RESOURCE_REPIN, true)
->getResponseData();
$this->post($requestOptions, UrlBuilder::RESOURCE_REPIN);

return $this->response->getResponseData();
}

/**
Expand Down
5 changes: 2 additions & 3 deletions src/Api/Traits/HandlesRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,10 @@ trait HandlesRequest
*
* @param array $requestOptions
* @param string $resourceUrl
* @param bool $returnResponse
*
* @return Response|bool
*/
abstract protected function post($requestOptions, $resourceUrl, $returnResponse = false);
abstract protected function post($requestOptions, $resourceUrl);

/**
* Executes a GET request to Pinterest API.
Expand All @@ -25,4 +24,4 @@ abstract protected function post($requestOptions, $resourceUrl, $returnResponse
* @return array|bool|Response
*/
abstract protected function get(array $requestOptions = [], $resourceUrl = '');
}
}
2 changes: 1 addition & 1 deletion src/Api/Traits/Searchable.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ protected function execSearchRequest($query, $scope)
*/
public function search($query, $limit = Pagination::DEFAULT_LIMIT)
{
return $this->paginateCustom(function() use ($query) {
return $this->paginateCustom(function () use ($query) {
return $this->execSearchRequest($query, $this->getSearchScope());
})->take($limit);
}
Expand Down
23 changes: 17 additions & 6 deletions src/Helpers/Pagination.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
use Traversable;
use EmptyIterator;
use IteratorAggregate;
use seregazhuk\PinterestBot\Api\Response;
use seregazhuk\PinterestBot\Api\Contracts\PaginatedResponse;


/**
* Class Pagination
Expand Down Expand Up @@ -53,7 +54,7 @@ public function __construct($limit = self::DEFAULT_LIMIT)
}

/**
* Sets a callback to make requests. Should be a closure.
* Sets a callback to make requests. Callback should return PaginatedResponse object.
*
* @param callable $callback
* @return $this
Expand Down Expand Up @@ -135,15 +136,14 @@ protected function processCallback()
{
$this->resultsNum = 0;
$this->processed = 0;
$callback = $this->callback;

while (true) {
/** @var Response $response */
$response = $callback();
/** @var PaginatedResponse $response */
$response = call_user_func($this->callback);

if ($response->isEmpty()) return;

foreach ($response->getResponseData() as $result) {
foreach ($this->getResultsFromResponse($response) as $result) {
$this->processed++;

if ($this->processed > $this->offset) {
Expand All @@ -157,4 +157,15 @@ protected function processCallback()
if (!$response->hasBookmarks()) return;
}
}

/**
* @param $response
* @return array
*/
protected function getResultsFromResponse(PaginatedResponse $response)
{
$results = $response->getResponseData();

return $results === false ? [] : $results;
}
}
14 changes: 13 additions & 1 deletion tests/Bot/PaginationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace seregazhuk\tests\Bot;

use Mockery;
use PHPUnit_Framework_TestCase;
use seregazhuk\PinterestBot\Api\Response;
use seregazhuk\tests\Helpers\ResponseHelper;
Expand All @@ -18,7 +19,7 @@ class PaginationTest extends PHPUnit_Framework_TestCase
public function it_returns_first_result_when_no_bookmarks()
{
$pagination = new Pagination();
$responseData = $this->createPaginatedResponse($this->paginatedResponse);
$responseData = $this->createSuccessApiResponse($this->paginatedResponse);

$pagination->paginateOver(function() use ($responseData){
return (new Response())->fill($responseData);
Expand Down Expand Up @@ -119,6 +120,17 @@ public function it_accepts_limit_in_constructor()

$this->assertEquals($expected, $pagination->toArray());
}

/** @test */
public function it_stops_when_response_is_empty()
{
$pagination = new Pagination();
$pagination->paginateOver(function(){
return (new Response())->fill([]);
});

$this->assertCount(0, $pagination->toArray());
}
}


Loading

0 comments on commit 3875a23

Please sign in to comment.