Browse Source

remove untested and old lib.

Mark Scherer 10 years ago
parent
commit
5234f536cf
2 changed files with 0 additions and 327 deletions
  1. 0 148
      Lib/GooglLib.php
  2. 0 179
      Test/Case/Lib/GooglLibTest.php

+ 0 - 148
Lib/GooglLib.php

@@ -1,148 +0,0 @@
-<?php
-App::uses('CakeLog', 'Log');
-
-/**
- * Googl Url Shortener
- * @see http://goo.gl
- *
- * @author Eslam Mahmoud
- * @url http://hunikal.com/
- * @copyright Creative Commons Attribution-ShareAlike 3.0 Unported License.
- * @version 0.1
- *
- * TODO: implement OAuth
- *
- * @edited Mark Scherer
- */
-class GooglLib {
-
-	const PROJECTION_FULL = 'FULL';
-
-	const PROJECTION_CLICKS = 'ANALYTICS_CLICKS';
-
-	const PROJECTION_TOP = 'ANALYTICS_TOP_STRINGS';
-
-	/**
-	 * Application key
-	 */
-	protected $apiKey;
-
-	/**
-	 * API URL
-	 */
-	protected $apiUrl = "https://www.googleapis.com/urlshortener/v1/url";
-
-	/**
-	 * @param string $apiKey (optional)
-	 */
-	public function __construct($apiKey = null) {
-		if ($apiKey === null) {
-			$apiKey = Configure::read('Googl.key');
-		}
-		if ($apiKey) {
-			$this->apiKey = $apiKey;
-		}
-	}
-
-	/**
-	 * Reverse the shortening process
-	 * TODO: rename to expand
-	 *
-	 * @param strin $url
-	 * @return result as array
-	 */
-	public function getLong($shortURL, $projection = null) {
-		$vars = '?shortUrl=' . $shortURL;
-		if ($projection) {
-			$vars .= '&projection=' . $projection;
-		}
-		if ($this->apiKey) {
-			$vars .= '&key=' . $this->apiKey;
-		}
-		$ch = curl_init($this->apiUrl . $vars);
-		curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
-		curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
-		$result = curl_exec($ch);
-		curl_close($ch);
-		$array = json_decode($result, true);
-		return $array;
-	}
-
-	/**
-	 * Shorten a long url
-	 *
-	 * @param string $url
-	 * @return array result as array or false on failure
-	 */
-	public function getShort($longURL) {
-		$vars = '';
-		if ($this->apiKey) {
-			$vars .= "?key=$this->apiKey";
-		}
-
-		$ch = curl_init($this->apiUrl . $vars);
-		curl_setopt($ch, CURLOPT_POST, 1);
-		curl_setopt($ch, CURLOPT_HTTPHEADER, ['Content-Type: application/json']);
-		curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
-		curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
-		curl_setopt($ch, CURLOPT_POSTFIELDS, '{"key": "' . $this->apiKey . '", "longUrl": "' . $longURL . '"}');
-		$result = curl_exec($ch);
-		curl_close($ch);
-		$array = json_decode($result, true);
-		if (empty($array['id'])) {
-			// throw error?
-			CakeLog::write('googl', $longURL . ' - ' . print_r($array, true));
-			return false;
-		}
-		$separator = strrpos($array['id'], '/');
-		$array['key'] = substr($array['id'], $separator + 1);
-		return $array;
-	}
-
-	/**
-	 * FIXME: not working yet
-	 * TODO: use oacurl etc
-	 *
-	 * @return array
-	 */
-	public function getHistory() {
-		$vars = '';
-		$url = $this->apiUrl . '/history';
-		$ch = curl_init($url . $vars);
-		curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
-		curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
-		$result = curl_exec($ch);
-		curl_close($ch);
-		$array = json_decode($result, true);
-		return $array;
-	}
-
-	/**
-	 * Retrieve the statistics for this key.
-	 *
-	 * @param string $key
-	 * @return array
-	 */
-	public function getStatistics($key) {
-		$url = $this->apiUrl . '?shortUrl=http://goo.gl/' . $key . '&projection=FULL&key=' . $this->apiKey;
-		$ch = curl_init($url);
-		curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
-		curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
-		$result = curl_exec($ch);
-		curl_close($ch);
-		$array = json_decode($result, true);
-		return $array;
-	}
-
-	/**
-	 * Retrieve the url for the statistics page for this key
-	 *
-	 * @param string $key
-	 * @return string url
-	 */
-	public static function statisticsUrl($key) {
-		$url = 'http://goo.gl/#analytics/goo.gl/' . $key . '/all_time';
-		return $url;
-	}
-
-}

+ 0 - 179
Test/Case/Lib/GooglLibTest.php

