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

Commit

Permalink
Merge pull request #30 from hansott/feature-follow-unfollow
Browse files Browse the repository at this point in the history
✨ Allow (un)following boards and users
  • Loading branch information
hansott authored Mar 18, 2018
2 parents 9e5d339 + 05e5b47 commit e894cc4
Show file tree
Hide file tree
Showing 33 changed files with 447 additions and 347 deletions.
134 changes: 128 additions & 6 deletions src/Pinterest/Api.php
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,8 @@ public function execute(Request $request, $processor = null)
*
* @param Request $request
*
* @throws RateLimitedReached
*
* @return Response The response
*/
private function fetchUser(Request $request)
Expand Down Expand Up @@ -151,7 +153,7 @@ private function fetchPin(Request $request)
/**
* Fetch multiple boards and processes the response.
*
* @param Request $request
* @param Request $request
* @param string[] $fields
*
* @throws RateLimitedReached
Expand Down Expand Up @@ -217,6 +219,8 @@ private function fetchMultiplePins(Request $request, array $fields = null)
*
* @param string $usernameOrId The username or identifier of the user.
*
* @throws RateLimitedReached
*
* @return Response The response
*/
public function getUser($usernameOrId)
Expand All @@ -235,6 +239,8 @@ public function getUser($usernameOrId)
*
* @param string $boardId The board id.
*
* @throws RateLimitedReached
*
* @return Response The response
*/
public function getBoard($boardId)
Expand All @@ -253,6 +259,8 @@ public function getBoard($boardId)
*
* @param Board $board The updated board.
*
* @throws RateLimitedReached
*
* @return Response The response
*/
public function updateBoard(Board $board)
Expand All @@ -279,6 +287,8 @@ public function updateBoard(Board $board)
/**
* Get the boards of the authenticated user.
*
* @throws RateLimitedReached
*
* @return Response The response
*/
public function getUserBoards()
Expand All @@ -291,6 +301,8 @@ public function getUserBoards()
/**
* Get the pins of the authenticated user.
*
* @throws RateLimitedReached
*
* @return Response The response
*/
public function getUserPins()
Expand All @@ -303,6 +315,8 @@ public function getUserPins()
/**
* Get the authenticated user.
*
* @throws RateLimitedReached
*
* @return Response The response
*/
public function getCurrentUser()
Expand All @@ -315,6 +329,8 @@ public function getCurrentUser()
/**
* Get the followers of the authenticated user.
*
* @throws RateLimitedReached
*
* @return Response The response
*/
public function getUserFollowers()
Expand All @@ -327,6 +343,8 @@ public function getUserFollowers()
/**
* Get the boards that the authenticated user follows.
*
* @throws RateLimitedReached
*
* @return Response The response
*/
public function getUserFollowingBoards()
Expand All @@ -339,6 +357,8 @@ public function getUserFollowingBoards()
/**
* Get the users that the authenticated user follows.
*
* @throws RateLimitedReached
*
* @return Response The response
*/
public function getUserFollowing()
Expand All @@ -353,6 +373,8 @@ public function getUserFollowing()
*
* @link https://www.pinterest.com/explore/901179409185
*
* @throws RateLimitedReached
*
* @return Response The response
*/
public function getUserInterests()
Expand All @@ -367,6 +389,8 @@ public function getUserInterests()
*
* @param string $username The username of the user to follow.
*
* @throws RateLimitedReached
*
* @return Response The response
*/
public function followUser($username)
Expand All @@ -386,12 +410,96 @@ public function followUser($username)
return $this->execute($request);
}

/**
* Unfollow a user.
*
* @param string $usernameOrUserId The username or ID of the user to unfollow.
*
* @throws RateLimitedReached
*
* @return Response The response
*/
public function unfollowUser($usernameOrUserId)
{
if (empty($usernameOrUserId)) {
throw new InvalidArgumentException('Username or user ID is required.');
}

$request = new Request(
'DELETE',
"me/following/users/{$usernameOrUserId}"
);

return $this->execute($request);
}

/**
* Follow a board.
*
* @param string $username The username of the user that owns the board
* @param string $boardName The name of the board
*
* @return Response The response
*
* @throws RateLimitedReached
*/
public function followBoard($username, $boardName)
{
if (empty($username)) {
throw new InvalidArgumentException('Username is required.');
}

if (empty($boardName)) {
throw new InvalidArgumentException('The board name is required.');
}

$request = new Request(
'POST',
'me/following/boards/',
array(
'board' => "{$username}/{$boardName}",
)
);

return $this->execute($request);
}

