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 #172 from seregazhuk/develop
Browse files Browse the repository at this point in the history
Fix: upload
  • Loading branch information
seregazhuk authored Sep 18, 2016
2 parents d76e1a5 + a56cdfc commit 8c2e3e9
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 5 deletions.
4 changes: 4 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.

## [4.11.2] - 2016-09-18
### Fixed:
- Images upload

## [4.11.1] - 2016-09-18
### Added:
- Bot *getClientInfo* method.
Expand Down
2 changes: 1 addition & 1 deletion src/Api/Request.php
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ public function upload($pathToFile, $url)
* @param string $resourceUrl
* @param string $postString
*
* @return Response
* @return string
*/
public function exec($resourceUrl, $postString = '')
{
Expand Down
9 changes: 9 additions & 0 deletions src/Api/Response.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,15 @@ public function getData($key = '', $default = null)
return $this->getValueByKey($key, $this->data, $default);
}

/**
* @param string $key
* @return bool
*/
public function hasData($key = '')
{
return !is_null($this->getValueByKey($key, $this->data));
}

/**
* Parse data from Pinterest Api response.
* Data is stored in ['resource_response']['data'] array.
Expand Down
10 changes: 8 additions & 2 deletions src/Api/Traits/UploadsImages.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace seregazhuk\PinterestBot\Api\Traits;

use seregazhuk\PinterestBot\Api\Request;
use seregazhuk\PinterestBot\Api\Response;
use seregazhuk\PinterestBot\Helpers\UrlBuilder;
use seregazhuk\PinterestBot\Exceptions\InvalidRequest;

Expand All @@ -11,6 +12,7 @@
* @package seregazhuk\PinterestBot\Api\Traits
*
* @property Request $request
* @property Response $response
*/
trait UploadsImages
{
Expand All @@ -23,9 +25,13 @@ trait UploadsImages
*/
public function upload($image)
{
$res = $this->request
$result = $this->request
->upload($image, UrlBuilder::IMAGE_UPLOAD);

return $res['success'] ? $res['image_url'] : null;
$this->processResult($result);

return $this->response->hasData('success') ?
$this->response->getData('image_url') :
null;
}
}
6 changes: 5 additions & 1 deletion tests/Api/PinsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,11 @@ public function it_should_upload_images_when_creating_pin_with_local_image()
$image = 'image.jpg';
$this->request
->shouldReceive('upload')
->withArgs([$image, UrlBuilder::IMAGE_UPLOAD]);
->withArgs([$image, UrlBuilder::IMAGE_UPLOAD])
->andReturn(json_encode([
'success' => true,
'image_url' => 'http://example.com/example.jpg'
]));

$response = $this->createPinCreationResponse();
$this->setResponseExpectation($response);
Expand Down
6 changes: 5 additions & 1 deletion tests/Api/UserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,11 @@ public function it_should_upload_image_when_editing_profile_with_local_image()
];
$this->request
->shouldReceive('upload')
->withArgs([$attributes['profile_image'], UrlBuilder::IMAGE_UPLOAD]);
->withArgs([$attributes['profile_image'], UrlBuilder::IMAGE_UPLOAD])
->andReturn(json_encode([
'success' => true,
'image_url' => 'http://example.com/example.jpg'
]));

$this->setSuccessResponse();
$this->assertTrue($this->provider->profile($attributes));
Expand Down

0 comments on commit 8c2e3e9

Please sign in to comment.