@@ -1,179 +0,0 @@
-<?php
-
-App::uses('GooglLib', 'Tools.Lib');
-App::uses('MyCakeTestCase', 'Tools.TestSuite');
-
-/**
- */
-class GooglLibTest extends MyCakeTestCase {
-
-	public function setUp() {
-		parent::setUp();
-
-		//Configure::write('Googl.key', 'YOUR KEY');
-
-		$this->Googl = new TestGooglLib();
-		if ($this->isDebug()) {
-			$this->Googl->setLive();
-		}
-	}
-
-	public function tearDown() {
-		parent::tearDown();
-
-		unset($this->Googl);
-	}
-
-	//TODO
-
-	public function testOAuth() {
-	}
-
-	public function testHistory() {
-		$this->skipIf(true, 'Login required');
-
-		$is = $this->Googl->getHistory();
-		$this->debug($is);
-	}
-
-	/**
-	 * GooglLibTest::testShortenAndUnshorten()
-	 *
-	 * @return void
-	 */
-	public function testShortenAndUnshorten() {
-		// Shorten without key (publicly)
-		Configure::write('Googl.key', '');
-
-		$url = 'http://www.spiegel.de';
-		$is = $this->Googl->getShort($url);
-		$this->assertTrue(!empty($is) && is_array($is) && !empty($is['id']) && $is['kind'] === 'urlshortener#url' && $is['longUrl'] == $url . '/');
-
-		// Unshorten
-		$shortUrl = $is['id'];
-		$is = $this->Googl->getLong($shortUrl);
-		$this->assertTrue(!empty($is));
-		$this->assertTrue(!empty($is['id']));
-		$this->assertSame('urlshortener#url', $is['kind']);
-		$this->assertSame('OK', $is['status']);
-		$this->assertSame($url . '/', $is['longUrl']);
-	}
-
-	/**
-	 * GooglLibTest::testApi()
-	 *
-	 * @return void
-	 */
-	public function testApi() {
-		$this->skipIf(!Configure::read('Googl.key'), 'No Api Key found');
-
-		// Shorten with key
-		$url = 'http://www.blue.de';
-		$is = $this->Googl->getShort($url);
-		$this->debug($is);
-		$res = $this->assertTrue(!empty($is) && is_array($is) && !empty($is['id']) && $is['kind'] === 'urlshortener#url' && $is['longUrl'] == $url . '/');
-
-		// Unshorten
-		$shortUrl = $is['id'];
-		$is = $this->Googl->getLong($shortUrl);
-		$this->debug($is);
-		$res = $this->assertTrue(!empty($is) && is_array($is) && !empty($is['id']) && $is['kind'] === 'urlshortener#url' && $is['status'] === 'OK' && $is['longUrl'] == $url . '/');
-
-		// FULL INFOS
-		$url = 'http://www.web.de#123456';
-		$is = $this->Googl->getShort($url);
-		$this->debug($is);
-		$res = $this->assertTrue(!empty($is) && is_array($is) && !empty($is['id']) && $is['kind'] === 'urlshortener#url' && $is['longUrl'] === 'http://www.web.de/#123456');
-
-		$shortUrl = $is['id'];
-		$is = $this->Googl->getLong($shortUrl, GooglLib::PROJECTION_CLICKS);
-		$this->debug($is);
-		$res = $this->assertTrue(!empty($is) && is_array($is) && !empty($is['id']) && $is['kind'] === 'urlshortener#url' && $is['status'] === 'OK' && $is['longUrl'] === 'http://www.web.de/#123456');
-	}
-
-}
-
-/**
- * Wrapper to mock the API calls away
- */
-class TestGooglLib extends GooglLib {
-
-	protected $_debug = true;
-
-	protected $_map = [
-		'http://www.spiegel.de' => '{
- "kind": "urlshortener#url",
- "id": "http://goo.gl/nBBg",
- "longUrl": "http://www.spiegel.de/"
-}',
-		'http://goo.gl/nBBg' => '{
- "kind": "urlshortener#url",
- "id": "http://goo.gl/nBBg",
- "longUrl": "http://www.spiegel.de/",
- "status": "OK"
-}',
-		'http://www.blue.de' => '{
- "kind": "urlshortener#url",
- "id": "http://goo.gl/leVfu4",
- "longUrl": "http://www.blue.de/"
-}',
-		'http://goo.gl/leVfu4' => '{
- "kind": "urlshortener#url",
- "id": "http://goo.gl/leVfu4",
- "longUrl": "http://www.blue.de/",
- "status": "OK"
-}',
-		'http://www.web.de#123456' => '{
- "kind": "urlshortener#url",
- "id": "http://goo.gl/7937W",
- "longUrl": "http://www.web.de/#123456"
-}',
-		'http://goo.gl/7937W' => '{
- "kind": "urlshortener#url",
- "id": "http://goo.gl/7937W",
- "longUrl": "http://www.web.de/#123456",
- "status": "OK",
- "analytics": {
-  "allTime": {
-   "shortUrlClicks": "1",
-   "longUrlClicks": "1"
-  },
-  "month": {
-   "shortUrlClicks": "0",
-   "longUrlClicks": "0"
-  },
-  "week": {
-   "shortUrlClicks": "0",
-   "longUrlClicks": "0"
-  },
-  "day": {
-   "shortUrlClicks": "0",
-   "longUrlClicks": "0"
-  },
-  "twoHours": {
-   "shortUrlClicks": "0",
-   "longUrlClicks": "0"
-  }
- }
-}'
-	];
-
-	public function setLive($live = true) {
-		$this->_debug = !$live;
-	}
-
-	public function getShort($url) {
-		if ($this->_debug) {
-			return json_decode($this->_map[$url], true);
-		}
-		return parent::getShort($url);
-	}
-
-	public function getLong($url, $projection = null) {
-		if ($this->_debug) {
-			return json_decode($this->_map[$url], true);
-		}
-		return parent::getLong($url, $projection);
-	}
-
-}