Skip to content

Commit

Permalink
Merge pull request #3 from evgb/master
Browse files Browse the repository at this point in the history
endpoint simplifications
  • Loading branch information
marvinosswald authored Oct 18, 2017
2 parents 88bbb63 + 1f415a8 commit 239b4d3
Show file tree
Hide file tree
Showing 9 changed files with 96 additions and 109 deletions.
32 changes: 4 additions & 28 deletions src/Endpoints/Location.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,7 @@ public function get($id = '')
if ($id){
$this->id = $id;
}
$res = $this->instagram->get(Location::API_SEGMENT.$this->id);
$this->data = $res->data;
$this->meta = $res->meta;
return $this;
return $this->instagram->get(Location::API_SEGMENT.$this->id);
}

/**
Expand All @@ -61,17 +58,10 @@ public function recentMedia($minTagId='',$maxTagId='')
if(!$this->id){
return "No Location id set";
}
$res = $this->instagram->get(Location::API_SEGMENT.$this->id.'/media/recent',[
return $this->instagram->get(Location::API_SEGMENT.$this->id.'/media/recent',[
'min_tag_id' => $minTagId,
'max_tag_id' => $maxTagId
]);
$arr = [];
foreach ($res->data as $item){
$tag = new Media($this->instagram,$item->id);
$tag->setData($item);
array_push($arr,$tag);
}
return $arr;
}

/**
Expand All @@ -82,18 +72,11 @@ public function recentMedia($minTagId='',$maxTagId='')
*/
public function searchByCoordinates($lat, $lng, $distance=500)
{
$res = $this->instagram->get(Location::API_SEGMENT.'search',[
return $this->instagram->get(Location::API_SEGMENT.'search',[
'lat' => $lat,
'lng' => $lng,
'distance' => $distance
]);
$arr = [];
foreach ($res->data as $item){
$tag = new Location($this->instagram,$item->id);
$tag->setData($item);
array_push($arr,$tag);
}
return $arr;
}

/**
Expand All @@ -103,17 +86,10 @@ public function searchByCoordinates($lat, $lng, $distance=500)
*/
public function searchByFbPlacesId($fb_places_id='',$distance=500)
{
$res = $this->instagram->get(Location::API_SEGMENT.'search',[
return $this->instagram->get(Location::API_SEGMENT.'search',[
'facebook_places_id' => $fb_places_id,
'distance' => $distance
]);
$arr = [];
foreach ($res->data as $item){
$tag = new Location($this->instagram,$item->id);
$tag->setData($item);
array_push($arr,$tag);
}
return $arr;
}
/**
* @param $name
Expand Down
10 changes: 3 additions & 7 deletions src/Endpoints/Media.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,7 @@ public function get($id = false)
if ($id){
$this->id = $id;
}
$res = $this->instagram->get(Media::API_SEGMENT.$this->id);
$this->data = $res->data;
return $this;
return $this->instagram->get(Media::API_SEGMENT.$this->id);
}

/**
Expand All @@ -56,10 +54,7 @@ public function get($id = false)
*/
public function getByShortcode($shortcode)
{
$res = $this->instagram->get(Media::API_SEGMENT.'shortcode/'.$shortcode);
$this->id = $res->data->id;
$this->data = $res->data;
return $this;
return $this->instagram->get(Media::API_SEGMENT.'shortcode/'.$shortcode);
}

/**
Expand All @@ -76,6 +71,7 @@ public function search($lat,$lng,$distance='')
'distance' => $distance
]);
}

/**
* @return mixed|\Psr\Http\Message\ResponseInterface
*/
Expand Down
15 changes: 3 additions & 12 deletions src/Endpoints/Tag.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,7 @@ public function get($tagName = '')
if($tagName){
$this->tagName = $tagName;
}
$res = $this->instagram->get(Tag::API_SEGMENT.$this->tagName);
$this->data = $res->data;
return $this;
return $this->instagram->get(Tag::API_SEGMENT.$this->tagName);
}

/**
Expand All @@ -66,16 +64,9 @@ public function recentMedia($count = '',$minTagId='',$maxTagId='')

public function search($query)
{
$res = $this->instagram->get(Tag::API_SEGMENT.'search',['q' => $query]);

$arr = [];
foreach ($res->data as $item){
$tag = new Tag($this->instagram,$item->name);
$tag->setData($item);
array_push($arr,$tag);
}
return $arr;
return $this->instagram->get(Tag::API_SEGMENT.'search',['q' => $query]);
}

/**
* @param $name
* @return null
Expand Down
48 changes: 6 additions & 42 deletions src/Endpoints/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,23 +49,15 @@ public function get($id = false)
if($id){
$this->id = $id;
}
$res = $this->instagram->get(User::API_SEGMENT.$this->id);
if (isset($res->data)) {
$this->data = $res->data;
}
$this->meta = $res->meta;
return $this;
return $this->instagram->get(User::API_SEGMENT.$this->id);
}

/**
* @return mixed|\Psr\Http\Message\ResponseInterface
*/
public function self()
{
$res = $this->instagram->get(User::API_SEGMENT.'self');
$this->data = $res->data;
$this->meta = $res->meta;
return $this;
return $this->instagram->get(User::API_SEGMENT.'self');
}

/**
Expand All @@ -76,18 +68,11 @@ public function self()
*/
public function getMediaRecent($count='',$minId='',$maxId='')
{
$res = $this->instagram->get(User::API_SEGMENT.$this->id.'/media/recent',[
return $this->instagram->get(User::API_SEGMENT.$this->id.'/media/recent',[
'count' => $count,
'min_id' => $minId,
'max_id' => $maxId
]);
$arr = [];
foreach ($res->data as $item){
$media = new Media($this->instagram,$item->id);
$media->setData($item);
array_push($arr,$media);
}
return $arr;
}

/**
Expand All @@ -98,18 +83,11 @@ public function getMediaRecent($count='',$minId='',$maxId='')
*/
public function selfMediaRecent($count='',$minId='',$maxId='')
{
$res = $this->instagram->get(User::API_SEGMENT.'self/media/recent',[
return $this->instagram->get(User::API_SEGMENT.'self/media/recent',[
'count' => $count,
'min_id' => $minId,
'max_id' => $maxId
]);
$arr = [];
foreach ($res->data as $item){
$media = new Media($this->instagram,$item->id);
$media->setData($item);
array_push($arr,$media);
}
return $arr;
}

/**
Expand All @@ -119,17 +97,10 @@ public function selfMediaRecent($count='',$minId='',$maxId='')
*/
public function selfMediaLiked($count = '',$maxLikeId='')
{
$res = $this->instagram->get(User::API_SEGMENT.'self/media/liked',[
return $this->instagram->get(User::API_SEGMENT.'self/media/liked',[
'count' => $count,
'max_like_id' => $maxLikeId
]);
$arr = [];
foreach ($res->data as $item){
$media = new Media($this->instagram,$item->id);
$media->setData($item);
array_push($arr,$media);
}
return $arr;
}

/**
Expand All @@ -139,17 +110,10 @@ public function selfMediaLiked($count = '',$maxLikeId='')
*/
public function search($q,$count = false)
{
$res = $this->instagram->get(User::API_SEGMENT.'search',[
return $this->instagram->get(User::API_SEGMENT.'search',[
'q' => $q,
'count' => $count ?: ''
]);
$arr = [];
foreach ($res->data as $item){
$user = new User($this->instagram,$item->id);
$user->setData($item);
array_push($arr,$user);
}
return $arr;
}

/**
Expand Down
25 changes: 20 additions & 5 deletions tests/LocationTest.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
<?php

use \PHPUnit\Framework\TestCase;
use GuzzleHttp\Exception\ClientException;
use marvinosswald\Instagram\Instagram;

class LocationTest extends TestCase{
protected static $instagram;
Expand All @@ -11,31 +13,44 @@ public static function setUpBeforeClass()
$dotenv = new Dotenv\Dotenv(__DIR__."/../");
$dotenv->load();
}
self::$instagram = new \marvinosswald\Instagram\Instagram(['accessToken' => getenv('INSTAGRAM_ACCESS_TOKEN')]);
self::$instagram = new Instagram(['accessToken' => getenv('INSTAGRAM_ACCESS_TOKEN')]);
}

public function testGet()
{
$res = $this::$instagram->location(213385402)->get();
$this->assertEquals(213385402,$res->id);
$this->assertEquals(213385402,$res->data->id);

}

public function testRecentMedia()
{
$res = $this::$instagram->location(213385402)->recentMedia();
$this->assertInternalType('array',$res);
$this->assertEquals(200,$res->meta->code);
}

public function testSearchByFbPlacesId()
{
$res = $this::$instagram->location()->searchByFbPlacesId('106078429431815');
$this->assertEquals(213385402,$res[0]->id);
$this->assertEquals(213385402,$res->data[0]->id);
}

public function testSearchByCoordinates()
{
$res = $this::$instagram->location()->searchByCoordinates('51.518732','-0.129756');
$this->assertEquals(213385402,$res[2]->id);
$this->assertEquals(213385402,$res->data[2]->id);
}
public function testGetClientException()
{
$this->expectException(ClientException::class);
$this::$instagram->location(12345)->get();
}
public function testGetAPIError400()
{
$instagram = new Instagram(['accessToken' => getenv('INSTAGRAM_ACCESS_TOKEN')], ['http_errors' => false]);
$res = $instagram->location(12345)->get();
$this->assertEquals($res->meta->code,'400');
$this->assertInternalType('string', $res->meta->error_type);
$this->assertInternalType('string', $res->meta->error_message);
}
}
21 changes: 18 additions & 3 deletions tests/MediaTest.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
<?php

use \PHPUnit\Framework\TestCase;
use GuzzleHttp\Exception\ClientException;
use marvinosswald\Instagram\Instagram;

class MediaTest extends TestCase{
protected static $instagram;
Expand All @@ -11,23 +13,36 @@ public static function setUpBeforeClass()
$dotenv = new Dotenv\Dotenv(__DIR__."/../");
$dotenv->load();
}
self::$instagram = new \marvinosswald\Instagram\Instagram(['accessToken' => getenv('INSTAGRAM_ACCESS_TOKEN')]);
self::$instagram = new Instagram(['accessToken' => getenv('INSTAGRAM_ACCESS_TOKEN')]);
}

public function testGet()
{
$res = $this::$instagram->media('503364100596583201_45913985')->get();
$this->assertEquals('503364100596583201_45913985',$res->id);
$this->assertEquals('503364100596583201_45913985',$res->data->id);
}
public function testGetByShortcode()
{
$res = $this::$instagram->media()->getByShortcode('b8TvuIvksh');
$this->assertEquals('503364100596583201_45913985',$res->id);
$this->assertEquals('503364100596583201_45913985',$res->data->id);
}

public function testGetLikes()
{
$res = $this::$instagram->media('503364100596583201_45913985')->likes();
$this->assertEquals(200,$res->meta->code);
}
public function testGetClientException()
{
$this->expectException(ClientException::class);
$this::$instagram->media('dummy_media_id')->get();
}
public function testGetAPIError400()
{
$instagram = new Instagram(['accessToken' => getenv('INSTAGRAM_ACCESS_TOKEN')], ['http_errors' => false]);
$res = $instagram->media('dummy_media_id')->get();
$this->assertEquals($res->meta->code,'400');
$this->assertInternalType('string', $res->meta->error_type);
$this->assertInternalType('string', $res->meta->error_message);
}
}
18 changes: 17 additions & 1 deletion tests/RelationshipsTest.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
<?php

use \PHPUnit\Framework\TestCase;
use GuzzleHttp\Exception\ClientException;
use marvinosswald\Instagram\Instagram;

class RelationshipsTest extends TestCase{
protected static $instagram;
Expand All @@ -11,7 +13,7 @@ public static function setUpBeforeClass()
$dotenv = new Dotenv\Dotenv(__DIR__."/../");
$dotenv->load();
}
self::$instagram = new \marvinosswald\Instagram\Instagram(['accessToken' => getenv('INSTAGRAM_ACCESS_TOKEN')]);
self::$instagram = new Instagram(['accessToken' => getenv('INSTAGRAM_ACCESS_TOKEN')]);
}
public function testFollows()
{
Expand All @@ -28,4 +30,18 @@ public function testFollowingRequests()
$res = $this::$instagram->relationships->followingRequests();
$this->assertEquals(200,$res->meta->code);
}
public function testGetClientException()
{
$this->expectException(ClientException::class);
$instagram = new Instagram(['accessToken' => "dummy_token"], ['http_errors' => true]);
$instagram->relationships->follows();
}
public function testGetAPIError400()
{
$instagram = new Instagram(['accessToken' => "dummy_token"], ['http_errors' => false]);
$res = $instagram->relationships->follows();
$this->assertEquals($res->meta->code,'400');
$this->assertInternalType('string', $res->meta->error_type);
$this->assertInternalType('string', $res->meta->error_message);
}
}
Loading

0 comments on commit 239b4d3

Please sign in to comment.