package com.mariusht.puremvcexample.model
{
    import flash.events.Event;
    import flash.net.URLRequestMethod;
    
    import mx.collections.XMLListCollection;
    import mx.rpc.events.FaultEvent;
    import mx.rpc.events.ResultEvent;
    import mx.rpc.http.HTTPService;
    
    import org.puremvc.as3.interfaces.IProxy;
    import org.puremvc.as3.patterns.proxy.Proxy;

    public class RssProxy extends Proxy implements IProxy
    {
        public static const NAME:String = 'RssProxy';
        
        public static const RSS_SEND:String = 'rssSend';
        public static const RSS_RECEIVED:String = 'rssReceived';
        
        private static const RSS_URL:String = 'http://rss.news.yahoo.com/rss/topstories';
        private var service:HTTPService;
        
        public function RssProxy()
        {
            super(NAME, new XMLListCollection());
            
            service = new HTTPService();
            service.url = RSS_URL;
            service.useProxy = false;
            service.method = URLRequestMethod.POST;
            service.resultFormat = HTTPService.RESULT_FORMAT_E4X;
            service.addEventListener(FaultEvent.FAULT, onFault);
            service.addEventListener(ResultEvent.RESULT, onResult);
        }
        
        public function send():void
        {
            service.send();
        }
        
        private function onResult(event:Event):void
        {
            facade.sendNotification(RSS_RECEIVED, service.lastResult);
        }
        
        private function onFault(event:FaultEvent):void
        {
            trace('fault');
        }
    }
}