2
0
فهرست منبع

New Zend_Service_ShortUrl_BitLy

git-svn-id: http://framework.zend.com/svn/framework/standard/trunk@24745 44c647ce-9c0f-0410-b52a-842ac1e357ba
ezimuel 13 سال پیش
والد
کامیت
f113500926

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

@@ -46,6 +46,13 @@
                         url="http://is.gd">is.gd</ulink> service.
                 </para>
             </listitem>
+
+            <listitem>
+                <para>
+                    <classname>Zend_Service_ShortUrl_BitLy</classname>, which accesses the <ulink
+                        url="http://bitly.com">bit.ly</ulink> service.
+                </para>
+            </listitem>
         </itemizedlist>
     </sect2>
 
@@ -158,4 +165,67 @@ $long  = $tinyurl->unshorten('http://tinyurl.com/rxtuq'); // http://framework.ze
             </varlistentry>
         </variablelist>
     </sect2>
+
+    <sect2 id="zend.service.short-url.bitly">
+        <title>Additional Bit.ly Methods</title>
+
+        <para>
+            The bitly API require that authentication credentials be supplied as query arguments.
+            To get started, you'll need a free bitly user account and apiKey. Signup at:
+            <ulink url="http://http://bitly.com/a/sign_up">http://bitly.com/a/sign_up</ulink>
+        </para>
+
+        <para>
+            bitly currently also supports the OAuth 2 draft specification. You could provide a
+            generated OAuth access token to <classname>Zend_Service_ShortUrl_BitLy</classname>
+            using <methodname>setOAuthAccessToken</methodname> or as constructor argument.
+        </para>
+
+        <programlisting language="php"><![CDATA[
+$bitly = new Zend_Service_ShortUrl_BitLy('username','apiKey');
+
+// Shorten a URL:
+$short = $bitly->shorten('http://framework.zend.com/'); // http://bit.ly/15Oe0
+
+// Inflate or unshorten a short URL:
+$long  = $bitly->unshorten('http://bit.ly/15Oe0'); // http://framework.zend.com/
+]]></programlisting>
+
+        <variablelist>
+            <varlistentry id="zend.service.short-url.bitly.setApiLogin">
+                <term>
+                    <methodsynopsis>
+                        <methodname>setApiLogin</methodname>
+                        <methodparam>
+                            <funcparams>$login, $apiKey</funcparams>
+                        </methodparam>
+                    </methodsynopsis>
+                </term>
+
+                <listitem>
+                    <para>
+                        Takes the required <varname>$login</varname> username and <varname>$apiKey</varname>
+                        and passes it to the service.
+                    </para>
+                </listitem>
+            </varlistentry>
+
+            <varlistentry id="zend.service.short-url.bitly.setOAuthAccessToken">
+                <term>
+                    <methodsynopsis>
+                        <methodname>setOAuthAccessToken</methodname>
+                        <methodparam>
+                            <funcparams>$accessToken</funcparams>
+                        </methodparam>
+                    </methodsynopsis>
+                </term>
+
+                <listitem>
+                    <para>
+                        Use the OAuth <varname>$accessToken</varname> for authentication
+                    </para>
+                </listitem>
+            </varlistentry>
+        </variablelist>
+    </sect2>
 </sect1>

+ 167 - 0
library/Zend/Service/ShortUrl/BitLy.php

