<?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; URLLoader</title>
	<atom:link href="http://blog.yoz.sk/tag/urlloader/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.yoz.sk</link>
	<description>My life, my work</description>
	<lastBuildDate>Tue, 31 Jan 2012 12:40:53 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>URLRequestBuffer &#8211; Buffer Your URLRequests</title>
		<link>http://blog.yoz.sk/2010/06/urlrequestbuffer-buffer-your-requests/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=urlrequestbuffer-buffer-your-requests</link>
		<comments>http://blog.yoz.sk/2010/06/urlrequestbuffer-buffer-your-requests/#comments</comments>
		<pubDate>Wed, 02 Jun 2010 14:09:50 +0000</pubDate>
		<dc:creator>Jozef Chúťka</dc:creator>
				<category><![CDATA[ActionScript]]></category>
		<category><![CDATA[Flash / Flex]]></category>
		<category><![CDATA[buffer]]></category>
		<category><![CDATA[delay]]></category>
		<category><![CDATA[Loader]]></category>
		<category><![CDATA[priority]]></category>
		<category><![CDATA[timeout]]></category>
		<category><![CDATA[URLLoader]]></category>
		<category><![CDATA[URLRequest]]></category>
		<category><![CDATA[URLRequestBuffer]]></category>

		<guid isPermaLink="false">http://blog.yoz.sk/?p=1698</guid>
		<description><![CDATA[Let&#8217;s suppose your app makes a lot of http requests at once, and you need a good administration for that. Based on used browser your app is running on, you are only allowed to limited request count &#8211; mostly its 2 requests per domain, the remaining ones are sitting in browser buffer waiting to finish [...]]]></description>
			<content:encoded><![CDATA[<p><img src="http://blog.yoz.sk/wp-content/uploads/buffer.jpg" alt="" title="buffer" width="200" height="100" class="alignleft size-full wp-image-1711" /></p>
<p>Let&#8217;s suppose your app makes a lot of http requests at once, and you need a good administration for that. Based on used browser your app is running on, you are only allowed to limited request count &#8211; mostly its 2 requests per domain, the remaining ones are sitting in browser buffer waiting to finish those actual. URLRequestBuffer is handy class that lets you manage URLRequest-s. E.g. you are creating image gallery and request hundreds of images in gallery at a time, now when user navigates throug the images you need to make some request more prioritized before others, some to be canceled, some may timeout etc. Here comes URLRequestBuffer in scene.</p>
<p><span id="more-1698"></span></p>
<p>Pushing requests into the buffer automaticaly sorts requests by priority and tries to load next request based on priority/delay logic. After request is complete (timeouted, or error) it loads next and so on. Usage is very simple once you get familiar with 3 classes it consist of (<a href="http://classes.yoz.sk/sk/yoz/net/URLRequestBuffer.as">URLRequestBuffer</a>, <a href="http://classes.yoz.sk/sk/yoz/net/URLRequestBufferItem.as">URLRequestBufferItem</a> and <a href="http://classes.yoz.sk/sk/yoz/events/URLRequestBufferEvent.as">URLRequestBufferEvent</a>)</p>
<pre class="brush: as3; title: ; notranslate">// define buffer and max simultaneous (active) requests
var buffer:URLRequestBuffer = new URLRequestBuffer(2);

// loader - URLLoader or Loader instance
// request - URLRequest instance
// priority - uint, higher priority requests loads first
// delay - request is called after X milliseconds after pushing
// pushing requests into buffer returns URLRequestBufferItem, next suitable requests called
var bufferItem:URLRequestBufferItem = buffer.push(loader, request, priority, delay);
var bufferId:uint = bufferItem.id;

// URLRequestBufferItem by bufferId
buffer.getWaitingById(bufferId)
buffer.getActiveById(bufferId)

// stop and trash request (buffered or active)
buffer.removeWaitingById(bufferId);
buffer.removeActiveById(bufferId);

// enumerating waiting item list
for(var i:uint = 0; i &lt; buffer.countWaiting; i++)
    bufferItem = buffer.getWaitingItem(i);

// enumerating active item list
for(var i:uint = 0; i &lt; buffer.countActive; i++)
    bufferItem = buffer.getActiveItem(i);

// buffer event handling
buffer.addEventListener(URLRequestBufferEvent.WAITING_REQUEST_ADDED, onBufferChange);
buffer.addEventListener(URLRequestBufferEvent.WAITING_REQUEST_REMOVED, onBufferChange);
buffer.addEventListener(URLRequestBufferEvent.ACTIVE_REQUEST_ADDED, onBufferChange);
buffer.addEventListener(URLRequestBufferEvent.ACTIVE_REQUEST_REMOVED, onBufferChange);
buffer.addEventListener(URLRequestBufferEvent.REQUEST_TIMEOUT, onBufferChange);

private function onBufferChange(event:URLRequestBufferEvent):void
{
    var bufferItem:URLRequestBufferItem = event.item;

    // some stats
    trace(buffer.countWaiting, buffer.countActive)
}</pre>
]]></content:encoded>
			<wfw:commentRss>http://blog.yoz.sk/2010/06/urlrequestbuffer-buffer-your-requests/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>URLShorten Class (update)</title>
		<link>http://blog.yoz.sk/2010/02/urlshorten-class/?utm_source=rss&#038;utm_medium=rss&#038;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; title: ; notranslate">// 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; title: ; notranslate">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; title: ; notranslate">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; title: ; notranslate">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>

