dribbble api datasource for cakephp

David Jones
@david3jones
avatar-davidejones

I’ve been watching the site dribbble for sometime, although I’m not able to post any shots due to the lack of any invites i thoroughly enjoy looking at what ideas and concepts designers are working on and these can often inspire me in my own work. The discovery of the dribbble api which i only happened to notice a few weeks ago led me to create my own datasource for cakephp to utilize this beta api. The cakephp datasource is still an early release but covers most of the data in a fairly easy format, ideally it needs to be more standardized and better model connection for shots and comments etc but its a good start and hopefully will provide some use to another cakephp developer. The usual rules apply, first off you need to setup a connection in your database like so in the config/database.php file

<?php
class DATABASE_CONFIG {
    	var $dribbble = array(
    		'datasource' => 'dribbble'
    	);
}
?>

Then we need to setup the models and datasource. models/dribbbleplayer.php

<?php
class Dribbbleplayer extends AppModel {
    	public $useDbConfig = 'dribbble';
    	public $name = "Dribbbleplayer";
}
?>

models/dribbbleplayer.php

<?php
class Dribbbleshot extends AppModel {
    	public $useDbConfig = 'dribbble';
    	public $name = "Dribbbleshot";
}
?>

models/datasources/dribbble_source.php

<?php
App::import('Core', 'HttpSocket');
class DribbbleSource extends DataSource {
    	public $description = "Dribbble API Data Source";
    	public $version = '0.1';
    	protected $_schema = array(
            'dribbbleshots' => array(
    			'id' => array('type' => 'Integer'),
    			'title' => array('type' => 'String'),
    			'short_url' => array('type' => 'String'),
    			'image_url' => array('type' => 'String'),
    			'image_teaser_url' => array('type' => 'String'),
    			'width' => array('type' => 'Integer'),
    			'height' => array('type' => 'Integer'),
    			'views_count' => array('type' => 'Integer'),
    			'likes_count' => array('type' => 'Integer'),
    			'comments_count' => array('type' => 'Integer'),
    			'rebounds_count' => array('type' => 'Integer'),
    			'rebound_source_id' => array('type' => 'Integer'),
    			'created_at' => array('type' => 'Integer')
    		),
    		'dribbbleplayers' => array(
    			'id' => array('type' => 'integer'),
    			'name' => array('type' => 'String'),
    			'username' => array('type' => 'String'),
    			'url' => array('type' => 'String'),
    			'avatar_url' => array('type' => 'String'),
    			'location' => array('type' => 'String'),
    			'twitter_screen_name' => array('type' => 'integer'),
    			'drafted_by_player_id' => array('type' => 'integer'),
    			'shots_count' => array('type' => 'integer'),
    			'draftees_count' => array('type' => 'integer'),
    			'followers_count' => array('type' => 'integer'),
    			'following_count' => array('type' => 'String'),
    			'comments_count' => array('type' => 'String'),
    			'comments_received_count' => array('type' => 'integer'),
    			'likes_count' => array('type' => 'integer'),
    			'likes_received_count' => array('type' => 'String'),
    			'rebounds_count' => array('type' => 'String'),
    			'rebounds_received_count' => array('type' => 'String'),
    			'created_at' => array('type' => 'String')
    		)
        );
    	function __construct($config) {
    		$this->connection = new HttpSocket('http://api.dribbble.com');
    		parent::__construct($config);
    	}
    	public function listSources() {
    		return array('dribbbleshots','dribbbleplayers');
    	}
    	public function read(&$model, $queryData = array())
    	{
    		$results = array();
    		$data = array();
    		if (isset($queryData['conditions']['page'])) {
    			$data['page'] = $queryData['conditions']['page'];
    		}
    		if (isset($queryData['conditions']['per_page'])) {
    			$data['per_page'] = $queryData['conditions']['per_page'];
    		}
    		$u = http_build_query($data);
    		if(strlen($u) > 0) {
    			$pag = "?".$u;
    		} else {
    			$pag = "";
    		}
    		switch($model->useTable)
    		{
    			case "dribbbleshots":
    				//default
    				$url = "/shots/";
    				//for find by id
    				if(isset($queryData['conditions']['id'])) {
    					$url = "/shots/".$queryData['conditions']['id'];
    					//for rebounds
    					if(isset($queryData['conditions']['reboundsonly']))
    					{
    						if($queryData['conditions']['reboundsonly'])
    						{
    							$url = "/shots/".$queryData['conditions']['id']."/rebounds";
    						}
    					}
    					//for comments
    					if(isset($queryData['conditions']['commentsonly']))
    					{
    						if($queryData['conditions']['commentsonly'])
    						{
    							$url = "/shots/".$queryData['conditions']['id']."/comments";
    						}
    					}
    				}
    				//for list
    				if(isset($queryData['conditions']['list'])) {
    					if(in_array($queryData['conditions']['list'],array('everyone','debuts','popular')))
    					{
    						$url = "/shots/".$queryData['conditions']['list'];
    					}
    				}
    				$url .= $pag;
    				$response = json_decode($this->connection->get($url), true);
    				$results[$model->useTable] = array();
    				if(isset($response['shots']))
    				{
    					foreach($response['shots'] as $shots)
    					{
    						$results[$model->useTable][] = $shots;
    					}
    				} else {
    					$results[$model->useTable] = $response;
    				}
    				break;
    			case "dribbbleplayers":
    				//default
    				$url = "/players/";
    				//for find by id
    				if(isset($queryData['conditions']['id'])) {
    					$url = "/players/".$queryData['conditions']['id'];
    				}
    				if(isset($queryData['conditions']['PlayerId'])) {
    					$url = "/players/".$queryData['conditions']['PlayerId']."/shots";
    				}
    				if(isset($queryData['conditions']['FollowPlayerId'])) {
    					$url = "/players/".$queryData['conditions']['FollowPlayerId']."/shots/following";
    				}
    				if(isset($queryData['conditions']['LikesPlayerId'])) {
    					$url = "/players/".$queryData['conditions']['LikesPlayerId']."/shots/likes";
    				}
    				if(isset($queryData['conditions']['FollowersPlayerId'])) {
    					$url = "/players/".$queryData['conditions']['FollowersPlayerId']."/shots/followers";
    				}
    				if(isset($queryData['conditions']['PlayersFollowedPlayerId'])) {
    					$url = "/players/".$queryData['conditions']['PlayersFollowedPlayerId']."/following";
    				}
    				if(isset($queryData['conditions']['PlayersDrafteesPlayerId'])) {
    					$url = "/players/".$queryData['conditions']['PlayersDrafteesPlayerId']."/draftees";
    				}
    				$url .= $pag;
    				$response = json_decode($this->connection->get($url), true);
    				$results[$model->useTable] = array();
    				//find by id
    				$results[$model->useTable] = $response;
    				break;
    		}
    		return $results;
    	}
    	public function query($method, $args, &$model)
    	{
    		if (strpos(strtolower($method), 'findby') === 0) {
    			$field = Inflector::underscore(preg_replace('/^findBy/i', '', $method));
    			if ($field == 'id') {
    				$field = $model->primaryKey;
    			}
    			return $model->find('all', array('limit'=>1,'conditions' => array($field => current($args))));
    			#return $model->find('first', array('conditions' => array($field => current($args))));
    		} else {
    			switch($method)
    			{
    				case "findShotsByPlayerId":
    					return $model->find('all', array('limit'=>1,'conditions' => array('PlayerId' => current($args))));
    					break;
    				case "findFollowersShots":
    					return $model->find('all', array('limit'=>1,'conditions' => array('FollowPlayerId' => current($args))));
    					break;
    				case "findLikedShots":
    					return $model->find('all', array('limit'=>1,'conditions' => array('LikesPlayerId' => current($args))));
    					break;
    				case "findPlayerFollowers":
    					return $model->find('all', array('limit'=>1,'conditions' => array('FollowersPlayerId' => current($args))));
    					break;
    				case "findPlayersFollowed":
    					return $model->find('all', array('limit'=>1,'conditions' => array('PlayersFollowedPlayerId' => current($args))));
    					break;
    				case "findPlayersDraftees":
    					return $model->find('all', array('limit'=>1,'conditions' => array('PlayersDrafteesPlayerId' => current($args))));
    					break;
    			}
    		}
    	}
    	public function describe($model) {
    		#return $model->_schema;
    		return $this->_schema['dribbbleshots'];
    	}
}
?>

