Urls

Don’t have time to ‘talk’ about this one, just whipped it together for something I needed. Could probably use a little bit of work, but you guys get the idea.

Just a wrapper for url strings to make them a little easier to work with, along with a ‘utility’ method for getting a URL object for the url of the current page the swf is in.

Stuff I think will be added:

  • representing the path in a url (possibly a Path object)
  • filename (?)
  • support

the two classes…

URL and QueryString

URL

package com.avila.net
{
	import flash.external.ExternalInterface;
	
	public class URL
	{
		private static const PROTOCOL:RegExp = /^[a-zA-Z]*(?=:)/g;
		private static const HOST:RegExp = /(?<=:)\/\/(\w|\.)+.(\w){3}\/*/g;
		private static const PORT:RegExp = /(?<=:)(\d)+/g;
		
		private var _url:String;
		public function get url():String { return _url; }
		
		/**
		 * Returns the protocol of the url.
		 */
		public function get protocol():String
		{
			return url.match( PROTOCOL )[0];
		}
		
		/**
		 * Returns the host domain of the url.
		 */
		public function get host():String
		{
			return url.match( HOST )[0];
		}
		
		/**
		 * Returns the port number of the url.
		 */
		public function get port():int
		{
			return int(url.match( PORT )[0]);
		}
		
		private var _queryString:QueryString;
		public function get queryString():QueryString { return _queryString; }
		
		/**
		 * A URL object represents and provides methods for working
		 * with a URL string.
		 */
		public function URL( url:String )
		{
			_url = url;

			if ( url.indexOf( '?' ) != -1 )
				_queryString = new QueryString( url.split( '?' )[1] );
		}
		
		/**
		 * Returns the URL string the URL object represents.
		 */
		public function toString():String
		{
			return _url;
		}
		
		/**
		 * Returns a URL object which represents the URL of the html page 
		 * of the embedded swf.  This URL contains all query string, and anchor information.
		 */
		public static function getPageURL():URL
		{
			return new URL( String(ExternalInterface.call( "function get_url() { return window.location.toString(); }" )) );
		}
	}
}

QueryString

package com.avila.net
{
	internal class QueryString
	{
		private var _string:String;
		public function get string():String { return _string; }
		
		private var queryString:Object;
		
		public function QueryString( string:String )
		{
			_string = string;
			parseString();
		}
		
		public function getValue( name:String ):String
		{
			return queryString[ name ];
		}

		// parses out the query string into the queryString object.
		private function parseString():void
		{		
			queryString = new Object();
			var temp:Object = new Object();
			
			var args:Array = string.split( "&" );
        
    		while( args.length )
			{
        		temp = args.shift().split( "=" );
        		queryString[ temp[ 0 ] ] = temp[ 1 ];
		    }
		}
	}
}

How it’d be used…

URLDevelopment


package 
{
	import flash.display.Sprite;
	import com.avila.net.URL;

	public class URLDevelopment extends Sprite
	{
		public function URLDevelopment()
		{
			var url:URL = new URL( "http://specialrelativity.org:80/?pid=1" );
			trace( url.protocol );
			trace( url.host );
			trace( url.port );
			
			trace( url.queryString.getValue( 'pid' ) );
		}
	}
}

That’s about it. Enjoy. Be on the lookout for an update and an official release on my blog.

-M