Browse Source

[1.11] Merged is.gd and metamark.net ShortUrl services to trunk

git-svn-id: http://framework.zend.com/svn/framework/standard/trunk@23157 44c647ce-9c0f-0410-b52a-842ac1e357ba
matthew 15 years ago
parent
commit
392b2e8c78

+ 14 - 0
documentation/manual/en/module_specs/Zend_Service_ShortUrl.xml

@@ -32,6 +32,20 @@
                         url="http://tinyurl.com">tinyurl.com</ulink> service.
                 </para>
             </listitem>
+
+            <listitem>
+                <para>
+                    <classname>Zend_Service_ShortUrl_MetamarkNet</classname>, which accesses the <ulink
+                        url="http://metamark.net/">metamark.net</ulink> service.
+                </para>
+            </listitem>
+			
+			<listitem>
+                <para>
+                    <classname>Zend_Service_ShortUrl_IsGd</classname>, which accesses the <ulink
+                        url="http://is.gd">is.gd</ulink> service.
+                </para>
+            </listitem>
         </itemizedlist>
     </sect2>
 

+ 96 - 0
library/Zend/Service/ShortUrl/IsGd.php

@@ -0,0 +1,96 @@
+<?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_ShortUrl
+ * @copyright  Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
+ * @license    http://framework.zend.com/license/new-bsd     New BSD License
+ * @version    $Id: $
+ */
+
+/**
+ * @see Zend_Service_ShortUrl_AbstractShortener
+ */
+require_once 'Zend/Service/ShortUrl/AbstractShortener.php';
+
+/**
+ * Is.gd API implementation
+ *
+ * @category   Zend
+ * @package    Zend_Service_ShortUrl
+ * @copyright  Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
+ * @license    http://framework.zend.com/license/new-bsd     New BSD License
+ */
+class Zend_Service_ShortUrl_IsGd extends Zend_Service_ShortUrl_AbstractShortener
+{
+    /**
+     * Base URI of the service
+     *
+     * @var string
+     */
+    protected $_baseUri = 'http://is.gd';
+    
+    /**
+     * This function shortens long url
+     *
+     * @param string $url URL to Shorten
+     * @throws Zend_Service_ShortUrl_Exception When URL is not valid
+     * @return string New URL
+     */
+    public function shorten($url)
+    {
+        $this->_validateUri($url);
+        
+        $serviceUri = 'http://is.gd/api.php';
+        
+        $this->getHttpClient()->resetParameters(true);
+        $this->getHttpClient()->setUri($serviceUri);
+        $this->getHttpClient()->setParameterGet('longurl', $url);
+        
+        $response = $this->getHttpClient()->request();
+        
+        return $response->getBody();
+    }
+
+   /**
+     * Reveals target for short URL
+     *
+     * @param string $shortenedUrl URL to reveal target of
+     * @throws Zend_Service_ShortUrl_Exception When URL is not valid or is not shortened by this service
+     * @return string
+     */
+    public function unshorten($shortenedUrl)
+    {
+        $this->_validateUri($shortenedUrl);
+
+        $this->_verifyBaseUri($shortenedUrl);
+        
+        $this->getHttpClient()->resetParameters(true);
+        $this->getHttpClient()->setUri($shortenedUrl);
+        $this->getHttpClient()->setConfig(array('maxredirects' => 0));
+        
+        $response = $this->getHttpClient()->request();
+        if ($response->isError()) {
+            require_once 'Zend/Service/ShortUrl/Exception.php';
+            throw new Zend_Service_ShortUrl_Exception($response->getMessage());
+        }
+        
+        if ($response->isRedirect()) {
+            return $response->getHeader('Location');
+        }
+        
+        require_once 'Zend/Service/ShortUrl/Exception.php';
+        throw new Zend_Service_ShortUrl_Exception('Url unshortening was not successful');
+    }
+}

+ 85 - 0
library/Zend/Service/ShortUrl/MetamarkNet.php

