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

Commit

Permalink
Merge pull request #226 from seregazhuk/develop
Browse files Browse the repository at this point in the history
pagination skip
  • Loading branch information
seregazhuk authored Dec 11, 2016
2 parents 7f9bba6 + 1410099 commit 02597c9
Show file tree
Hide file tree
Showing 7 changed files with 122 additions and 19 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,13 @@
All notable changes to this project will be documented in this file.


## [5.1.2] - 2016-12-11
### Fixed:
- Registration now sends confirmation emails.

### Added:
- *Skip* method to pagination.

## [5.1.1] - 2016-12-03
### Added:
- To any pagination added *toArray* method to receive all pagination results as array.
Expand Down
19 changes: 18 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -78,11 +78,21 @@ for no limit. For example,
foreach($bot->pins->search('query', 2) as $pin) {
// ...
}
```

will return only 2 pins of the search results.

To receive all results at once as array:
```php
// get all results as array
$results = $bot->pins->search('query', 2)->toArray();
```
will return only 2 pins of the search results.

Skip some results:
```php
// skip first 50 results
$results = $bot->pins->search('query', 2)->skip(50)->get();
```

## Account

Expand Down Expand Up @@ -147,6 +157,13 @@ $bot->auth->registerBusiness('youremail@gmail.com', 'password', 'BusinessName');
$bot->auth->registerBusiness('youremail@gmail.com', 'password', 'BusinessName', 'http://yoursite.com');
```

After registration you will receive a confirmation email. You can pass a link from this email to `confirmEmail`
method:

```php
$bot->auth->confirmEmail($linkFromEmail);
```

Convert your account to a business one. Requires log in. The last parameter with website url is *optional*:

