/*
 Copyright (c) 2006 Eric J. Feminella <eric@ericfeminella.com>
 All rights reserved.

 Permission is hereby granted, free of charge, to any person obtaining a copy
 of this software and associated documentation files (the "Software"), to deal
 in the Software without restriction, including without limitation the rights
 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 copies of the Software, and to permit persons to whom the Software is furnished
 to do so, subject to the following conditions:

 The above copyright notice and this permission notice shall be included in all
 copies or substantial portions of the Software.

 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
 INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
 PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
 HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
 OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
 SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

 @internal
 */

package com.ericfeminella.net
{
    import com.ericfeminella.utils.AbstractStaticType;
    import flash.net.URLLoader;
    import flash.net.URLRequest;
    import flash.net.URLRequestMethod;
    import flash.net.URLVariables;
    import mx.rpc.IResponder;
    import mx.rpc.events.FaultEvent;
    import mx.rpc.events.ResultEvent;

    /**
     * 
     * Provides an all static API for posting name / value pairs 
     * to a specifed URI
     * 
     * @see flash.net.URLVariables
     * @see mx.rpc.IResponder
     *
     */
    public final class HTTPPOSTGateway extends AbstractStaticType
    {
        /**
         * 
         * Posts a series of name / value pairs to a specified URI in 
         * a URL encoded query string
         *
         * @param URI in which the values are to be posted
         * @param arbitrary object consisting of name / value pairs
         * @param an object which implements <code>IResponder</code>
         * 
         */
        public static function post(uri:String, rawData:Object, responder:IResponder) : void
        {
            var nameValuePairs:URLVariables = new URLVariables();

            for (var name:String in rawData) 
            {
                nameValuePairs[name] = rawData[name];
            }

            var request:URLRequest = new URLRequest( uri );
            request.method = URLRequestMethod.POST;
            request.data   = nameValuePairs;

            var loader:URLLoader = new URLLoader();
            loader.addEventListener( ResultEvent.RESULT, responder.result );
            loader.addEventListener( FaultEvent.FAULT,   responder.fault  );
            loader.load( request );
        }
    }
}