/**
* Unfollow a board.
*
* @param string $username The username of the user that owns the board
* @param string $boardName The name of the board
*
* @return Response The response
*
* @throws RateLimitedReached
*/
public function unfollowBoard($username, $boardName)
{
if (empty($username)) {
throw new InvalidArgumentException('Username is required.');
}

if (empty($boardName)) {
throw new InvalidArgumentException('The board name is required.');
}

$request = new Request(
'DELETE',
"me/following/boards/{$username}/{$boardName}"
);

return $this->execute($request);
}

/**
* Create a board.
*
* @param string $name The board name.
* @param string $name The board name.
* @param string $description The board description.
*
* @throws RateLimitedReached
*
* @return Response The response
*/
public function createBoard($name, $description = null)
Expand All @@ -418,6 +526,8 @@ public function createBoard($name, $description = null)
*
* @param int $boardId The board id.
*
* @throws RateLimitedReached
*
* @return Response The response
*/
public function deleteBoard($boardId)
Expand All @@ -434,10 +544,12 @@ public function deleteBoard($boardId)
/**
* Create a pin on a board.
*
* @param string $boardId The board id.
* @param string $note The note.
* @param Image $image The image.
* @param string|null $link The link (Optional).
* @param string $boardId The board id.
* @param string $note The note.
* @param Image $image The image.
* @param string|null $link The link (Optional).
*
* @throws RateLimitedReached
*
* @return Response The response
*/
Expand Down Expand Up @@ -478,6 +590,8 @@ public function createPin($boardId, $note, Image $image, $link = null)
*
* @param string $pinId The id of the pin to delete.
*
* @throws RateLimitedReached
*
* @return Response The response
*/
public function deletePin($pinId)
Expand All @@ -496,6 +610,8 @@ public function deletePin($pinId)
*
* @param PagedList $pagedList
*
* @throws RateLimitedReached
*
* @return Response The response
*/
public function getNextItems(PagedList $pagedList)
Expand Down Expand Up @@ -553,6 +669,8 @@ private function buildRequestForPagedList(PagedList $pagedList)
*
* @param string $boardId
*
* @throws RateLimitedReached
*
* @return Response The response
*/
public function getBoardPins($boardId)
Expand All @@ -572,6 +690,8 @@ public function getBoardPins($boardId)
*
* @param string $pinId
*
* @throws RateLimitedReached
*
* @return Response The Response
*/
public function getPin($pinId)
Expand All @@ -591,6 +711,8 @@ public function getPin($pinId)
*
* @param Pin $pin
*
* @throws RateLimitedReached
*
* @return Response The response
*/
public function updatePin(Pin $pin)
Expand Down
4 changes: 2 additions & 2 deletions src/Pinterest/Http/Request.php
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ public function getHeaders()
*/
public function isPost()
{
return $this->getMethod() === 'post';
return $this->getMethod() === 'POST';
}

/**
Expand All @@ -141,6 +141,6 @@ public function isPost()
*/
public function isGet()
{
return $this->getMethod() === 'get';
return $this->getMethod() === 'GET';
}
}
15 changes: 9 additions & 6 deletions src/Pinterest/Http/Response.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,10 +68,12 @@ final class Response
/**
* The constructor.
*
* @param Request $request The request object.
* @param int $statusCode The status code.
* @param string $rawBody The raw response body.
* @param array $headers A key => value representation of response headers.
* @param Request $request The request object.
* @param int $statusCode The status code.
* @param string $rawBody The raw response body.
* @param array $headers A key => value representation of response headers.
*
* @throws MalformedJson
*/
public function __construct(Request $request, $statusCode, $rawBody, array $headers)
{
Expand All @@ -89,10 +91,11 @@ public function __construct(Request $request, $statusCode, $rawBody, array $head
*/
public function ok()
{
return
return (
!isset($this->body->error)
&& $this->statusCode >= 200
&& $this->statusCode < 300;
&& $this->statusCode < 300
);
}

/**
Expand Down
Loading

0 comments on commit e894cc4

Please sign in to comment.