Final Flash Just Giving Parser

12 12 2007

Final Just Giving Example

Right so after several versions of this I finally have a nice class based AS2 version that’s optimised for performance and grabs the full data set. Thanks to all the team at Glue who worked on our Christmas card that inspired this research and also to Rich at Just Giving for his invaluable assistance and for amending their crossdomain.xml file to allow us Flash Devs access to their lovely RSS feeds:

FLA & com folder for download:

Download FLA

[code lang="actionscript"]

class com.glue.xmas.ui.JustGiving extends MovieClip {

// Class assets
private var serverStringPath:String = "http://www.justgiving.com/rss/getfundraisingpage.asp?eventgivinggroupid=";
private var jgGroupID:String;
private var xmlIn:XML;
private var xmlOut:XML;
private var updateJustGivingObj:Object;
private var updateJustGivingInterval;
private var pollSeconds:Number;
// On stage asset declarations
private var justGiving_mc:MovieClip;
private var target_txt:TextField;
private var total_txt:TextField;
private var online_txt:TextField;
private var offline_txt:TextField;
private var giftaid_txt:TextField;
// Temporary conversion assets
private var rcArr:Array;
private var rcStr:String;
private var t_arr:Array;
private var t_arr2:Array;
private var t_pounds:Number;
private var t_pence:Number;
private var t_total:Number;
private var dStr_init:String;
private var dStr_arr:Array;
private var dStr_arr2:Array;;
private var dStr_arr3:Array;
// Core Variable - with Public Getter/Setters
private var currentTarget:Number;
private var currentTotal:Number;
private var currentOnline:Number;
private var currentOffline:Number;
private var currentGiftAid:Number;

function JustGiving(){
// Poll secs & Just Giving Group ID
init(10, "963699");
}

private function init(secs:Number, groupID:String):Void{
pollSeconds = secs;
jgGroupID = groupID;
justGiving_mc = this;
setRepeat(pollSeconds, justGiving_mc, true);
hide();
}

//////////////////////////////////////////////////////////////////
// Hide & Show - Getters and Setters //
//////////////////////////////////////////////////////////////////

private function show():Void {
justGiving_mc._visible = true;
}
private function hide():Void {
justGiving_mc._visible = false;
}

public function set target(newTarget:Number):Void {
currentTarget = newTarget;
}
public function get target():Number {
return currentTarget;
}
public function set total(newTotal:Number):Void {
currentTotal = newTotal;
}
public function get total():Number {
return currentTotal;
}
public function set online(newOnline:Number):Void {
currentOnline = newOnline;
}
public function get online():Number {
return currentOnline;
}
public function set offline(newOffline:Number):Void {
currentOffline = newOffline;
}
public function get offline():Number {
return currentOffline;
}
public function set giftaid(newGiftAid:Number):Void {
currentGiftAid = newGiftAid;
}
public function get giftaid():Number {
return currentGiftAid;
}

//////////////////////////////////////////////////////////////////////////////////
// Setup Interval Set and Clear to control update period of live RSS data //
// //
// Seconds to leave between updates //
// Optional boolean - whether to execute instantly as well as on interval //
//////////////////////////////////////////////////////////////////////////////////

private function setRepeat(repeatTime:Number, mcScope:MovieClip, alsoCallOnInit:Boolean):Void {
updateJustGivingObj = new Object();
updateJustGivingObj.updateFromRSS = function():Void {
//On Interval - Parse RSS
trace("Requesting Fresh RSS Data...");
mcScope.parseJGRSS(mcScope.serverStringPath, mcScope);
}
updateJustGivingInterval = setInterval(updateJustGivingObj, "updateFromRSS", repeatTime*1000);
// Optional Instant Execute
if(alsoCallOnInit == true){
updateJustGivingObj.updateFromRSS();
}
}

private function clearRepeat():Void {
clearInterval(updateJustGivingInterval);
}

private function onJGFeedParse():Void {
target_txt.text = ""+target;
total_txt.text = ""+total;
online_txt.text = ""+online;
offline_txt.text = ""+offline;
giftaid_txt.text = ""+giftaid;
show();
}

//////////////////////////////////////////////////////////////////////////////////
// Main Parsing function and 2 String & Datatype Manipulation functions used by //
// removeCommas() //
// jgFeedStrToNum() //
// parseJGRSS() //
//////////////////////////////////////////////////////////////////////////////////

private function removeCommas(args:String):String {
trace("Removing Comma's from tridecimal string "+args);
// Split the string into tridecimal strings and reconcatenate without the commas
rcArr = args.split(',');
var rcStr:String = "";
for (var i=0; i rcStr += rcArr[i];
}
return rcStr;
}

private function jgFeedStrToNum(args:String):Number {

// Remove any commas as Just Giving add them at triddecimals e.g "£1,056,234"
if (args.indexOf(',') != -1) {
args = removeCommas(args);
}
// Split Donations string into Pounds/Pence - (or Dollars/Cents)
t_arr = args.split('.');
// Remove any trailing space from after the Pence/Cents string
t_arr2 = t_arr[1].split(' ');
var t_pounds:Number = new Number(t_arr[0]);
var t_pence:Number = new Number(t_arr2[0]);
//trace("Pounds "+t_pounds+" Pence "+t_pence+" Pence in Pounds "+(t_pence/100));
var t_total:Number = t_pounds+(t_pence/100);

return t_total;
}

private function parseJGRSS(jgRSSUrl:String, mcScope:MovieClip) {
var xmlIn:XML = new XML();
var xmlOut:XML = new XML();
xmlIn.ignoreWhite = true;

xmlIn.onLoad = function(success) {
if (success) {
//////////////////////////////////////////////////////////////////////////////////////////////
// PROCESS THE "Item Description" NODE TO GET ALL THE FINANCE TOTALS //
//////////////////////////////////////////////////////////////////////////////////////////////

trace("Loaded Feed & Parsing RSS...");

var dStr_init:String = this.firstChild.childNodes[0].childNodes[6].childNodes[2].childNodes[0].nodeValue;

// Split XML node string by html list end symbol
dStr_arr = dStr_init.split('

');

// Get Fundraising Page Target :
dStr_arr2 = dStr_arr[0].split(';');
currentTarget = mcScope.jgFeedStrToNum(dStr_arr2[1]);
//trace("currentTarget = "+currentTarget);
//mcScope.target_txt.text = currentTarget;
mcScope.target = currentTarget;

// Get Total donations to date :
dStr_arr2 = dStr_arr[1].split(';');
currentTotal = mcScope.jgFeedStrToNum(dStr_arr2[1]);
//trace("currentTotal = "+currentTotal);
//mcScope.total_txt.text = currentTotal;
mcScope.total = currentTotal;

// Get Online donations to date :
dStr_arr2 = dStr_arr[2].split(';');
currentOnline = mcScope.jgFeedStrToNum(dStr_arr2[1]);
//trace("currentOnline = "+currentOnline);
//mcScope.online_txt.text = currentOnline;
mcScope.online = currentOnline;

// Get Amount raised offline :
dStr_arr2 = dStr_arr[3].split(';');
currentOffline = mcScope.jgFeedStrToNum(dStr_arr2[1]);
//trace("currentOffline = "+currentOffline);
//mcScope.offline_txt.text = currentOffline;
mcScope.offline = currentOffline;

// Get The UK Gift Aid Reclaimed :
dStr_arr2 = dStr_arr[4].split(';');
dStr_arr3 = dStr_arr2[1].split('');
currentGiftAid = mcScope.jgFeedStrToNum(dStr_arr3[0]);
//trace("currentGiftAid = "+currentGiftAid);
//mcScope.giftaid_txt.text = currentGiftAid;
mcScope.giftaid = currentGiftAid;

mcScope.onJGFeedParse();

} else {
trace("RSS Load Failed");
}
};
xmlOut.sendAndLoad(jgRSSUrl+jgGroupID, xmlIn);
}
}

[/code]


Actions

Informations

Leave a comment

You can use these tags : <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>