URLShorten Class (update)

URL shortening is a technique where a provider makes a web page available under a very short URL in addition to the original address. There are hundreds of provider out there, but only a few of them provides public api and crossdomain.xml. I put toghether some of those that work and URLShorten class was created 🙂 feel free to use it and enjoy

Update: jdem.cz, tinyurl.com, bit.ly, tr.im, is.gd providers implemented.

Application (view source enabled), please use your own api key for bit.ly

I have just discovered tr.im has not crossdomain.xml, please do not us it while it throws errors. I have just asked them and few more providers to create crossdomain files, will see…

There are two methods you can handle resuls:

// 1. use callback function
URLShorten.jdemcz(long_url.text, jdemczResult);

// ...callback function gets one parameter = short url
private function jdemczResult(url:String):void
{
    jdemcz = url;
}
        
// 2. addEventDispatcher to have bigger control
var dispatcher:EventDispatcher = URLShorten.tinyurlcom(long_url.text);
dispatcher.addEventListener(URLShortenEvent.COMPLETE, tinyurlcomComplete);

// ...dispatched event.url = short url
private function tinyurlcomComplete(event:URLShortenEvent):void
{
    tinyurl = event.url;
}

URLShorten class

package sk.yoz.net
{
    import com.adobe.serialization.json.JSON;
    
    import flash.events.Event;
    import flash.net.URLRequest;
    import flash.net.URLVariables;
    
    import sk.yoz.events.URLShortenEvent;
    
    public class URLShorten extends Object
    {
        public function URLShorten()
        {
            super();
        }
        
        private static function prepare(request:URLRequest, 
            variables:URLVariables, callback:Function, 
            completeHandler:Function=null):URLLoaderDynamic
        {
            var loader:URLLoaderDynamic = new URLLoaderDynamic();
            var handler:Function = completeHandler || complete;
            
            request.data = variables;
            loader.callback = callback;
            loader.load(request);
            loader.addEventListener(Event.COMPLETE, handler);
            return loader;
        }
        
        private static function complete(event:Event):void
        {
            var loader:URLLoaderDynamic = URLLoaderDynamic(event.target);
            var url:String = String(loader.data);
            
            loader.removeEventListener(Event.COMPLETE, complete);
            dispatch(url, loader)
        }
        
        private static function dispatch(url:String, loader:URLLoaderDynamic)
            :void
        {
            var callback:Function = loader.callback;
            var type:String = URLShortenEvent.COMPLETE;
            
            if(callback != null)
                callback(url);
            loader.dispatchEvent(new URLShortenEvent(type, false, false, url));
        }
        
        public static function jdemcz(longUrl:String, callback:Function=null)
            :URLLoaderDynamic
        {
            var apiUrl:String = "http://www.jdem.cz/get";
            var request:URLRequest = new URLRequest(apiUrl);
            var variables:URLVariables = new URLVariables();
            
            variables.url = longUrl;
            return prepare(request, variables, callback);
        }
        
        public static function tinyurlcom(longUrl:String, 
            callback:Function=null):URLLoaderDynamic
        {
            var apiUrl:String = "http://tinyurl.com/api-create.php";
            var request:URLRequest = new URLRequest(apiUrl);
            var variables:URLVariables = new URLVariables();
            
            variables.url = longUrl;
            return prepare(request, variables, callback);
        }
        
        public static function bitly(longUrl:String, login:String, 
            apiKey:String, version:String="2.0.1", callback:Function=null)
            :URLLoaderDynamic
        {
            var apiUrl:String = "http://api.bit.ly/shorten";
            var request:URLRequest = new URLRequest(apiUrl);
            var variables:URLVariables = new URLVariables();
            
            variables.version = version;
            variables.longUrl = longUrl;
            variables.login = login;
            variables.apiKey = apiKey;
            return prepare(request, variables, callback, bitlyComplete);
        }
        
        private static function bitlyComplete(event:Event):void
        {
            var loader:URLLoaderDynamic = URLLoaderDynamic(event.target);
            var result:String = String(loader.data);
            var data:Object = JSON.decode(result);
            var url:String;
            
            loader.removeEventListener(Event.COMPLETE, bitlyComplete);
            for each(var resultItem:Object in data.results)
                url = resultItem.shortUrl;
            dispatch(url, loader);
        }
        
        public static function trim(longUrl:String, 
            callback:Function=null):URLLoaderDynamic
        {
            var apiUrl:String = "http://api.tr.im/v1/trim_simple";
            var request:URLRequest = new URLRequest(apiUrl);
            var variables:URLVariables = new URLVariables();
            
            variables.url = longUrl;
            return prepare(request, variables, callback);
        }
        
        public static function isgd(longUrl:String, 
            callback:Function=null):URLLoaderDynamic
        {
            var apiUrl:String = "http://is.gd/api.php";
            var request:URLRequest = new URLRequest(apiUrl);
            var variables:URLVariables = new URLVariables();
            
            variables.longurl = longUrl;
            return prepare(request, variables, callback);
        }
    }
}

we need loader to hold callback function reference, URLLoaderDynamic class pretty much fits this needs

package sk.yoz.net
{
    import flash.net.URLRequest;
    import flash.net.URLLoader;
    
    import sk.yoz.net.URLLoaderDynamic;
    
    public dynamic class URLLoaderDynamic extends URLLoader
    {
        public function URLLoaderDynamic(request:URLRequest=null)
        {
            super(request);
        }
        
    }
}

custom URLShortenEvent class is dispatched after url is returned

package sk.yoz.events
{
    import flash.events.Event;

    public class URLShortenEvent extends Event
    {
        public static const COMPLETE:String = "URLShortenEventCOMPLETE";
        
        private var _url:String;
        
        public function URLShortenEvent(type:String, bubbles:Boolean=false, 
            cancelable:Boolean=false, url:String="")
        {
            super(type, bubbles, cancelable);
            _url = url;
        }
        
        public function get url():String
        {
            return _url;
        }
    }
}

Leave a comment

Please be polite and on topic. Your e-mail will never be published.