06.10 php saved
gwoo
Tags add more
 
Note
gwoo did not leave a note
  1. <?php
  2. /* SVN FILE: $Id$ */
  3. /* Ohloh Test cases generated on: 2008-06-07 18:06:54 : 1212887334*/
  4. App::import('Component', 'Ohloh');
  5. class TestOhloh extends OhlohComponent {
  6. }
  7.  
  8. class OhlohComponentTest extends CakeTestCase {
  9.  
  10.     function start() {
  11.         parent::start();
  12.         $this->Ohloh = new TestOhloh();
  13.     }
  14.  
  15.     function testOhlohInstance() {
  16.         $this->assertTrue(is_a($this->Ohloh, 'OhlohComponent'));
  17.     }
  18.    
  19.     function testProject() {
  20.         $result = $this->Ohloh->Project('cakephp');
  21.        
  22.         $this->assertEqual($result['Project']['id'], '3176');
  23.         $this->assertEqual($result['Project']['name'], 'CakePHP');
  24.        
  25.         $result = $this->Ohloh->Project(array('project_id' => 'cakephp'));
  26.        
  27.         $this->assertEqual($result['Project']['id'], '3176');
  28.         $this->assertEqual($result['Project']['name'], 'CakePHP');
  29.     }
  30. }
  31. ?>
  32. <?php
  33. /**
  34. * Ohloh Api Access
  35. *
  36. * @package default
  37. * @author gwoo
  38. **/
  39. App::import(array('Overloadable', 'HttpSocket', 'Xml'));
  40.  
  41. class OhlohComponent extends Overloadable {
  42.  
  43.     var $config = array('user' => '', 'key' => '', 'version' => 1);
  44.  
  45.     var $__gets = array(
  46.         'Account' => '/accounts/{account_id}.xml',
  47.         'AccountByHash' => '/accounts/{email_md5_hash}.xml',
  48.         'Accounts' => '/accounts.xml',
  49.         'ActivityFacts' => '/projects/{project_id}/analyses/{analysis_id}/activity_facts.xml',
  50.         'ActivityLatest' => '/projects/{project_id}/analyses/latest/activity_facts.xml',
  51.         'ContributorFact' => "/projects/{project_id}/contributors/{contributor_id}.xml",
  52.         'ContributorFacts' => "/projects/{project_id}/contributors.xml",
  53.         'ContributorLanguageFacts' => '/projects/{project_id}/contributors/{contributor_id}.xml',
  54.         'Enlistment' => '/projects/{project_id}/enlistment/{enlistment_id}.xml',
  55.         'Enlistments' => '/projects/{project_id}/enlistment/{enlistment_id}.xml',
  56.         'Factoid' => '/projects/{project_id}/factoids/{factoid_id}.xml',
  57.         'Factoids' => '/projects/{project_id}/factoids.xml',
  58.         'KudosReceived' => '/accounts/{account_id}/kudos.xml',
  59.         'KudosSent' => '/accounts/{account_id}/kudos/sent.xml',
  60.         'Language' => '/languages/{language_id}.xml',
  61.         'Languages' => '/languages.xml',
  62.         'Project' => '/projects/{project_id}.xml',
  63.         'Projects' => '/projects.xml',
  64.         'SizeFacts' => '/projects/{project_id}/analyses/{analysis_id}/size_facts.xml',
  65.         'SizeFactsLatest' => '/projects/{project_id}/analyses/latest/size_facts.xml',
  66.         'StackByAccount' => '/accounts/{account_id}/stacks/{stack_id}.xml',
  67.         'StackByAccountDefault' => '/accounts/{account_id}/stacks/default.xml',
  68.         'Stacks' => '/projects/{project_id}/stacks.xml'
  69.     );
  70. /**
  71. * call the svn method
  72. *
  73. *
  74. */
  75.     function call__($method, $params) {
  76.  
  77.         if (isset($this->__gets[$method])) {
  78.             $data = null;
  79.             $Http = new HttpSocket();
  80.  
  81.             if(preg_match_all('/\\{(.*?)\\}/', $this->__gets[$method], $matches)) {
  82.                 $uri = null;
  83.  
  84.                 if (!empty($params) && is_array($params[0])) {
  85.                     $params = $params[0];
  86.                 }
  87.                
  88.                 if (count($matches[1]) !== count($params)) {
  89.                     trigger_error("wrong parameter count for {$method}. expecting " . count($matches[1]), E_USER_ERROR);
  90.                     return false;
  91.                 }
  92.  
  93.                 foreach ($matches[1] as $key => $match) {
  94.                     if (isset($params[$key])) {
  95.                         $uri = str_replace("{{$match}}", $params[$key], $this->__gets[$method]);
  96.                     } elseif (isset($params[$match])) {
  97.                         $uri = str_replace("{{$match}}", $params[$match], $this->__gets[$method]);
  98.                     } else {
  99.                         trigger_error("wrong parameter count for {$method}. missing ". $match, E_USER_ERROR);
  100.                         return false;
  101.                     }
  102.                 }
  103.                
  104.                 $result = $Http->get('http://www.ohloh.net' . $uri .'?api_key='.$this->config['key'] . '&v=' . $this->config['version'], array(), array('header' => array('Content-Type' => 'text/xml')));
  105.                 $data = Set::reverse(new Xml($result));
  106.                
  107.                 if (!empty($data['Response']) && $data['Response']['status'] == 'success') {
  108.                     return $data['Response']['Result'];
  109.                 } elseif (!empty($data['Response']['Error'])) {
  110.                     return $data['Response']['Error'];
  111.                 }
  112.             }
  113.  
  114.         }
  115.         trigger_error("{$method} could not be found.", E_USER_ERROR);
  116.         return false;
  117.     }
  118. /**
  119. * sets or returns config
  120. *
  121. */
  122.     function config($config = array()) {
  123.         if (empty($config)) {
  124.             return $this->config;
  125.         }
  126.         return $this->config = array_merge($this->config, $config);
  127.     }
  128.  
  129. /**
  130. * callbacks that need to be overriden for call__
  131. *
  132. */
  133.     function initialize(&$controller) {}
  134.     function startup(&$controller) {}
  135.     function beforeRender() {}
  136.     function beforeRedirect() {}
  137.     function shutdown() {}
  138. }
  139.  
Parsed in 0.260 seconds, using GeSHi 1.0.7.14

Modify this Paste