<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Jozef Chúťka&#039;s blog &#187; URLLoaderDynamic</title>
	<atom:link href="http://blog.yoz.sk/tag/urlloaderdynamic/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.yoz.sk</link>
	<description>My life, my work</description>
	<lastBuildDate>Thu, 09 Sep 2010 14:00:43 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>URLShorten Class (update)</title>
		<link>http://blog.yoz.sk/2010/02/urlshorten-class/?utm_source=rss&amp;utm_medium=rss&amp;utm_campaign=urlshorten-class</link>
		<comments>http://blog.yoz.sk/2010/02/urlshorten-class/#comments</comments>
		<pubDate>Thu, 04 Feb 2010 10:52:18 +0000</pubDate>
		<dc:creator>Jozef Chúťka</dc:creator>
				<category><![CDATA[ActionScript]]></category>
		<category><![CDATA[Flash / Flex]]></category>
		<category><![CDATA[shorten]]></category>
		<category><![CDATA[URLLoader]]></category>
		<category><![CDATA[URLLoaderDynamic]]></category>
		<category><![CDATA[URLRequest]]></category>
		<category><![CDATA[URLShorten]]></category>
		<category><![CDATA[URLShortenEvent]]></category>
		<category><![CDATA[URLVariables]]></category>

		<guid isPermaLink="false">http://blog.yoz.sk/?p=989</guid>
		<description><![CDATA[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 [...]]]></description>
			<content:encoded><![CDATA[<p>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 <img src='http://blog.yoz.sk/wp-includes/images/smilies/icon_smile.gif' alt=':-)' class='wp-smiley' />  feel free to use it and enjoy </p>
<p>Update: <a href="http://jdem.cz">jdem.cz</a>, <a href="http://tinyurl.com">tinyurl.com</a>, <a href="http://bit.ly">bit.ly</a>, <a href="http://tr.im">tr.im</a>, <a href="http://is.gd">is.gd</a> providers implemented.</p>
<p><span id="more-989"></span></p>
<p>Application (view source enabled), please use your own api key for bit.ly</p>
<p><iframe width="100%" height="200" src="http://blog.yoz.sk/examples/URLShorten/"></iframe></p>
<p>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&#8230;</p>
<p>There are two methods you can handle resuls:</p>
<pre class="brush: as3;">// 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;
}</pre>
<p><a href="http://classes.yoz.sk/sk/yoz/net/URLShorten.as">URLShorten class</a></p>
<pre class="brush: as3;">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 = &quot;http://www.jdem.cz/get&quot;;
            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 = &quot;http://tinyurl.com/api-create.php&quot;;
            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=&quot;2.0.1&quot;, callback:Function=null)
            :URLLoaderDynamic
        {
            var apiUrl:String = &quot;http://api.bit.ly/shorten&quot;;
            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 = &quot;http://api.tr.im/v1/trim_simple&quot;;
            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 = &quot;http://is.gd/api.php&quot;;
            var request:URLRequest = new URLRequest(apiUrl);
            var variables:URLVariables = new URLVariables();

            variables.longurl = longUrl;
            return prepare(request, variables, callback);
        }
    }
}</pre>
<p>we need loader to hold callback function reference, <a href="http://classes.yoz.sk/sk/yoz/net/URLLoaderDynamic.as">URLLoaderDynamic class</a> pretty much fits this needs</p>
<pre class="brush: as3;">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);
        }

    }
}</pre>
<p>custom <a href="http://classes.yoz.sk/sk/yoz/events/URLShortenEvent.as">URLShortenEvent class</a> is dispatched after url is returned</p>
<pre class="brush: as3;">package sk.yoz.events
{
    import flash.events.Event;

    public class URLShortenEvent extends Event
    {
        public static const COMPLETE:String = &quot;URLShortenEventCOMPLETE&quot;;

        private var _url:String;

        public function URLShortenEvent(type:String, bubbles:Boolean=false,
            cancelable:Boolean=false, url:String=&quot;&quot;)
        {
            super(type, bubbles, cancelable);
            _url = url;
        }

        public function get url():String
        {
            return _url;
        }
    }
}</pre>
]]></content:encoded>
			<wfw:commentRss>http://blog.yoz.sk/2010/02/urlshorten-class/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
