net.wonderfl.utils.WonderflAPI
Accessing Wonderfl Api
/**
* Copyright aobyrne ( http://wonderfl.net/user/aobyrne )
* MIT License ( http://www.opensource.org/licenses/mit-license.php )
* Downloaded from: http://wonderfl.net/c/aBbe
*/
package
{
import com.adobe.serialization.json.JSON;
import flash.display.Loader;
import flash.display.Sprite;
import flash.events.Event;
import flash.events.HTTPStatusEvent;
import flash.events.IEventDispatcher;
import flash.events.IOErrorEvent;
import flash.events.ProgressEvent;
import flash.net.URLLoader;
import flash.net.URLRequest;
import flash.text.TextField;
import net.wonderfl.data.APIUserCodesData;
import net.wonderfl.data.SmallCodeData;
import net.wonderfl.utils.WonderflAPI;
/**
* ...
* @author sandcastles
*/
public class AccessingWonderflApi extends Sprite
{
private var ldr:URLLoader;
private var aPIUserCodesData:APIUserCodesData;
private var tf:TextField;
private const WONDERFL_ID:String = '1QBp';
public function AccessingWonderflApi()
{
tf = TextField(addChild(new TextField));
tf.width = tf.height = 465;
tf.wordWrap = true;
var params:Object = { };
//params = loaderInfo.parameters;
/*
params["key"] = "9gdu";
params["appId"]=
params["domain"]=
params["viewer.iconURL"]=
*/
params["open_api_key"] = 'a76b80051993a7c6322bf8b4bbc457b44c3d4f5c';
params["viewer.displayName"] = 'aobyrne';
var wonderflAPI:WonderflAPI = new WonderflAPI(params);
ldr = new URLLoader;
ldr.addEventListener(Event.COMPLETE, _complete );
ldr.load(wonderflAPI.apiUserCodes('aobyrne'));
}
private function _complete(e:*):void
{
ldr.removeEventListener(Event.COMPLETE, _complete);
var rawData:Object = JSON.decode(ldr.data);
aPIUserCodesData = new APIUserCodesData(rawData);
var c:Vector.<SmallCodeData> = aPIUserCodesData.codes;
var l:int = c.length;
var item:SmallCodeData;
for (var i:int = 0; i < l; i++)
{
item = SmallCodeData(c[i]);
if (item.id==WONDERFL_ID)
{
cTrace( "title: " + item.title );
addLoader(item.swf);
}
}
//aPIUserCodesData.message
//aPIUserCodesData.length
//aPIUserCodesData.codes[1].swf
cTrace( "aPIUserCodesData.length : " + aPIUserCodesData.length );
cTrace( "aPIUserCodesData.message : " + aPIUserCodesData.message );
cTrace( "aPIUserCodesData.isOK : " + aPIUserCodesData.isOK );
//traceme(new APIUserCodesData(rawData), rawData);
cTrace(APIUserCodesData + ' test ok');
}
private function cTrace(...o):void
{
tf.appendText(o + "\n");
trace( o );
tf.scrollV = tf.maxScrollV;
}
private function addLoader(url:String):void
{
var ladr:Loader = new Loader();
configureListeners(ladr.contentLoaderInfo);
//ladr.contentLoaderInfo.addEventListener(Event.COMPLETE, onLoadedComplete);
//var smallCodeData:SmallCodeData = SmallCodeData(aPIUserCodesData.codes[0]);
//cTrace( "smallCodeData.title : " + smallCodeData.title );
//cTrace( "smallCodeData.title : " + smallCodeData.id );
var urlReq:URLRequest = new URLRequest(url);
ladr.load(urlReq);
addChild(ladr);
}
/*
* taken from https://github.com/mash/WonderflUtils
*/
private function traceme(a:Object, b:Object):void
{
var value:*;
for (var key:String in b) {
cTrace( "key : -------------------------------->" + key );
value = a[key];
if (typeof value == 'object') {
cTrace('=========== '+key+' =======================');
traceme(value, b[key]);
} else {
if (value != b[key]) throw new Error('doesnot match ' + key);
cTrace(key + ' -> '+b[key]);
}
}
}
/**
* Functionality taken from flash help
* @param dispatcher
*/
private function configureListeners(dispatcher:IEventDispatcher):void {
dispatcher.addEventListener(Event.COMPLETE, completeHandler);
dispatcher.addEventListener(HTTPStatusEvent.HTTP_STATUS, httpStatusHandler);
dispatcher.addEventListener(Event.INIT, initHandler);
dispatcher.addEventListener(IOErrorEvent.IO_ERROR, ioErrorHandler);
dispatcher.addEventListener(Event.OPEN, openHandler);
dispatcher.addEventListener(ProgressEvent.PROGRESS, progressHandler);
dispatcher.addEventListener(Event.UNLOAD, unLoadHandler);
}
private function completeHandler(event:Event):void {
cTrace("completeHandler: " + event);
}
private function httpStatusHandler(event:HTTPStatusEvent):void {
cTrace("httpStatusHandler: " + event);
}
private function initHandler(event:Event):void {
cTrace("initHandler: " + event);
}
private function ioErrorHandler(event:IOErrorEvent):void {
cTrace("ioErrorHandler: " + event);
}
private function openHandler(event:Event):void {
cTrace("openHandler: " + event);
}
private function progressHandler(event:ProgressEvent):void {
cTrace("progressHandler: bytesLoaded=" + event.bytesLoaded + " bytesTotal=" + event.bytesTotal);
}
private function unLoadHandler(event:Event):void {
cTrace("unLoadHandler: " + event);
}
}
}