constructor facility added
[statusbox:statusbox.git] / src / connector / communicator.js
1 net.status.js.Communicator=function(oauth){
2         this.oauth=oauth;
3         
4         /**
5          * @param method the endpoint of the api
6          *              eg:- /statuses/show.json
7          * @param params the array of parameters to be send to the service
8          * @param type the type of the response Object
9          * @param callback is the callback function to executed with the response
10          * @param custom the custom code to be append before the callback
11          * @param proxy whether to use Proxy method instead direct jsonp
12          * possible valiues GET | POST | null
13          * @param parentAPI the related API instance 
14          * eg:- Identi.ca or abc.status.net
15          */
16         this.requestAPI=function(method,params,type,callback,custom,proxy,parentAPI){
17                 var alteration="if("+ type +") response=net.status.js.Communicator.buildObject(response,'" + type + "',"+parentAPI+");";
18                 if(custom) alteration+=custom;
19                 if(proxy){
20                         var isPost=(proxy=="POST")?true:false;
21                         this.requestAPIWithAlteredResponseProxy
22                                 (method,params,callback,alteration,isPost);
23                 }
24                 else{
25                         this.requestAPIWithAlteredResponse(method,params,callback,alteration);
26                 }
27         };
28         
29         /**
30          * @param method api endpoint
31          * @param parametes in a Object
32          * @callback a callback function like function(response){..}
33          * @param alteration this is the codes where we can alter the response befor is gets to
34          * the callback
35          * the returning response is a variable called response
36          * you can alter it or add any code before that.
37          */
38         this.requestAPIWithAlteredResponse=function(method,params,callback,alteration){
39                 //make callback Global
40                 if(callback){
41                         var modifiedCallback=
42                                 net.status.js.Communicator.createGlobalFunctionWithAlteration(callback,alteration);
43                         
44                         if(!params)     params={callback:modifiedCallback};
45                         else params.callback=modifiedCallback;
46                 }
47                 
48                 var url=this.oauth.getRequestAPIUrl(method,params);
49                 
50                 jQuery.getScript(url);
51         };
52         
53         /**
54          * Same as requestAPIWithAlteredResponse() but using sendJSON Proxy
55          * 
56          * @param method api endpoint
57          * @param parametes in a Object
58          * @callback a callback function like function(response){..}
59          * @param alteration this is the codes where we can alter the response befor is gets to
60          * the callback
61          * the returning response is a variable called response
62          * you can alter it or add any code before that.
63          */
64         this.requestAPIWithAlteredResponseProxy=function(method,params,callback,alteration,isPost){
65                 //make callback Global
66                 var modifiedCallback="";
67                 if(callback){
68                         modifiedCallback=
69                                 net.status.js.Communicator.createGlobalFunctionWithAlteration(callback,alteration);
70                 }
71                 
72                 var url=this.oauth.getRequestAPIUrl(method,params,isPost);
73                 net.status.js.Communicator.sendJSON(url,modifiedCallback,isPost);
74         };
75
76 };
77
78
79 net.status.js.Communicator.createGlobalFunction=function(func){
80         //make callback Global
81         var callbackName="callback_" + Math.floor(Math.random()*10000);
82         jQuery.globalEval("var " + callbackName +"=" + func);
83         return callbackName;
84 };
85
86 net.status.js.Communicator.createDynamicFunction=function(script){
87         //make callback Global
88         var callbackName="callback_" + Math.floor(Math.random()*10000);
89         var func="function "+ callbackName +"(){"+script+"} ";
90         jQuery.globalEval(func);
91         return callbackName;
92 };
93
94 net.status.js.Communicator.createGlobalFunctionWithAlteration=function(callback,alteration){
95         //make callback Global
96         if(callback){
97                 var callbackName="callback_" + Math.floor(Math.random()*10000);
98                 jQuery.globalEval("var " + callbackName +"=" + callback);
99                 
100                 //creating modified callback
101                 
102                 var modifiedCallback=callbackName + "_m";
103                 var func=
104                         "function " + modifiedCallback + "(response){ " +
105                         alteration +
106                         "       " + callbackName + "(response);" +
107                         "}";
108                 
109                 jQuery.globalEval(func);
110         }
111         
112         return modifiedCallback;
113 };
114
115
116 /**
117  * @param url url to be request
118  * @callback string name of the callback tobe called
119  * 
120  */
121 net.status.js.Communicator.sendJSON=function(url,callbackName,isPost){
122         url=OAuthUtilities.urlEncode(url);
123         var callUrl="http://127.0.0.1/gsoc/statusbox/test/proxy.php?action=json&url=" + url + "&callback=" + callbackName ;
124         if(isPost) callUrl+="&isPost=1";
125         //console.log(callUrl);
126         jQuery.getScript(callUrl); 
127 };
128
129
130 //this will generate the perticular Object from the return response from the
131 //OAuth API
132 /**
133  * @param response the raw Object or Array of Objects
134  * @param type the type of the Object to build
135  * @param parentAPI the related API instance 
136  * eg:- Identi.ca or abc.status.net
137  */
138 net.status.js.Communicator.buildObject=function(response,type,parentAPI){
139         if(response instanceof Array){
140                 var users=[];
141                 for(item in response){
142                         users[item]=builtSingle(response[item],parentAPI);
143                 }
144                 return users;
145         }
146         else{
147                 return builtSingle(response,parentAPI);
148         }
149         
150         function builtSingle(data,parentAPI){
151                 var item;
152                 var addslashes=function (str) {
153                         if( typeof str != 'string') return str;
154                         str=str.replace(/\\/g,'\\\\');
155                         str=str.replace(/\'/g,'\\\'');
156                         str=str.replace(/\"/g,'\\"');
157                         str=str.replace(/\0/g,'\\0');
158                         return str;
159                 };
160
161                 eval("item=new " + type + "()");
162                 jQuery.extend(item,data);
163                 if(item.api) item.api(parentAPI);
164                 //calls the constructor();
165                 item.construct();
166                 
167                 return item;
168         }
169         
170 };
171