From e923bc5b2ecfd9346138c81f619a03ab2e38e223 Mon Sep 17 00:00:00 2001 From: Vincent Klaiber Date: Sun, 6 Nov 2016 20:27:36 +0100 Subject: [PATCH] Update library --- README.md | 8 ++++---- src/Instagram.php | 25 +++++++++---------------- tests/InstagramTest.php | 8 ++++---- 3 files changed, 17 insertions(+), 24 deletions(-) diff --git a/README.md b/README.md index 9f71f7b..67e2519 100644 --- a/README.md +++ b/README.md @@ -8,10 +8,10 @@ use Vinkla\Instagram\Instagram; // Create a new instagram instance. -$instagram = new Instagram('jerryseinfeld'); +$instagram = new Instagram(); // Fetch the media feed. -$instagram->get(); +$instagram->get('jerryseinfeld'); ``` [![Build Status](https://img.shields.io/travis/vinkla/instagram/master.svg?style=flat)](https://travis-ci.org/vinkla/instagram) @@ -35,13 +35,13 @@ First you need to create a new `Vinkla\Instagram\Instagram` instance. ```php use Vinkla\Instagram\Instagram; -$instagram = new Instagram('jerryseinfeld'); +$instagram = new Instagram(); ``` Then to fetch the data you can use the `get()` method. ```php -$instagram->get(); +$instagram->get('jerryseinfeld'); ``` To [preview the JSON data](https://www.instagram.com/jerryseinfeld/media) you can [visit the page](https://www.instagram.com/jerryseinfeld/media) in your browser. diff --git a/src/Instagram.php b/src/Instagram.php index a66a0cc..04299ef 100644 --- a/src/Instagram.php +++ b/src/Instagram.php @@ -25,13 +25,6 @@ */ class Instagram { - /** - * The username string. - * - * @var string - */ - protected $user; - /** * The guzzle http client. * @@ -42,34 +35,34 @@ class Instagram /** * Create a new instagram instance. * - * @param string $user * @param \GuzzleHttp\ClientInterface $client * * @return void */ - public function __construct(string $user, ClientInterface $client = null) + public function __construct(ClientInterface $client = null) { - $this->user = $user; $this->client = $client ?: new Client(); } /** - * Fetch the instagram media feed. + * Fetch the media items. + * + * @param string $user * * @throws \Vinkla\Instagram\Exceptions\NotFoundException * - * @return object + * @return array */ - public function get() + public function get(string $user): array { try { - $url = sprintf('https://www.instagram.com/%s/media', $this->user); + $url = sprintf('https://www.instagram.com/%s/media', $user); $response = $this->client->get($url); - return json_decode((string) $response->getBody()); + return json_decode((string) $response->getBody(), true)['items']; } catch (RequestException $e) { - throw new NotFoundException(sprintf('The user [%s] was not found.', $this->user)); + throw new NotFoundException(sprintf('The user [%s] was not found.', $user)); } } } diff --git a/tests/InstagramTest.php b/tests/InstagramTest.php index 7cdedaa..1907664 100644 --- a/tests/InstagramTest.php +++ b/tests/InstagramTest.php @@ -25,10 +25,10 @@ class InstagramTest extends TestCase { public function testGet() { - $response = (new Instagram('jerryseinfeld'))->get(); + $items = (new Instagram())->get('jerryseinfeld'); - $this->assertObjectHasAttribute('status', $response); - $this->assertSame('ok', $response->status); + $this->assertTrue(is_array($items)); + $this->assertCount(20, $items); } /** @@ -36,6 +36,6 @@ public function testGet() */ public function testNotFound() { - (new Instagram('imspeechlessihavenospeech'))->get(); + (new Instagram())->get('imspeechlessihavenospeech'); } }