package util { import com.adobe.crypto.MD5; import flash.events.Event; import flash.events.EventDispatcher; import flash.net.URLLoader; import flash.net.URLRequest; /** * Класс для работы с вконтактовым api * @author Kasoi | fsca.ru | kasoizz@gmail.com */ public class VKSender extends EventDispatcher { [Event(name = "complete", type = "flash.events.Event")] //////////////////////////////////////////////////////////////////// // Public properties //////////////////////////////////////////////////////////////////// public function get response() : XML { return this._response; } public function get lastCommand() : String { return this.command; } public function get testMode() : Boolean { return this._testMode; } public function set testMode(value: Boolean) : void { this._testMode = value; } //////////////////////////////////////////////////////////////////// // Private properties //////////////////////////////////////////////////////////////////// private var _path: String; private var _user: VKUser; private var _response: XML; private var _testMode: Boolean = false; private var command: String = ''; private var loader: URLLoader; private var commands: URLRequest; private var method: Function; //////////////////////////////////////////////////////////////////// // Public methods //////////////////////////////////////////////////////////////////// public function VKSender(user: VKUser, path: String = null) { this._user = user; if (path != null) this._path = path; else this._path = VK_API.API_PATH; this.loader = new URLLoader(); this.commands = new URLRequest(this._path); this.loader.addEventListener(Event.COMPLETE, this.loader_onLoad); } public function sendVars(...values) : void { var data: Array = []; if (values[0] is Array) data = values[0]; else data = values; if (values[0] is String && values.length == 1) { data = values[0].split('&'); } this.send(data); } public function getFriends(toMethod: Function = null) : void { this.method = toMethod; this.send(['method=' + VK_API.methods.GET_FRIENDS]); } public function getUserInfo(toMethod: Function = null) : void { this.method = toMethod; var comm: Array = ['method=' + VK_API.methods.GET_USER_INFO, 'viewer_id=' + this._user.viewer_id]; this.send(comm); } public function getUserInfoEx(toMethod: Function = null) : void { this.method = toMethod; this.send(['method=' + VK_API.methods.GET_USER_INFO_EX, 'viewer_id=' + this._user.viewer_id]); } /* * Получаете список профилей, указывая их айдишники * * @uids: *,(можно стрингом через запятую, а можно массив) * * @fields — дополнительные поля, которые нужно узнать от этих * профилей: * uid, first_name, last_name, nickname, sex, bdate (birthdate), * city, country, timezone, photo, photo_medium, photo_big, * has_mobile, rate * * @nameCase — склонение имен и фамилий(ники не трогаются): * именительный – nom, родительный – gen, дательный – dat, * винительный – acc, творительный – ins, предложный – abl * */ public function getProfiles(uids: * , fields: String = null, nameCase: String = null, toMethod: Function = null) : void { var u: String = uids.toString(); this.method = toMethod; if (nameCase == null) nameCase = ''; if (fields == null) fields = ''; this.send(['method=' + VK_API.methods.GET_PROFILES, 'viewer_id=' + this._user.viewer_id, 'uids=' + u, 'name_case=' + nameCase, 'fields=' + fields]); } //////////////////////////////////////////////////////////////////// // Private methods //////////////////////////////////////////////////////////////////// private function send(data: Array) : void { this.command = ''; var c: String = ''; data.push( 'v=' + VK_API.API_V, 'api_id=' + VK_API.API_ID); if (this._testMode) data.push('test_mode=1'); data = data.sort(); for (var i: int = 0; i < data.length; i++) { this.command += data[i]; c += data[i]; if (i < data.length - 1) this.command += '&'; } var user: VKUser = this._user; c = user.viewer_id + c + VK_API.SECRET_CODE; this.command += '&sig=' + MD5.hash(c); this.commands.data = this.command; this.loader.load(this.commands); } //////////////////////////////////////////////////////////////////// // Listeners //////////////////////////////////////////////////////////////////// private function loader_onLoad(e: Event) : void { this._response = new XML(this.loader.data); if (this.method != null) { this.method(this._response); this.method = null; } else { this.dispatchEvent(new Event(Event.COMPLETE)); } } } }