@@ -0,0 +1,167 @@
+<?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-2012 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';
+
+/**
+ * Bit.ly API implementation
+ *
+ * @category   Zend
+ * @package    Zend_Service_ShortUrl
+ * @copyright  Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
+ * @license    http://framework.zend.com/license/new-bsd     New BSD License
+ */
+class Zend_Service_ShortUrl_BitLy extends Zend_Service_ShortUrl_AbstractShortener
+{
+
+    /**
+     * Base URI of the service
+     *
+     * @var string
+     */
+    protected $_apiUri = 'http://api.bitly.com';
+
+    /**
+     * user login name
+     *
+     * @var string
+     */
+    protected $_loginName;
+
+    /**
+     * user API key or application access token
+     *
+     * @var string
+     */
+    protected $_apiKey;
+
+    /**
+     * @param string $login user login name or application access token
+     * @param null|string $apiKey user API key
+     */
+    public function __construct($login, $apiKey = null)
+    {
+        if(null === $apiKey) {
+            $this->setOAuthAccessToken($login);
+        } else {
+            $this->setApiLogin($login, $apiKey);
+        }
+    }
+
+    /**
+     * set OAuth credentials
+     *
+     * @param $accessToken
+     * @return Zend_Service_ShortUrl_BitLy
+     */
+    public function setOAuthAccessToken($accessToken)
+    {
+        $this->_apiKey = $accessToken;
+        $this->_loginName = null;
+        return $this;
+    }
+
+    /**
+     * set login credentials
+     *
+     * @param $login
+     * @param $apiKey
+     * @return Zend_Service_ShortUrl_BitLy
+     */
+    public function setApiLogin($login, $apiKey)
+    {
+        $this->_apiKey = $apiKey;
+        $this->_loginName = $login;
+        return $this;
+    }
+
+    /**
+     * prepare http client
+     * @return void
+     */
+    protected function _setAccessParameter()
+    {
+        if(null === $this->_loginName) {
+            //OAuth login
+            $this->getHttpClient()->setParameterGet('access_token', $this->_apiKey);
+        } else {
+            //login/APIKey authentication
+            $this->getHttpClient()->setParameterGet('login',$this->_loginName);
+            $this->getHttpClient()->setParameterGet('apiKey',$this->_apiKey);
+        }
+    }
+
+    /**
+     * handle bit.ly response
+     *
+     * @return string
+     * @throws Zend_Service_ShortUrl_Exception
+     */
+    protected function _processRequest()
+    {
+        $response = $this->getHttpClient()->request();
+        if(500 == $response->getStatus()) {
+            throw new Zend_Service_ShortUrl_Exception('Bit.ly :: '.$response->getBody());
+        }
+        return $response->getBody();
+    }
+
+    /**
+     * This function shortens long url
+     *
+     * @param  string $url URL to Shorten
+     * @throws Zend_Service_ShortUrl_Exception if bit.ly reports an error
+     * @return string Shortened Url
+     */
+    public function shorten($url)
+    {
+        $this->_validateUri($url);
+        $this->_setAccessParameter();
+
+        $this->getHttpClient()->setUri($this->_apiUri.'/v3/shorten');
+        $this->getHttpClient()->setParameterGet('longUrl',$url);
+        $this->getHttpClient()->setParameterGet('format','txt');
+
+        return $this->_processRequest();
+    }
+
+    /**
+     * Reveals target for short URL
+     *
+     * @param  string $shortenedUrl URL to reveal target of
+     * @throws Zend_Service_ShortUrl_Exception if bit.ly reports an error
+     * @return string Unshortened Url
+     */
+    public function unshorten($shortenedUrl)
+    {
+        $this->_validateUri($shortenedUrl);
+        $this->_setAccessParameter();
+
+        $this->getHttpClient()->setUri($this->_apiUri.'/v3/expand');
+        $this->getHttpClient()->setParameterGet('shortUrl',$shortenedUrl);
+        $this->getHttpClient()->setParameterGet('format','txt');
+
+        return $this->_processRequest();
+    }
+}

+ 1 - 1
library/Zend/Service/ShortUrl/Shortener.php

@@ -33,7 +33,7 @@ interface Zend_Service_ShortUrl_Shortener
      * @param  string $url URL to Shorten
      * @return string Shortened Url
      */
