Sharing feedback with Twitter using Bit.ly – Part 1

Over here there is a good article on sharing page feedback on Twitter.  I'll end up doing something similar but in a different manner.  A little while I posted an article (Do you queue?) on how you could use the Zend Server Job Queue to run individual tasks.  Well you can do the same thing here.  I've made a few changes since that article, namely that Zend_Application is passed in both to execute() and to run() so I could easily retrieve application settings.  Don't know why I didn't think of that earlier.  Oh well.

The way this is different is that I want to pre-populate the bit.ly URL for an individual page so I only have to make one call to bit.ly.  This is because I don't want to slow the performance of my web page down when someone is viewing the page.  Additionally, I don't want to have a time out when I submit a page.  Therefore I need to create a task.

class Admin_Task_Bitly extends Esc_Queue_TaskAbstract
{

    private $_contentKey;
    
    public function __construct($contentKey)
    {
        $this->_contentKey = $contentKey;
    }
    
    protected function _execute (Zend_Application $app)
    {
        $ct = new Model_DbTable_Content();
        $c = $ct->find($this->_contentKey)->current();
        if (!$c) return;
        // Don't need to do it again
        if ($c->getBitly()) return;
        $options = $app->getOption('bitly');
        $url = 'http://api.bit.ly/shorten?version=2.0.1&longUrl='
               . urlencode(
                       $options['url']
                    . $c->getPageId())
               . '&login='
               . $options['login']
               . '&apiKey='
               . $options['key']
               . '&format=json';
        $results = json_decode(
            file_get_contents($url),
            true
        );
        if (!$results['errorCode']) {
            $res = array_shift($results['results']);
            $c->setBitly($res['shortUrl']);
            $c->save();
        }
    }
}

What this code does is take the primary key of the content being shortened and stores it for serialization when being passed to the Job Queue.  Then when the Job Queue executes it will get a new instance of the object, check to see if it already has a bit.ly URL and then build the URL, sending it to the bit.ly API servers.  When it gets the response back it checks to see if it's an error and if not, saves the result to the database for later usage.

But we're not quite there yet.  We still have some application.ini settings to set.

bitly.login = "xxxxxxxxxx"
bitly.url = http://www.eschrade.com/page/
bitly.key = "xxxxxxxxxxxxxx"

… not that some of those are hard to find out.  And then we need to add the task to the code that saves the article after I have finished working on it.

$mod->save();
$task = new Admin_Task_Bitly($mod->getId());
$task->execute($this->getInvokeArg('bootstrap')->getApplication());

That's it, from the functional perspective.  All that's left to do now is test it.

class Admin_Task_BitlyTest extends PHPUnit_Framework_TestCase
{
 
    public function testGetBitly()
    {
        $ct  = new Model_DbTable_Content();
        $c = $ct->fetchAll()->current();
        /* @var $c Model_Content */
        $id = $c->getId();
        $c->setBitly('');
        $c->save();
        
        $task = new Admin_Task_Bitly($c->getId());
        $task->run(Zend_Registry::get('Zend_Application'));
        
        $c = $ct->find($id)->current();
        $this->assertNotEquals(
            '',
            $c->getBitly()
        );
        
    }
}

And we're done.  Except for the actual part about posting to Twitter.  But we'll do that in a bit.

Related posts

Leave a Comment