```php
Expand Down
65 changes: 61 additions & 4 deletions src/Api/Providers/Auth.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace seregazhuk\PinterestBot\Api\Providers;

use seregazhuk\PinterestBot\Api\Response;
use seregazhuk\PinterestBot\Helpers\UrlBuilder;

class Auth extends Provider
Expand Down Expand Up @@ -64,6 +65,7 @@ public function register($email, $password, $name, $country = 'GB', $age = 18)
"password" => $password,
"country" => $country,
"first_name" => $name,
"gender" => "male",
"container" => 'home_page',
];

Expand Down Expand Up @@ -100,6 +102,15 @@ public function isLoggedIn()
return $this->request->isLoggedIn();
}

/**
* @param string $link
* @return array|bool
*/
public function confirmEmail($link)
{
return $this->execGetRequest([], $link);
}

/**
* Validates password and login.
*
Expand All @@ -113,12 +124,37 @@ protected function checkCredentials($username, $password)
}
}

/**
* @return bool|Response
*/
protected function sendEmailVerificationRequest()
{
$actions = [
['name' => 'unauth.signup_step_1.completed']
];

return $this->sendRegisterActionRequest($actions);
}

/**
* @param array $actions
* @return bool|Response
*/
protected function sendRegisterActionRequest($actions = [])
{
return $this->execPostRequest(
['actions' => $actions],
UrlBuilder::RESOURCE_UPDATE_REGISTRATION_TRACK,
true
);
}

/**
* @return bool
*/
protected function completeRegistration()
{
$this->request->setTokenFromCookies();
if(!$this->sendRegisterActions()) return false;

return $this->execPostRequest(
['placed_experience_id' => self::REGISTRATION_COMPLETE_EXPERIENCE_ID],
Expand All @@ -135,9 +171,9 @@ protected function makeRegisterCall($data)
$this->visitMainPage();
$this->request->setTokenFromCookies();

if (!$this->execPostRequest($data, UrlBuilder::RESOURCE_CREATE_REGISTER)) {
return false;
}
if(!$this->sendEmailVerificationRequest()) return false;

if(!$this->execPostRequest($data, UrlBuilder::RESOURCE_CREATE_REGISTER)) return false;

return $this->completeRegistration();
}
Expand Down Expand Up @@ -187,4 +223,25 @@ protected function getProfile()
{
return $this->execGetRequest([], UrlBuilder::RESOURCE_GET_USER_SETTINGS);
}

/**
* @return bool
*/
protected function sendRegisterActions()
{
$actions = [
["name" => "multi_step_step_2_complete"],
["name" => "signup_home_page"],
["name" => "signup_referrer.other"],
["name" => "signup_referrer_module.unauth_home_react_page"],
["name" => "unauth.signup_step_2.completed"],
["name" => "setting_new_window_location"],
];

if(!$this->sendRegisterActionRequest($actions)) return false;

if(!$this->sendRegisterActionRequest()) return false;

return true;
}
}
2 changes: 1 addition & 1 deletion src/Api/Request.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ class Request
'DNT: 1',
'X-Pinterest-AppState: active',
'X-NEW-APP: 1',
'X-APP-VERSION: 2414ec2',
'X-APP-VERSION: aa0e0f3',
'X-Requested-With: XMLHttpRequest',
];

Expand Down
4 changes: 2 additions & 2 deletions src/Api/Response.php
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ public function isEmpty()
*/
public function isOk()
{
return !$this->hasErrors() && !$this->isEmpty();
return !$this->hasErrors();
}

/**
Expand All @@ -131,7 +131,7 @@ public function isOk()
*/
public function hasErrors()
{
return !is_null($this->lastError);
return !empty($this->lastError);
}

/**
Expand Down
32 changes: 27 additions & 5 deletions src/Helpers/Pagination.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,11 @@ class Pagination implements \IteratorAggregate
*/
protected $callback;

/**
* @var int
*/
protected $offset;

/**
* @param int $limit
*/
Expand All @@ -54,6 +59,15 @@ public function paginateOver(callable $callback)
return $this;
}

/**
* Syntax sugar for getIterator method
* @return Traversable
*/
public function get()
{
return $this->getIterator();
}

/**
* Retrieve an external iterator
* @return Traversable
Expand All @@ -69,15 +83,23 @@ public function getIterator()

foreach ($results as $result) {
$resultsNum++;
yield $result;

if ($this->paginationFinished($resultsNum)) {
return;
}
if($resultsNum > $this->offset) yield $result;

if ($this->paginationFinished($resultsNum)) return;
}
}
}

return;
/**
* @param int $offset
* @return $this
*/
public function skip($offset)
{
$this->offset = $offset;

return $this;
}

/**
Expand Down
12 changes: 6 additions & 6 deletions tests/Bot/Api/AuthTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,10 @@ class AuthTest extends ProviderTest
*/
public function it_should_register_new_user()
{
$this->setTokenFromCookiesExpectation(2);
$this->setTokenFromCookiesExpectation();
$this->setProperty('request', $this->request);

$this->apiShouldReturnSuccess(3)
$this->apiShouldReturnSuccess(6)
->assertTrue($this->provider->register('email@email.com', 'test', 'name'));
}

Expand All @@ -42,7 +42,7 @@ public function it_returns_false_when_error_in_registration()
$this->setTokenFromCookiesExpectation();
$this->setProperty('request', $this->request);

$this->apiShouldReturnError(2)
$this->apiShouldReturnError(3)
->assertFalse($this->provider->register('email@email.com', 'test', 'name'));
}

Expand All @@ -51,10 +51,10 @@ public function it_returns_false_when_error_in_registration()
*/
public function it_should_register_business_account()
{
$this->setTokenFromCookiesExpectation(2);
$this->setTokenFromCookiesExpectation();
$this->setProperty('request', $this->request);

$this->apiShouldReturnSuccess(3)
$this->apiShouldReturnSuccess(6)
->assertTrue($this->provider->registerBusiness('email@email.com', 'test', 'name'));
}

Expand All @@ -66,7 +66,7 @@ public function it_should_return_false_when_error_in_business_registration()
$this->setTokenFromCookiesExpectation();
$this->setProperty('request', $this->request);

$this->apiShouldReturnError(2)
$this->apiShouldReturnError(3)
->assertFalse($this->provider->registerBusiness('email@email.com', 'test', 'name'));
}

Expand Down

0 comments on commit 02597c9

Please sign in to comment.