@@ -0,0 +1,85 @@
+<?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_ShortUrl
+ * @copyright  Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
+ * @license    http://framework.zend.com/license/new-bsd     New BSD License
+ * @version    $Id: $
+ */
+
+/**
+ * @see Zend_Service_ShortUrl_AbstractShortener
+ */
+require_once 'Zend/Service/ShortUrl/AbstractShortener.php';
+
+/**
+ * Metamark.net API implementation
+ *
+ * @category   Zend
+ * @package    Zend_Service_ShortUrl
+ * @copyright  Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
+ * @license    http://framework.zend.com/license/new-bsd     New BSD License
+ */
+class Zend_Service_ShortUrl_MetamarkNet extends Zend_Service_ShortUrl_AbstractShortener
+{
+    /**
+     * Base URI of the service
+     *
+     * @var string
+     */
+    protected $_baseUri = 'http://xrl.us/';
+    
+    protected $_apiUri = 'http://metamark.net/api/rest/simple';
+    
+    /**
+     * This function shortens long url
+     *
+     * @param string $url URL to Shorten
+     * @throws Zend_Service_ShortUrl_Exception When URL is not valid
+     * @return string New URL
+     */
+    public function shorten($url)
+    {
+        $this->_validateUri($url);
+        
+        $this->getHttpClient()->setUri($this->_apiUri);
+        $this->getHttpClient()->setParameterGet('long_url', $url);
+        
+        $response = $this->getHttpClient()->request();
+        
+        return $response->getBody();
+    }
+
+   /**
+     * Reveals target for short URL
+     *
+     * @param string $shortenedUrl URL to reveal target of
+     * @throws Zend_Service_ShortUrl_Exception When URL is not valid or is not shortened by this service
+     * @return string
+     */
+    public function unshorten($shortenedUrl)
+    {
+        $this->_validateUri($shortenedUrl);
+
+        $this->_verifyBaseUri($shortenedUrl);
+        
+        $this->getHttpClient()->setUri($this->_apiUri);
+        $this->getHttpClient()->setParameterGet('short_url', $shortenedUrl);
+        
+        $response = $this->getHttpClient()->request();
+        
+        return $response->getBody();
+    }
+}

+ 13 - 1
tests/Zend/Service/ShortUrl/AllTests.php

