Skip to content

Commit

Permalink
Added lists_trends patch from nocontents@gmail.com in lists_trends br…
Browse files Browse the repository at this point in the history
…anch for review

git-svn-id: http://svn.php.net/repository/pear/packages/Services_Twitter/branches/lists_trends@300014 c90b9560-bf6c-de11-be94-00142212c4b1
  • Loading branch information
Bill Shupp committed Jun 1, 2010
1 parent 6874efe commit 34548a5
Show file tree
Hide file tree
Showing 38 changed files with 470 additions and 16 deletions.
48 changes: 34 additions & 14 deletions Services/Twitter.php
Expand Up @@ -594,9 +594,8 @@ protected function prepareRequest($endpoint, array $args = array(), $cat = null)
// check if we have is a search or trends call, in this case the base
// uri is different
if ( $cat == 'search'
|| (string)$endpoint['name'] == 'search'
|| $cat == 'trends'
|| (string)$endpoint['name'] == 'trends'
|| ( $cat == 'trends'
&& !in_array((string)$endpoint['name'], array('available', 'location')))
) {
$uri = self::$searchUri;
} else {
Expand All @@ -613,8 +612,8 @@ protected function prepareRequest($endpoint, array $args = array(), $cat = null)
if ($cat !== null) {
$path .= $cat . '/';
}
$method = (string)$endpoint['method'];
$path .= (string)$endpoint['name'];
$method = (string)$endpoint['method'];

// check if we have a POST method and a registered source to pass
$source = $this->getOption('source');
Expand Down Expand Up @@ -645,6 +644,10 @@ protected function prepareRequest($endpoint, array $args = array(), $cat = null)
}
$needargs = $minargs;

