Skip to content

Commit eda4f3a

Browse files
author
Luciano Mammino
committed
Added Instagram extractor
1 parent 96b804f commit eda4f3a

File tree

3 files changed

+281
-1
lines changed

3 files changed

+281
-1
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,8 +91,9 @@ $ php composer.phar install
9191
Currently there are extractors for the following providers:
9292

9393
- Facebook
94-
- Twitter
94+
- Instagram
9595
- Linkedin
96+
- Twitter
9697

9798
More to come (obviously)! Want to [contribute](#how-to-contribute)?
9899

Lines changed: 139 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,139 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Oryzone PHPoAuthUserData package <https://github.com/Oryzone/PHPoAuthUserData>.
5+
*
6+
* (c) Oryzone, developed by Luciano Mammino <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace OAuth\UserData\Extractor;
13+
14+
use OAuth\UserData\Utils\ArrayUtils;
15+
16+
/**
17+
* Class Instagram
18+
* @package OAuth\UserData\Extractor
19+
*/
20+
class Instagram extends LazyExtractor
21+
{
22+
23+
const REQUEST_PROFILE = '/users/self';
24+
25+
/**
26+
* Constructor
27+
*/
28+
public function __construct()
29+
{
30+
parent::__construct(
31+
self::getDefaultLoadersMap(),
32+
self::getDefaultNormalizersMap(),
33+
self::getSupportedFields()
34+
);
35+
}
36+
37+
protected static function getSupportedFields()
38+
{
39+
return array(
40+
self::FIELD_UNIQUE_ID,
41+
self::FIELD_USERNAME,
42+
self::FIELD_FULL_NAME,
43+
self::FIELD_FIRST_NAME,
44+
self::FIELD_LAST_NAME,
45+
self::FIELD_DESCRIPTION,
46+
self::FIELD_WEBSITES,
47+
self::FIELD_IMAGE_URL,
48+
self::FIELD_PROFILE_URL,
49+
self::FIELD_EXTRA
50+
);
51+
}
52+
53+
protected function profileLoader()
54+
{
55+
return json_decode($this->service->request(self::REQUEST_PROFILE), true);
56+
}
57+
58+
protected function uniqueIdNormalizer($data)
59+
{
60+
return isset($data['data']['id']) ? $data['data']['id'] : null;
61+
}
62+
63+
protected function usernameNormalizer($data)
64+
{
65+
return isset($data['data']['username']) ? $data['data']['username'] : null;
66+
}
67+
68+
protected function fullNameNormalizer($data)
69+
{
70+
return isset($data['data']['full_name']) ? $data['data']['full_name'] : null;
71+
}
72+
73+
protected function firstNameNormalizer()
74+
{
75+
$fullName = $this->getField(self::FIELD_FULL_NAME);
76+
if ($fullName) {
77+
$names = explode(' ', $fullName);
78+
79+
return $names[0];
80+
}
81+
82+
return null;
83+
}
84+
85+
protected function lastNameNormalizer()
86+
{
87+
$fullName = $this->getField(self::FIELD_FULL_NAME);
88+
if ($fullName) {
89+
$names = explode(' ', $fullName);
90+
91+
return $names[sizeof($names) - 1];
92+
}
93+
94+
return null;
95+
}
96+
97+
protected function descriptionNormalizer($data)
98+
{
99+
return isset($data['data']['bio']) ? $data['data']['bio'] : null;
100+
}
101+
102+
protected function websitesNormalizer($data)
103+
{
104+
$websites = array();
105+
if (isset($data['data']['website'])) {
106+
$websites[] = $data['data']['website'];
107+
}
108+
109+
return $websites;
110+
}
111+
112+
protected function profileUrlNormalizer()
113+
{
114+
$username = $this->getField(self::FIELD_USERNAME);
115+
116+
if (null !== $username) {
117+
return sprintf('http://instagram.com/%s', $username);
118+
}
119+
120+
return null;
121+
}
122+
123+
protected function imageUrlNormalizer($data)
124+
{
125+
return isset($data['data']['profile_picture']) ? $data['data']['profile_picture'] : null;
126+
}
127+
128+
protected function extraNormalizer($data)
129+
{
130+
return ArrayUtils::removeKeys($data['data'], array(
131+
'id',
132+
'username',
133+
'full_name',
134+
'website',
135+
'profile_picture',
136+
'bio',
137+
));
138+
}
139+
}
Lines changed: 140 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
1+
<?php
2+
3+
namespace OAuth\Unit\UserData\Extractor;
4+
5+
use OAuth\UserData\Extractor\Instagram;
6+
7+
/**
8+
* Generated by PHPUnit_SkeletonGenerator 1.2.1 on 2014-02-10 at 16:20:53.
9+
*/
10+
class InstagramTest extends \PHPUnit_Framework_TestCase
11+
{
12+
const PROFILE_RESPONSE =
13+
'{
14+
"meta": {
15+
"code": 200
16+
},
17+
"data": {
18+
"username": "johnnydonny",
19+
"bio": "A life on the edge",
20+
"website": "http://blog.johnnydonny.com",
21+
"profile_picture": "http://images.ak.instagram.com/profiles/profile_weird_numbers.jpg",
22+
"full_name": "John Doe",
23+
"counts": {
24+
"media": 131,
25+
"followed_by": 80,
26+
"follows": 64
27+
},
28+
"id": "1111222333"
29+
}
30+
}';
31+
32+
/**
33+
* @var Instagram
34+
*/
35+
protected $extractor;
36+
37+
/**
38+
* Sets up the fixture, for example, opens a network connection.
39+
* This method is called before a test is executed.
40+
*/
41+
protected function setUp()
42+
{
43+
$this->extractor = new Instagram();
44+
$service = $this->getMockBuilder('\\OAuth\\OAuth2\\Service\\Instagram')
45+
->disableOriginalConstructor()
46+
->getMock();
47+
$service->expects($this->any())
48+
->method('request')
49+
->with(Instagram::REQUEST_PROFILE)
50+
->will($this->returnValue(InstagramTest::PROFILE_RESPONSE));
51+
/**
52+
* @var \OAuth\Common\Service\ServiceInterface $service
53+
*/
54+
$this->extractor->setService($service);
55+
}
56+
57+
/**
58+
* Tears down the fixture, for example, closes a network connection.
59+
* This method is called after a test is executed.
60+
*/
61+
protected function tearDown()
62+
{
63+
}
64+
65+
public function testDoesNotSupportEmail()
66+
{
67+
$this->assertFalse($this->extractor->supportsEmail());
68+
$this->assertNull($this->extractor->getEmail());
69+
}
70+
71+
public function testDoesNotSupportVerifiedEmail()
72+
{
73+
$this->assertFalse($this->extractor->supportsVerifiedEmail());
74+
$this->assertNull($this->extractor->isEmailVerified());
75+
}
76+
77+
public function testDoesNotSupportLocation()
78+
{
79+
$this->assertFalse($this->extractor->supportsLocation());
80+
$this->assertNull($this->extractor->getLocation());
81+
}
82+
83+
public function testGetUniqueId()
84+
{
85+
$this->assertEquals('1111222333', $this->extractor->getUniqueId());
86+
}
87+
88+
public function testUsername()
89+
{
90+
$this->assertEquals('johnnydonny', $this->extractor->getUsername());
91+
}
92+
93+
public function testGetFirstName()
94+
{
95+
$this->assertEquals('John', $this->extractor->getFirstName());
96+
}
97+
98+
public function testGetLastName()
99+
{
100+
$this->assertEquals('Doe', $this->extractor->getLastName());
101+
}
102+
103+
public function testGetFullName()
104+
{
105+
$this->assertEquals('John Doe', $this->extractor->getFullName());
106+
}
107+
108+
public function testGetDescription()
109+
{
110+
$this->assertEquals('A life on the edge', $this->extractor->getDescription());
111+
}
112+
113+
public function testGetImageUrl()
114+
{
115+
$this->assertEquals('http://images.ak.instagram.com/profiles/profile_weird_numbers.jpg', $this->extractor->getImageUrl());
116+
}
117+
118+
public function testGetProfileUrl()
119+
{
120+
$this->assertEquals('http://instagram.com/johnnydonny', $this->extractor->getProfileUrl());
121+
}
122+
123+
public function testGetWebsites()
124+
{
125+
$expected = array(
126+
'http://blog.johnnydonny.com'
127+
);
128+
$this->assertEquals($expected, $this->extractor->getWebsites());
129+
}
130+
131+
public function testGetExtras()
132+
{
133+
$extras = $this->extractor->getExtras();
134+
$this->assertArrayHasKey('counts', $extras);
135+
136+
$this->assertArrayNotHasKey('id', $extras);
137+
$this->assertArrayNotHasKey('bio', $extras);
138+
$this->assertArrayNotHasKey('website', $extras);
139+
}
140+
}

0 commit comments

Comments
 (0)