@@ -30,11 +30,21 @@ if (!defined('PHPUnit_MAIN_METHOD')) {
 require_once dirname(__FILE__) . '/../../../TestHelper.php';
 
 /**
+ * @see Zend_Service_ShortUrl_IsGdTest
+ */
+require_once 'Zend/Service/ShortUrl/IsGdTest.php';
+
+/**
  * @see Zend_Service_ShortUrl_JdemCzTest
  */
 require_once 'Zend/Service/ShortUrl/JdemCzTest.php';
 
 /**
+ * @see Zend_Service_ShortUrl_MetamarkNetTest
+ */
+require_once 'Zend/Service/ShortUrl/MetamarkNetTest.php';
+
+/**
  * @see Zend_Service_ShortUrl_TinyUrlComTest
  */
 require_once 'Zend/Service/ShortUrl/TinyUrlComTest.php';
@@ -58,7 +68,9 @@ class Zend_Service_ShortUrl_AllTests
     {
         $suite = new PHPUnit_Framework_TestSuite('Zend Framework - Zend_Service_ShortUrl');
 
+        $suite->addTestSuite('Zend_Service_ShortUrl_IsGdTest');
         $suite->addTestSuite('Zend_Service_ShortUrl_JdemCzTest');
+        $suite->addTestSuite('Zend_Service_ShortUrl_MetamarkNetTest');
         $suite->addTestSuite('Zend_Service_ShortUrl_TinyUrlComTest');
         
         return $suite;
@@ -67,4 +79,4 @@ class Zend_Service_ShortUrl_AllTests
 
 if (PHPUnit_MAIN_METHOD == 'Zend_Service_ShortUrl_AllTests::main') {
     Zend_Service_ShortUrl_AllTests::main();
-}
+}

+ 107 - 0
tests/Zend/Service/ShortUrl/IsGdTest.php

@@ -0,0 +1,107 @@
+<?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 UnitTests
+ * @copyright  Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
+ * @license    http://framework.zend.com/license/new-bsd     New BSD License
+ */
+
+/**
+ * Test helper
+ */
+require_once dirname(__FILE__) . '/../../../TestHelper.php';
+
+/** Zend_Service_ShortUrl_IsGd */
+require_once 'Zend/Service/ShortUrl/IsGd.php';
+
+/**
+ * @package  Zend_Service
+ * @subpackage  UnitTests
+ */
+class Zend_Service_ShortUrl_IsGdTest extends PHPUnit_Framework_TestCase
+{
+    /**
+     * Zend_Service_ShortUrl_IsGd object
+     *
+     * @var Zend_Service_ShortUrl_IsGd
+     */
+    protected $_s;
+    
+    /**
+     * Creates a new Zend_Service_ShortUrl_IsGd object for each test method
+     *
+     * @return void
+     */
+    public function setUp ()
+    {
+        $this->_s = new Zend_Service_ShortUrl_IsGd();
+        $this->_s->getHttpClient()->resetParameters(true);
+    }
+
+    public function testShortenEmptyUrlException()
+    {
+        $this->setExpectedException('Zend_Service_ShortUrl_Exception');
+        $this->_s->shorten('');
+    }
+    
+    public function testShortenIncorrectUrlException()
+    {
+        $this->setExpectedException('Zend_Service_ShortUrl_Exception');
+        $this->_s->shorten('wrongAdress.cccc');
+    }
+    
+    public function testShorten()
+    {
+        $urls = array(
+            'http://framework.zend.com/'           => 'http://is.gd/g87rz',
+            'http://framework.zend.com/manual/en/' => 'http://is.gd/g87LE',
+        );
+        
+        foreach ($urls as $url => $shortenedUrl) {
+            $this->assertEquals($shortenedUrl, $this->_s->shorten($url));
+        }
+    }
+    
+    public function testUnshorten()
+    {
+        $urls = array(
+            'http://framework.zend.com/'           => 'http://is.gd/g3ASn',
+            'http://framework.zend.com/manual/en/' => 'http://is.gd/g3AVm'
+        );
+        
+        foreach ($urls as $url => $shortenedUrl) {
+            $this->assertEquals($url, $this->_s->unshorten($shortenedUrl));
+        }
+    }
+    
+    public function testUnshortenEmptyUrlException()
+    {
+        $this->setExpectedException('Zend_Service_ShortUrl_Exception');
+        $this->_s->unshorten('');
+    }
+    
+    public function testUnshortenIncorrectUrlException()
+    {
+        $this->setExpectedException('Zend_Service_ShortUrl_Exception');
+        $this->_s->unshorten('wrongAdress.cccc');
+    }
+    
+    public function testUnshortenWrongUrlException()
+    {
+        $this->setExpectedException('Zend_Service_ShortUrl_Exception');
+        $this->_s->unshorten('http://www.zend.com');
+    }
+}

+ 107 - 0
tests/Zend/Service/ShortUrl/MetamarkNetTest.php

@@ -0,0 +1,107 @@
+<?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 UnitTests
+ * @copyright  Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
+ * @license    http://framework.zend.com/license/new-bsd     New BSD License
+ */
+
+/**
+ * Test helper
+ */
+require_once dirname(__FILE__) . '/../../../TestHelper.php';
+
+/** Zend_Service_ShortUrl_MetamarkNet */
+require_once 'Zend/Service/ShortUrl/MetamarkNet.php';
+
+/**
+ * @package  Zend_Service
+ * @subpackage  UnitTests
+ * @see http://metamark.net/docs/api/rest.html
+ */
+class Zend_Service_ShortUrl_MetamarkNetTest extends PHPUnit_Framework_TestCase
+{
+    /**
+     * Zend_Service_ShortUrl_MetamarkNet object
+     *
+     * @var Zend_Service_ShortUrl_MetamarkNet
+     */
+    protected $_s;
+    
+    /**
+     * Creates a new Zend_Service_ShortUrl_MetamarkNet object for each test method
+     *
+     * @return void
+     */
+    public function setUp ()
+    {
+        $this->_s = new Zend_Service_ShortUrl_MetamarkNet();
+    }
+
+    public function testShortenEmptyUrlException()
+    {
+        $this->setExpectedException('Zend_Service_ShortUrl_Exception');
+        $this->_s->shorten('');
+    }
+    
+    public function testShortenIncorrectUrlException()
+    {
+        $this->setExpectedException('Zend_Service_ShortUrl_Exception');
+        $this->_s->shorten('wrongAdress.cccc');
+    }
+    
+    public function testShorten()
+    {
+        $urls = array(
+            'http://framework.zend.com/'           => 'http://xrl.us/bh4ptf',
+            'http://framework.zend.com/manual/en/' => 'http://xrl.us/bh4pth'
+        );
+        
+        foreach ($urls as $url => $shortenedUrl) {
+            $this->assertEquals($shortenedUrl, $this->_s->shorten($url));
+        }
+    }
+    
+    public function testUnshorten()
+    {
+        $urls = array(
+            'http://framework.zend.com/'           => 'http://xrl.us/bh4ptf',
+            'http://framework.zend.com/manual/en/' => 'http://xrl.us/bh4pth'
+        );
+        
+        foreach ($urls as $url => $shortenedUrl) {
+            $this->assertEquals($url, $this->_s->unshorten($shortenedUrl));
+        }
+    }
+    
+    public function testUnshortenEmptyUrlException()
+    {
+        $this->setExpectedException('Zend_Service_ShortUrl_Exception');
+        $this->_s->unshorten('');
+    }
+    
+    public function testUnshortenIncorrectUrlException()
+    {
+        $this->setExpectedException('Zend_Service_ShortUrl_Exception');
+        $this->_s->unshorten('wrongAdress.cccc');
+    }
+    
+    public function testUnshortenWrongUrlException()
+    {
+        $this->setExpectedException('Zend_Service_ShortUrl_Exception');
+        $this->_s->unshorten('http://www.zend.com');
+    }
+}