$routing = array();
if (isset($endpoint['routing'])) {
$routing = explode('/', (string)$endpoint['routing']);
}
// now process arguments according to their definition in the xml
// mapping
foreach ($endpoint->param as $param) {
Expand All @@ -671,21 +674,26 @@ protected function prepareRequest($endpoint, array $args = array(), $cat = null)
$path
);
}
if ($pName == 'id') {
$path .= '/' . $arg;
if (in_array(':' . $pName, $routing)) {
$routing[array_search(':' . $pName, $routing)] = rawurlencode($arg);
} else {
if ($pType == 'string' && !$this->isUtf8($arg)) {
// iso-8859-1 string that we must convert to unicode
$arg = utf8_encode($arg);
}
if ($pType == 'image') {
// we have a file upload
$files[$pName] = $arg;
if ($pName == 'id') {
$path .= '/' . $arg;
} else {
$params[$pName] = $arg;
if ($pType == 'string' && !$this->isUtf8($arg)) {
// iso-8859-1 string that we must convert to unicode
$arg = utf8_encode($arg);
}
if ($pType == 'image') {
// we have a file upload
$files[$pName] = $arg;
} else {
$params[$pName] = $arg;
}
}
}
}
$path = count($routing) ? implode('/', $routing) : $path;
$uri .= $path . '.' . $this->getOption('format');
return array($uri, $method, $params, $files);
}
Expand Down Expand Up @@ -841,6 +849,18 @@ protected function validateArg($name, &$val, $type, $maxLength = null)
}
// XXX we don't check the image type for now...
break;
case 'listid_or_slug':
if (!preg_match('/^[0-9a-z]+(?:-?[0-9a-z]+)*$/', $val)) {
$msg = $name . ' must be a valid list id or slug';
}
break;
case 'mode':
$modes = array('public', 'private');
if (!in_array($val, $modes)) {
$msg = $name . ' must be one of the following: '
. implode(', ', $modes);
}
break;
}
if ($msg !== null) {
throw new Services_Twitter_Exception($msg, self::ERROR_PARAMS);
Expand Down
8 changes: 8 additions & 0 deletions data/api.rng
Expand Up @@ -30,6 +30,7 @@
<choice>
<value>GET</value>
<value>POST</value>
<value>DELETE</value>
</choice>
</attribute>
<attribute name="auth_required">
Expand All @@ -40,6 +41,11 @@
<data type="integer"/>
</attribute>
</optional>
<optional>
<attribute name="routing">
<data type="string"/>
</attribute>
</optional>
<interleave>
<optional>
<!-- formats are not used at the moment -->
Expand Down Expand Up @@ -67,6 +73,8 @@
<value>long</value>
<value>image</value>
<value>color</value>
<value>listid_or_slug</value>
<value>mode</value>
</choice>
</attribute>
<attribute name="required">
Expand Down
124 changes: 122 additions & 2 deletions data/api.xml
Expand Up @@ -121,8 +121,113 @@ http://pear.php.net/packages/Services_Twitter
<param name="cursor" type="integer" required="false"/>
</endpoint>
</category>

<!-- TODO add list, list members and list subscribers methods -->

<!-- List API methods -->
<category name="lists">
<endpoint name="create" method="POST" auth_required="true" min_args="2" routing="/:user/lists">
<formats>xml,json</formats>
<param name="user" type="id_or_screenname" required="true"/>
<param name="name" type="string" required="true"/>
<param name="mode" type="mode" required="false"/>
<param name="description" type="string" max_length="100" required="false"/>
</endpoint>
<endpoint name="update" method="POST" auth_required="true" min_args="2" routing="/:user/lists/:id">
<formats>xml,json</formats>
<param name="user" type="id_or_screenname" required="true"/>
<param name="id" type="listid_or_slug" required="true"/>
<param name="name" type="string" required="false"/>
<param name="mode" type="mode" required="false"/>
<param name="description" type="string" max_length="100" required="false"/>
</endpoint>
<endpoint name="list" method="GET" auth_required="true" min_args="1" routing="/:user/lists">
<formats>xml,json</formats>
<param name="user" type="id_or_screenname" required="true"/>
<param name="cursor" type="integer" required="false"/>
</endpoint>
<endpoint name="show" method="GET" auth_required="true" min_args="2" routing="/:user/lists/:id">
<formats>xml,json</formats>
<param name="user" type="id_or_screenname" required="true"/>
<param name="id" type="listid_or_slug" required="true"/>
</endpoint>
<endpoint name="delete" method="DELETE" auth_required="true" min_args="2" routing="/:user/lists/:id">
<formats>xml,json</formats>
<param name="user" type="id_or_screenname" required="true"/>
<param name="id" type="listid_or_slug" required="true"/>
</endpoint>
<endpoint name="statuses" method="GET" auth_required="false" min_args="1" routing="/:user/lists/:list_id/statuses">
<formats>xml,json,atom</formats>
<param name="user" type="id_or_screenname" required="true"/>
<param name="list_id" type="listid_or_slug" required="true"/>
<param name="since_id" type="integer" required="false"/>
<param name="max_id" type="integer" required="false"/>
<param name="per_page" type="integer" required="false"/>
<param name="page" type="integer" required="false"/>
</endpoint>
<endpoint name="memberships" method="GET" auth_required="true" min_args="1" routing="/:user/lists/memberships">
<formats>xml,json</formats>
<param name="user" type="id_or_screenname" required="true"/>
<param name="cursor" type="integer" required="false"/>
</endpoint>
<endpoint name="subscriptions" method="GET" auth_required="true" min_args="1" routing="/:user/lists/subscriptions">
<formats>xml,json</formats>
<param name="user" type="id_or_screenname" required="true"/>
<param name="cursor" type="integer" required="false"/>
</endpoint>
</category>

<!-- List members API methods -->
<category name="list_members">
<endpoint name="list" method="GET" auth_required="true" min_args="2" routing="/:user/:list_id/members">
<formats>xml,json</formats>
<param name="user" type="id_or_screenname" required="true"/>
<param name="list_id" type="listid_or_slug" required="true"/>
<param name="cursor" type="integer" required="false"/>
</endpoint>
<endpoint name="create" method="POST" auth_required="true" min_args="3" routing="/:user/:list_id/members">
<formats>xml,json</formats>
<param name="user" type="id_or_screenname" required="true"/>
<param name="list_id" type="listid_or_slug" required="true"/>
<param name="id" type="integer" required="true"/>
</endpoint>
<endpoint name="delete" method="DELETE" auth_required="true" min_args="3" routing="/:user/:list_id/members">
<formats>xml,json</formats>
<param name="user" type="id_or_screenname" required="true"/>
<param name="list_id" type="listid_or_slug" required="true"/>
<param name="id" type="integer" required="true"/>
</endpoint>
<endpoint name="show" method="GET" auth_required="true" min_args="3" routing="/:user/:list_id/members/:id">
<formats>xml,json</formats>
<param name="user" type="id_or_screenname" required="true"/>
<param name="list_id" type="listid_or_slug" required="true"/>
<param name="id" type="integer" required="true"/>
</endpoint>
</category>

<!-- List subscribers API methods -->
<category name="list_subscribers">
<endpoint name="list" method="GET" auth_required="true" min_args="2" routing="/:user/:list_id/subscribers">
<formats>xml,json</formats>
<param name="user" type="id_or_screenname" required="true"/>
<param name="list_id" type="listid_or_slug" required="true"/>
<param name="cursor" type="integer" required="false"/>
</endpoint>
<endpoint name="create" method="POST" auth_required="true" min_args="2" routing="/:user/:list_id/subscribers">
<formats>xml,json</formats>
<param name="user" type="id_or_screenname" required="true"/>
<param name="list_id" type="listid_or_slug" required="true"/>
</endpoint>
<endpoint name="delete" method="DELETE" auth_required="true" min_args="2" routing="/:user/:list_id/subscribers">
<formats>xml,json</formats>
<param name="user" type="id_or_screenname" required="true"/>
<param name="list_id" type="listid_or_slug" required="true"/>
</endpoint>
<endpoint name="show" method="GET" auth_required="true" min_args="3" routing="/:user/:list_id/subscribers/:id">
<formats>xml,json</formats>
<param name="user" type="id_or_screenname" required="true"/>
<param name="list_id" type="listid_or_slug" required="true"/>
<param name="id" type="integer" required="true"/>
</endpoint>
</category>

<!-- User methods -->
<category name="users">
Expand All @@ -132,6 +237,12 @@ http://pear.php.net/packages/Services_Twitter
<param name="user_id" type="id_or_screenname" required="false"/>
<param name="screen_name" type="id_or_screenname" required="false"/>
</endpoint>
<endpoint name="search" method="GET" auth_required="false">
<formats>xml,json</formats>
<param name="q" type="string" required="true"/>
<param name="per_page" type="integer" required="false"/>
<param name="page" type="integer" required="false"/>
</endpoint>
</category>

<!-- Direct message methods -->
Expand Down Expand Up @@ -378,6 +489,15 @@ http://pear.php.net/packages/Services_Twitter
<param name="date" type="date" required="false"/>
<param name="exclude" type="string" required="false"/>
</endpoint>
<endpoint name="available" method="GET" auth_required="false">
<formats>xml,json</formats>
<param name="lat" type="string" required="false"/>
<param name="long" type="string" required="false"/>
</endpoint>
<endpoint name="location" method="GET" auth_required="false" min_args="1" routing="/trends/:woeid">
<formats>xml,json</formats>
<param name="woeid" type="integer" required="true"/>
</endpoint>
</category>

</api>
18 changes: 18 additions & 0 deletions tests/150-lists-list.phpt
@@ -0,0 +1,18 @@
--TEST--
lists-list
--FILE--
<?php

require_once dirname(__FILE__) . '/setup.php';

try {
$twitter = Services_Twitter_factory('lists/list');
$list = $twitter->lists->list(array('user' => $config['user']));
var_dump($list instanceof stdclass && is_array($list->lists));
} catch (Services_Twitter_Exception $exc) {
echo $exc . "\n";
}

?>
--EXPECT--
bool(true)
18 changes: 18 additions & 0 deletions tests/151-lists-update.phpt
@@ -0,0 +1,18 @@
--TEST--
lists-update
--FILE--
<?php

require_once dirname(__FILE__) . '/setup.php';

try {
$twitter = Services_Twitter_factory('lists/update');
$list = $twitter->lists->update(array('user' => $config['user'], 'id' => $config['list_slug'], 'mode' => 'private'));
var_dump($list instanceof stdclass && is_int($list->id));
} catch (Services_Twitter_Exception $exc) {
echo $exc . "\n";
}

?>
--EXPECT--
bool(true)
18 changes: 18 additions & 0 deletions tests/152-lists-show.phpt
@@ -0,0 +1,18 @@
--TEST--
lists-show
--FILE--
<?php

require_once dirname(__FILE__) . '/setup.php';

try {
$twitter = Services_Twitter_factory('lists/show');
$list = $twitter->lists->show(array('user' => $config['user'], 'id' => $config['list_slug']));
var_dump($list instanceof stdclass && is_int($list->id));
} catch (Services_Twitter_Exception $exc) {
echo $exc . "\n";
}

?>
--EXPECT--
bool(true)
22 changes: 22 additions & 0 deletions tests/153-lists-create-delete.phpt
@@ -0,0 +1,22 @@
--TEST--
lists-create-delete
--FILE--
<?php

require_once dirname(__FILE__) . '/setup.php';

try {
$twitter = Services_Twitter_factory('lists/create');
$list = $twitter->lists->create(array('user' => $config['user'], 'name' => $config['list_name']));
var_dump($list instanceof stdclass && is_int($list->id));
$twitter = Services_Twitter_factory('lists/delete');
$delete = $twitter->lists->delete(array('user' => $config['user'], 'id' => $list->id));
var_dump($delete instanceof stdclass && is_int($delete->id));
} catch (Services_Twitter_Exception $exc) {
echo $exc . "\n";
}

?>
--EXPECT--
bool(true)
bool(true)
18 changes: 18 additions & 0 deletions tests/160-lists-statuses.phpt
@@ -0,0 +1,18 @@
--TEST--
lists-statuses
--FILE--
<?php

require_once dirname(__FILE__) . '/setup.php';

try {
$twitter = Services_Twitter_factory('lists/statuses');
$response = $twitter->lists->statuses(array('user' => $config['user'], 'list_id' => $config['list_slug']));
var_dump(is_array($response));
} catch (Services_Twitter_Exception $exc) {
echo $exc . "\n";
}

?>
--EXPECT--
bool(true)
18 changes: 18 additions & 0 deletions tests/161-lists-memberships.phpt
@@ -0,0 +1,18 @@
--TEST--
lists-memberships
--FILE--
<?php

require_once dirname(__FILE__) . '/setup.php';

try {
$twitter = Services_Twitter_factory('lists/memberships');
$response = $twitter->lists->memberships(array('user' => $config['user'], 'list_id' => $config['list_slug']));
var_dump($response instanceof stdclass && is_array($response->lists));
} catch (Services_Twitter_Exception $exc) {
echo $exc . "\n";
}

?>
--EXPECT--
bool(true)
18 changes: 18 additions & 0 deletions tests/162-lists-subscriptions.phpt
@@ -0,0 +1,18 @@
--TEST--
lists-subscriptions
--FILE--
<?php

require_once dirname(__FILE__) . '/setup.php';

try {
$twitter = Services_Twitter_factory('lists/subscriptions');
$response = $twitter->lists->subscriptions(array('user' => $config['user']));
var_dump($response instanceof stdclass && is_array($response->lists));
} catch (Services_Twitter_Exception $exc) {
echo $exc . "\n";
}

?>
--EXPECT--
bool(true)

0 comments on commit 34548a5

Please sign in to comment.