..and finally a controller to make use of the new dribbble data

<?php
class TestsController extends AppController {
    	var $name = 'Tests';
    	var $uses = array('Dribbbleshot','Dribbbleplayer');
    	function index()
    	{
    		# SHOTS
    		# Find all shots
    		# pr($this->Dribbbleshot->find('all'));
    		# Find all shots with pagination, 3 at a time and page 2
    		# pr($this->Dribbbleshot->find('all',array('conditions'=>array('per_page'=>3,'page'=>2))));
    		# Find shot by shots id
    		# pr($this->Dribbbleshot->findById(21603));
    		# Find list of shots - debuts, everyone, popular
    		# pr($this->Dribbbleshot->find('all',array('conditions'=>array('list'=>'everyone'))));
    		# Find rebounds (shots in response to a shot) specified by id
    		# pr($this->Dribbbleshot->find('all',array('conditions'=>array('id'=>21603,'reboundsonly'=>true))));
    		# Find comments for a shot specified by id
    		# pr($this->Dribbbleshot->find('all',array('conditions'=>array('id'=>21603,'commentsonly'=>true))));
    		# PLAYERS
    		# Find player details by id
    		# pr($this->Dribbbleplayer->findById(1));
    		# pr($this->Dribbbleplayer->findById("simplebits"));
    		# Find shots by player id
    		# pr($this->Dribbbleplayer->findShotsByPlayerId(1));
    		# pr($this->Dribbbleplayer->findShotsByPlayerId("simplebits"));
    		# Find shots by followers of an player id
    		# pr($this->Dribbbleplayer->findFollowersShots(1));
    		# pr($this->Dribbbleplayer->findFollowersShots("simplebits"));
    		# Find shots liked by player id
    		# pr($this->Dribbbleplayer->findLikedShots(1));
    		# pr($this->Dribbbleplayer->findLikedShots("simplebits"));
    		# Find the list of followers for a player id
    		# pr($this->Dribbbleplayer->findPlayerFollowers(1));
    		# pr($this->Dribbbleplayer->findPlayerFollowers("simplebits"));
    		# Find the list of players followed by the id
    		# pr($this->Dribbbleplayer->findPlayersFollowed(1));
    		# pr($this->Dribbbleplayer->findPlayersFollowed("simplebits"));
    		# Find the list of players drafted by the player id
    		# pr($this->Dribbbleplayer->findPlayersDraftees(1));
    		# pr($this->Dribbbleplayer->findPlayersDraftees("simplebits"));
    		$this->autoRender = false;
    		exit();
    	}
}
?>

The files can be downloaded from github here https://github.com/davidejones/dribbble-cakephp

Comments

    Comments are currently closed