Skip to content

Instantly share code, notes, and snippets.

@rjungemann
Created January 14, 2010 02:45
Show Gist options
  • Save rjungemann/276801 to your computer and use it in GitHub Desktop.
Save rjungemann/276801 to your computer and use it in GitHub Desktop.
Access web services from AS in a Sinatra-like way
package com.thefifthcircuit.nancy {
import flash.net.URLLoader;
import flash.net.URLRequest;
import flash.net.URLVariables;
import flash.net.URLRequestMethod;
import flash.events.Event;
import flash.events.IOErrorEvent;
public class Nancy {
private var host: String;
public function Nancy(host: String) {
this.host = host;
}
public function get(path: String, vars: Object, onComplete: Function = null, onError: Function = null) : void {
req(URLRequestMethod.GET, path, vars, onComplete, onError);
}
public function post(path: String, vars: Object, onComplete: Function = null, onError: Function = null) : void {
req(URLRequestMethod.POST, path, vars, onComplete, onError);
}
public function req(method: *, path: String, vars: Object = null, onComplete: Function = null, onError: Function = null) : void {
var loader: URLLoader = new URLLoader();
var request: URLRequest = new URLRequest(host + path);
var variables: URLVariables = new URLVariables();
vars = (vars != null) ? vars: {};
if (vars != {}) {
for (var key: *invars) variables[key] = vars[key];
request.data = variables;
}
request.method = method;
if (onComplete != null) loader.addEventListener(Event.COMPLETE, onComplete);
if (onError != null) loader.addEventListener(IOErrorEvent.IO_ERROR, onError);
loader.load(request);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment