View Source

<ac:macro ac:name="note"><ac:parameter ac:name="title">Under Construction</ac:parameter><ac:rich-text-body>
<p>NOTICE<br />
This proposal is being revised and is not ready for review!</p></ac:rich-text-body></ac:macro>

<ac:macro ac:name="unmigrated-inline-wiki-markup"><ac:plain-text-body><![CDATA[{zone-template-instance:ZFDEV:Zend Proposal Zone Template}

{zone-data:component-name}
Zend_Service_Digg
{zone-data}

{zone-data:proposer-list}
[Luke Crouch|mailto:luke.crouch@gmail.com]
[Duo Zheng|~duoduo]
[Alexander Veremyev (Zend Liaison)|~alexander]
{zone-data}

{zone-data:revision}
1.1 - 21 October 2007: Updated from community comments.
{zone-data}

{zone-data:overview}
Zend_Service_Digg is a ZF implementation of popular digg API services.
{zone-data}

{zone-data:references}
* [Digg API docs|http://apidoc.digg.com/]
{zone-data}

{zone-data:requirements}
* This component *will* implement the non-draft [ListStories endpoints of the Digg API|http://apidoc.digg.com/ListStories]. (Add other endpoints later?)
* This component *will* support all query string parameters.
* This component *will* use an HTTP Accept header to specify json-serialized responses from Digg.
* This component *will not* implement any other endpoints until future releases.
{zone-data}

{zone-data:dependencies}
* Zend_Http_Client
* Zend_Json
{zone-data}

{zone-data:operation}
Instantiate an object of Zend_Service_Digg with any valid absolute URI in the constructor, which will be url-encoded and used as the mandatory appkey request parameter. Call getStories() to get an array of Zend_Service_Digg_Story objects, call getStory() to get an individual Zend_Service_Digg_Story object. getStories() has optional container, topic, and grouping (i.e., popular, upcoming, top, hot) parameters.
{zone-data}

{zone-data:milestones}
Describe some intermediate state of this component in terms of design notes, additional material added to this page, and / code. Note any significant dependencies here, such as, "Milestone #3 can not be completed until feature Foo has been added to ZF component XYZ." Milestones will be required for acceptance of future proposals. They are not hard, and many times you will only need to think of the first three below.
* Milestone 1: Working prototype checked into the incubator supporting use case #1
* Milestone 2: Unit tests exist, work, and are checked into SVN.
* Milestone 3: Initial documentation exists.
* Milestone 4: Add implementation for [ListEvents endpoints|http://apidoc.digg.com/ListEvents].
If a milestone is already done, begin the description with "\[DONE\]", like this:
* Milestone #: \[DONE\] Unit tests ...
{zone-data}

{zone-data:class-list}
* Zend_Service_Digg
* Zend_Service_Digg_DataElement
* Zend_Service_Digg_Exception
{zone-data}

{zone-data:use-cases}
{composition-setup}
{deck:id=Usecases}
{card:label=get stories}
{code}
$digg = new Zend_Service_Digg('http://framework.zend.com/');

// get stories
$stories = $digg->getStories();

// get stories by topic
$stories = $digg->getStories('programming');

// get stories by container
$stories = $digg->getStories(null,'technology');

// get stories by topic & "duggness"
$stories = $digg->getStories('programming',null,'popular');

// output stories (after any of the above calls)
foreach ($stories as $story) {
echo "story title: " . $story->title;
echo "story link: " . $story->link;
echo "story submitter: " . $story->user->name;
}
{code}
{card}
{card:label=set query params}
{code}
$digg = new Zend_Service_Digg('http://framework.zend.com/');
$digg->setQueryParam('count',5)->setQueryParam('domain','sourceforge.net');
// get stories
$stories = $digg->getStories();
{code}
{card}
{card:label=get stories by submitter}
{code}
$digg = new Zend_Service_Digg('http://framework.zend.com/');

// get all stories
$stories = $digg->getStoriesBySubmitter('groovepapa');

// output stories
foreach ($stories as $story) {
echo "story title: " . $story->title;
echo "story link: " . $story->link;
}
{code}
{card}
{card:label=get stories by ids}
{code}
$digg = new Zend_Service_Digg('http://framework.zend.com/');

// get all stories
$stories = $digg->getStoriesByIds(array(3766279,3444808));

// output stories
foreach ($stories as $story) {
echo "story title: " . $story->title;
echo "story link: " . $story->link;
}
{code}
{card}
{deck}
{zone-data}

{zone-data:skeletons}
{composition-setup}
{deck:id=Skeletons}
{card:label=Zend_Service_Digg}
{code}
<?php
/**
* Zend Framework
*
* LICENSE
*
* This source file is subject to the new BSD license that is bundled
* with this package in the file LICENSE.txt.
* It is also available through the world-wide-web at this URL:
* http://framework.zend.com/license/new-bsd
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to license@zend.com so we can send you a copy immediately.
*
* @category Zend
* @package Zend_Service
* @subpackage Digg
* @copyright Copyright (c) 2005-2007 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
* @version $Id: Digg.php 6673 2007-10-22 10:28:51Z lcrouch $
*/

/**
* @see Zend_Http_Client
*/
require_once 'Zend/Http/Client.php';

/**
* @see Zend_Http_Response
*/
require_once 'Zend/Http/Response.php';

/**
* @see Zend_Json
*/
require_once 'Zend/Json.php';

/**
* @see Zend_Service_Digg_Exception
*/
require_once 'Zend/Service/Digg/Exception.php';

/**
* @see Zend_Service_Digg_Story
*/
require_once 'Zend/Service/Digg/Story.php';

/**
* Zend_Service_Digg is an implementation of the digg api
*
* @category Zend
* @package Zend_Service
* @subpackage Digg
* @copyright Copyright (c) 2005-2007 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/
class Zend_Service_Digg
{
const API_URI = 'http://services.digg.com';

/**
* appkey holds the URI to use for the appkey
*
* @var string
*/
protected $_appkey = '';

/**
* httpClient is the Zend_Http_Client object used for the api calls
*
* @var Zend_Http_Client
*/
protected $_httpClient;

/**
* array for query params
*
* @var unknown_type
*/
protected $_queryParams = array();

/**
* Constructs a new digg api client
*
* @param string $appkeyUri URI to use as appkey identifier
* @return void
*/
public function __construct($appkeyUri)
{
$this->_appkey = $appkeyUri;
$this->_httpClient = new Zend_Http_Client(Zend_Service_Digg::API_URI);
}

/**
* get digg stories, optionally by topic or container and grouping
*
* @param string $topic
* @param string $container
* @param string $grouping
* @return array
*/
public function getStories($topic = null, $container = null, $grouping = null)
{
if(($topic != null) && ($container != null)) throw new Zend_Service_Digg_Exception('Cannot specify both topic and container.');
$endpoint = '/stories';
if($topic != null) {
$endpoint .= '/topic/' . $topic;
}
if($container != null) {
$endpoint .= '/container/' . $container;
}
if($grouping != null) {
$endpoint .= '/' . $grouping;
}
$storiesResponse = $this->makeRequest($endpoint);
return $this->parseStoriesResponse($storiesResponse['stories']);
}

/**
* get stories by submitter's username
*
* @param string $username
* @return array
*/
public function getStoriesBySubmitter($username)
{
if($username == null) throw new Zend_Service_Digg_Exception('Must supply username to get stories by submitter.');
$endpoint = '/user/' . $username . '/submissions';
$storiesResponse = $this->makeRequest($endpoint);
return $this->parseStoriesResponse($storiesResponse['stories']);
}

public function getStoriesByIds(array $storyIds)
{
if($storyIds == null) throw new Zend_Service_Digg_Exception('Must supply array of story ids to get stories by ids.');
$endpoint = '/stories/' . implode(',',$storyIds);
$storiesResponse = $this->makeRequest($endpoint);
return $this->parseStoriesResponse($storiesResponse['stories']);
}

public function getStoriesByDigger($username)
{
if($username == null) throw new Zend_Service_Digg_Exception('Must supply username to get stories by digger.');
$endpoint = '/user/' . $username . '/dugg';
$storiesResponse = $this->makeRequest($endpoint);
return $this->parseStoriesResponse($storiesResponse['stories']);
}

/**
* perform request to digg and decode JSON response
*
* @return array
*/
public function makeRequest($endpoint)
{
$uri = Zend_Service_Digg::API_URI . $endpoint . '?appkey=' . urlencode($this->_appkey);
foreach ($this->_queryParams as $param => $value){
$uri .= '&' . $param . '=' . $value;
}
$this->_httpClient->setUri($uri);
echo "making request to: <br/>" . $uri . "<br/>";
$this->_httpClient->setHeaders(array('Accept'=>'application/json'));
$response = $this->_httpClient->request();
$decodedJson = Zend_Json::decode($response->getBody());
return $decodedJson;
}

/**
* parse stories response into array of Zend_Service_Digg_Story objects
*
* @param array $storiesArray
* @return array
*/
public function parseStoriesResponse($storiesArray)
{
$zsds_array = array();
foreach($storiesArray as $story) {
$zsds= new Zend_Service_Digg_Story($story);
array_push($zsds_array, $zsds);
}
return $zsds_array;
}

/**
* return current appkey value
*
* @return string
*/
public function getAppkey()
{
return $this->_appkey;
}

/**
* return http client
*
* @return Zend_Http_Client
*/
public function getHttpClient()
{
return $this->_httpClient;
}

public function setQueryParam($key, $value)
{
$this->_queryParams[$key] = $value;
}

}
{code}
{card}
{card:label=Zend_Service_Digg_DataElement}
{code}
<?php

/**
* Zend Framework
*
* LICENSE
*
* This source file is subject to the new BSD license that is bundled
* with this package in the file LICENSE.txt.
* It is also available through the world-wide-web at this URL:
* http://framework.zend.com/license/new-bsd
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to license@zend.com so we can send you a copy immediately.
*
* @category Zend
* @package Zend_Service
* @subpackage Digg
* @copyright Copyright (c) 2005-2007 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
* @version $Id: Post.php 5384 2007-06-19 21:26:22Z darby $
*/


/**
* @see Zend_Date
*/
require_once 'Zend/Date.php';

/**
* Zend_Service_Delicious_Post represents a post of a user that can be edited
*
* @category Zend
* @package Zend_Service
* @subpackage Digg
* @copyright Copyright (c) 2005-2007 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/
class Zend_Service_Digg_DataElement
{
/**
* array property to hold all sub-elements
*
* @var array
*/
protected $_data = array();

/**
* construct object with data array
*
* @param array $data
*/
public function __construct(array $data)
{
$this->_data = $data;
}

/**
* magic get to access data array elements as object properties
*
* @param string $name
* @return string,array
*/
public function __get($name)
{
if(array_key_exists($name, $this->_data)){
if (is_array($this->_data[$name])){
return new Zend_Service_Digg_DataElement($this->_data[$name]);
} else {
return $this->_data[$name];
}
} else {
throw new Exception('Element, ' . $name . ', not found in story data.');
}
}

}
{code}
{card}
{card:label=Zend_Service_Digg_Exception}
{code}
<?php

/**
* Zend Framework
*
* LICENSE
*
* This source file is subject to the new BSD license that is bundled
* with this package in the file LICENSE.txt.
* It is also available through the world-wide-web at this URL:
* http://framework.zend.com/license/new-bsd
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to license@zend.com so we can send you a copy immediately.
*
* @category Zend
* @package Zend_Service
* @subpackage Digg
* @copyright Copyright (c) 2005-2007 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
* @version $Id: Digg.php 6673 2007-10-22 10:28:51Z lcrouch $
*
*/


/**
* @see Zend_Service_Exception
*/
require_once 'Zend/Service/Exception.php';


/**
* @category Zend
* @package Zend_Service
* @subpackage Digg
* @copyright Copyright (c) 2005-2007 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/
class Zend_Service_Digg_Exception extends Zend_Service_Exception
{}
{code}
{zone-data}

{zone-template-instance}]]></ac:plain-text-body></ac:macro>