Changeset 2401 -- 2011-04-05 16:24:04
- Author
SHIMOOKA Hideyuki
- Comment
- package released (0.3.0-beta) (@http://d.hatena.ne.jp/shimooka/)
Diffs
Services_ShortURL_Googl/tags/0.3.0-beta-20110405162403/test/Services_ShortURL_Googl/Services_ShortURL_GooglTest.php
@@ -0,0 +1,33 @@
+<?php
+ini_set("include_path", dirname(__FILE__) . "/../../" . PATH_SEPARATOR . ini_get("include_path"));
+
+require_once "PHPUnit/Framework/TestCase.php";
+require_once 'Services/ShortURL/Googl.php';
+
+/**
+ * testcase for Services_ShortURL_Google
+ *
+ * @version $Id$
+ */
+class Services_ShortURL_GoogleTest extends PHPUnit_Framework_TestCase
+{
+ private $obj;
+
+ public function setup() {
+ $this->obj = new Services_ShortURL_Googl();
+ }
+
+ public function testShorten() {
+ $this->assertEquals('http://goo.gl/fbsS', $this->obj->shorten('http://www.google.com/'));
+ $this->assertEquals('http://goo.gl/kJ9E', $this->obj->shorten('http://www.php.net/'));
+ $this->assertEquals('http://goo.gl/LJGZ', $this->obj->shorten('http://pear.php.net/'));
+ $this->assertEquals('http://goo.gl/BuQj', $this->obj->shorten('http://d.hatena.ne.jp/shimooka/'));
+ }
+
+ public function testExpand() {
+ $this->assertEquals('http://www.google.com/', $this->obj->expand('http://goo.gl/fbsS'));
+ $this->assertEquals('http://www.php.net/', $this->obj->expand('http://goo.gl/kJ9E'));
+ $this->assertEquals('http://pear.php.net/', $this->obj->expand('http://goo.gl/LJGZ'));
+ $this->assertEquals('http://d.hatena.ne.jp/shimooka/', $this->obj->expand('http://goo.gl/BuQj'));
+ }
+}
属性に変更があったパス: Services_ShortURL_Googl/tags/0.3.0-beta-20110405162403/test/Services_ShortURL_Googl/Services_ShortURL_GooglTest.php
___________________________________________________________________
追加: svn:mime-type
+ text/x-php
追加: svn:keywords
+ Id Rev Date
追加: svn:mergeinfo
追加: svn:eol-style
+ CRLF
Services_ShortURL_Googl/tags/0.3.0-beta-20110405162403/Services/ShortURL/Googl.php
@@ -0,0 +1,126 @@
+<?php
+
+/**
+ * Interface for creating/expanding goo.gl links
+ *
+ * PHP version 5.2.0+
+ *
+ * LICENSE: This source file is subject to the New BSD license that is
+ * available through the world-wide-web at the following URI:
+ * http://www.opensource.org/licenses/bsd-license.php. If you did not receive
+ * a copy of the New BSD License and are unable to obtain it through the web,
+ * please send a note to license@php.net so we can mail you a copy immediately.
+ *
+ * @category CategoryName
+ * @package Services_ShortURL
+ * @author Hideyuki Shimooka <shimooka@doyouphp.jp>
+ * @copyright 2011 Hideyuki Shimooka <shimooka@doyouphp.jp>
+ * @license http://tinyurl.com/new-bsd New BSD License
+ * @version SVN: $Id$
+ * @link http://pear.php.net/package/Services_ShortURL
+ * @see http://d.hatena.ne.jp/shimooka/20110112/1294796415
+ */
+
+require_once 'Services/ShortURL/Common.php';
+require_once 'Services/ShortURL/Interface.php';
+require_once 'Services/ShortURL/Exception/CouldNotShorten.php';
+require_once 'Services/ShortURL/Exception/CouldNotExpand.php';
+require_once 'Services/ShortURL/Exception.php';
+
+/**
+ * Interface for creating/expanding goo.gl links
+ *
+ * @category CategoryName
+ * @package Services_ShortURL
+ * @author Hideyuki Shimooka <shimooka@doyouphp.jp>
+ * @copyright 2011 Hideyuki Shimooka <shimooka@doyouphp.jp>
+ * @license http://tinyurl.com/new-bsd New BSD License
+ * @version Release: @package_version@
+ * @link http://pear.php.net/package/Services_ShortURL
+ * @see http://d.hatena.ne.jp/shimooka/20110112/1294796415
+ */
+class Services_ShortURL_Googl
+ extends Services_ShortURL_Common
+ implements Services_ShortURL_Interface
+{
+ /**
+ * API URL
+ *
+ * @var string $api The URL for the API
+ * @access protected
+ */
+ protected $api = 'https://www.googleapis.com/urlshortener/v1/url';
+
+ /**
+ * Constructor
+ *
+ * @param array $options The service options array
+ * @param object $req The request object
+ */
+ public function __construct(array $options = array(), HTTP_Request2 $req = null)
+ {
+ parent::__construct($options, $req);
+ }
+
+ /**
+ * Shorten a URL using {@link http://goo.gl}
+ *
+ * @param string $url The URL to shorten
+ *
+ * @throws {@link Services_ShortURL_Exception_CouldNotShorten}
+ * @return string The shortened URL
+ */
+ public function shorten($url)
+ {
+ $api = $this->api;
+ if (isset($options['key']) && $options['key'] !== '') {
+ $api .= '?key=' . $options['key'];
+ }
+ $this->req->setUrl($api);
+ $this->req->setMethod(HTTP_Request2::METHOD_POST);
+ $this->req->setBody(json_encode(array('longUrl' => $url)));
+ $this->req->setHeader('Content-Type', 'application/json');
+ $res = $this->req->send();
+ if ($res->getStatus() !== 200) {
+ throw new Services_ShortURL_Exception_CouldNotShorten(
+ 'Non-200 code returned', $res->getStatus()
+ );
+ }
+
+ $data = json_decode($res->getBody());
+ return $data->id;
+ }
+
+ /**
+ * Shorten a URL using {@link http://goo.gl}
+ *
+ * @param string $url The URL to shorten
+ *
+ * @throws {@link Services_ShortURL_Exception_CouldNotExpand}
+ * @return string The shortened URL
+ */
+ public function expand($url)
+ {
+ $this->req->setUrl($this->api . '?shortUrl=' . $url);
+ $this->req->setMethod(HTTP_Request2::METHOD_GET);
+ $this->req->setBody(null);
+ $this->req->setHeader('Content-Type', '');
+ $res = $this->req->send();
+ if ($res->getStatus() !== 200) {
+ throw new Services_ShortURL_Exception_CouldNotExpand(
+ 'Non-200 code returned', $res->getStatus()
+ );
+ }
+
+ $json = json_decode($res->getBody());
+ if ($json === false || is_null($json)) {
+ throw new Services_ShortURL_Exception('failed to decode json');
+ } else if ($json->status !== 'OK') {
+ throw new Services_ShortURL_Exception('maybe ' . $json->status);
+ }
+
+ return $json->longUrl;
+
+ }
+
+}
属性に変更があったパス: Services_ShortURL_Googl/tags/0.3.0-beta-20110405162403/Services/ShortURL/Googl.php
___________________________________________________________________
追加: svn:mime-type
+ text/x-php
追加: svn:keywords
+ Id Rev Date
追加: svn:eol-style
+ CRLF
Services_ShortURL_Googl/tags/0.3.0-beta-20110405162403/doc/Services_ShortURL_Googl/sample.php
@@ -0,0 +1,15 @@
+<?php
+ini_set("include_path", dirname(__FILE__) . "/../../" . PATH_SEPARATOR . ini_get("include_path"));
+require_once "Services/ShortURL/Googl.php";
+
+// Test
+//$obj = Services_ShortURL::factory('Googl');
+$obj = new Services_ShortURL_Googl();
+try {
+ $result = $obj->shorten('http://d.hatena.ne.jp/shimooka/');
+ echo $result . PHP_EOL;
+ echo $obj->expand($result) . PHP_EOL;
+} catch (Exception $e) {
+ echo $e->getMessage() . PHP_EOL;
+ exit;
+}
属性に変更があったパス: Services_ShortURL_Googl/tags/0.3.0-beta-20110405162403/doc/Services_ShortURL_Googl/sample.php
___________________________________________________________________
追加: svn:mime-type
+ text/x-php
追加: svn:keywords
+ Id Rev Date
追加: svn:mergeinfo
追加: svn:eol-style
+ CRLF