-    public function shorten($shortenedUrl);
+    public function shorten($url);
 
     /**
      * Reveals target for short URL

+ 6 - 0
tests/Zend/Service/ShortUrl/AllTests.php

@@ -45,6 +45,11 @@ require_once 'Zend/Service/ShortUrl/MetamarkNetTest.php';
 require_once 'Zend/Service/ShortUrl/TinyUrlComTest.php';
 
 /**
+ * @see Zend_Service_ShortUrl_BitLyTest
+ */
+require_once 'Zend/Service/ShortUrl/BitLyTest.php';
+
+/**
  * @category   Zend
  * @package    Zend_Service_ShortUrl
  * @subpackage UnitTests
@@ -67,6 +72,7 @@ class Zend_Service_ShortUrl_AllTests
         $suite->addTestSuite('Zend_Service_ShortUrl_JdemCzTest');
         $suite->addTestSuite('Zend_Service_ShortUrl_MetamarkNetTest');
         $suite->addTestSuite('Zend_Service_ShortUrl_TinyUrlComTest');
+        $suite->addTestSuite('Zend_Service_ShortUrl_BitLyTest');
 
         return $suite;
     }

+ 110 - 0
tests/Zend/Service/ShortUrl/BitLyTest.php

@@ -0,0 +1,110 @@
+<?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-2012 Zend Technologies USA Inc. (http://www.zend.com)
+ * @license    http://framework.zend.com/license/new-bsd     New BSD License
+ */
+
+/** Zend_Service_ShortUrl_BitLy */
+require_once 'Zend/Service/ShortUrl/BitLy.php';
+
+/**
+ * @package  Zend_Service
+ * @subpackage  UnitTests
+ */
+class Zend_Service_ShortUrl_BitLyTest extends PHPUnit_Framework_TestCase
+{
+
+    /**
+     * reset zend service http client
+     *
+     * @return void
+     */
+    public function setUp ()
+    {
+        Zend_Service_Abstract::setHttpClient(new Zend_Http_Client());
+    }
+
+    public function testShortenEmptyUrlException()
+    {
+        $this->setExpectedException('Zend_Service_ShortUrl_Exception');
+
+        $s = new Zend_Service_ShortUrl_BitLy('test');
+        $s->shorten('');
+    }
+
+    public function testShortenIncorrectUrlException()
+    {
+        $this->setExpectedException('Zend_Service_ShortUrl_Exception');
+
+        $s = new Zend_Service_ShortUrl_BitLy('test');
+        $s->shorten('wrongAdress.cccc');
+    }
+
+    public function testExceptionOnBadApiResponse()
+    {
+        $this->setExpectedException('Zend_Service_ShortUrl_Exception');
+
+        $clientResponse = $this->getMock('Zend_Http_Response', array(), array(), '', false);
+        $clientResponse->expects($this->once())->method('getStatus')->will($this->returnValue(500));
+
+        $client = $this->getMock('Zend_Http_Client', array(), array(), '', false);
+        $client->expects($this->once())->method('request')->will($this->returnValue($clientResponse));
+
+        $s = new Zend_Service_ShortUrl_BitLy('test');
+        $s->setHttpClient($client);
+        $s->shorten('http://framework.zend.com');
+    }
+
+    public function testAuthenticationWithAccessToken()
+    {
+        $accessToken = 'test';
+
+        $clientResponse = $this->getMock('Zend_Http_Response', array(), array(), '', false);
+        $clientResponse->expects($this->once())->method('getStatus')->will($this->returnValue(200));
+        $clientResponse->expects($this->once())->method('getBody')->will($this->returnValue('http://bit.ly/ZFramework'));
+
+        $client = $this->getMock('Zend_Http_Client', array(), array(), '', false);
+        $client->expects($this->any())->method('setParameterGet')->with($this->anything(),$this->anything());
+        $client->expects($this->at(0))->method('setParameterGet')->with('access_token',$accessToken);
+        $client->expects($this->once())->method('request')->will($this->returnValue($clientResponse));
+
+        $s = new Zend_Service_ShortUrl_BitLy($accessToken);
+        $s->setHttpClient($client);
+        $s->shorten('http://framework.zend.com');
+    }
+
+    public function testAuthenticationWithUserCredentials()
+    {
+        $login = 'test';
+        $apiKey = 'api';
+
+        $clientResponse = $this->getMock('Zend_Http_Response', array(), array(), '', false);
+        $clientResponse->expects($this->once())->method('getStatus')->will($this->returnValue(200));
+        $clientResponse->expects($this->once())->method('getBody')->will($this->returnValue('http://bit.ly/ZFramework'));
+
+        $client = $this->getMock('Zend_Http_Client', array(), array(), '', false);
+        $client->expects($this->any())->method('setParameterGet')->with($this->anything(),$this->anything());
+        $client->expects($this->at(0))->method('setParameterGet')->with('login',$login);
+        $client->expects($this->at(1))->method('setParameterGet')->with('apiKey',$apiKey);
+        $client->expects($this->once())->method('request')->will($this->returnValue($clientResponse));
+
+        $s = new Zend_Service_ShortUrl_BitLy($login, $apiKey);
+        $s->setHttpClient($client);
+        $s->shorten('http://framework.zend.com');
+    }
+}