changeorgapi.js 43 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078
  1. /**
  2. * ChangeOrgApiUtils
  3. *
  4. * Common utilities.
  5. *
  6. * @package ChangeOrgApi
  7. * @author Rick Buczynski <richard.buczynski@gmail.com>
  8. */
  9. var ChangeOrgApiUtils={
  10. debugMode: false,
  11. lastTimestamp: 0,
  12. proxy: '',
  13. /**
  14. * Append query parameters to the given URL.
  15. *
  16. * @param string url
  17. * @param mixed params
  18. * @return string
  19. */
  20. addParams: function(url,params) {
  21. if(typeof url=='string') {
  22. var body=[], parts=url.split('?');
  23. if(typeof params=='string')
  24. params=this.expandRequest(params);
  25. if(parts.length>1)
  26. params=this.extend(this.expandRequest(parts.pop()),params || {});
  27. for(var key in params)
  28. body.push(key+'='+encodeURIComponent(params[key]));
  29. return url.split('?').shift()+'?'+body.join('&');
  30. }
  31. },
  32. /**
  33. * Bind values to the given input.
  34. *
  35. * @param string input
  36. * @param mixed data
  37. * @return string
  38. */
  39. bind: function(input,data) {
  40. if(typeof data!='object')
  41. data=[data];
  42. if(typeof input=='string' && typeof data=='object') {
  43. for(var key in data) {
  44. if(data instanceof Array)
  45. input=input.replace(new RegExp(':[a-zA-Z0-9_]+'),new String(data[key]));
  46. else
  47. input=input.replace(new RegExp(':'+key,'g'),new String(data[key]));
  48. }
  49. }
  50. return input;
  51. },
  52. /**
  53. * Expand a request body into an object.
  54. *
  55. * @param string data
  56. * @param object targetForm
  57. * @return object
  58. */
  59. expandRequest: function(data,targetForm) {
  60. var parts=data.split('&'), pair, data={};
  61. for(var i=0;i<parts.length;i++) {
  62. if(!parts[0])
  63. continue;
  64. pair=parts[i].split('=');
  65. data[pair[0]]=decodeURIComponent(pair[1]);
  66. if(targetForm) {
  67. var element=document.createElement('input');
  68. element.type='hidden';
  69. element.name=pair[0];
  70. element.value=data[pair[0]];
  71. targetForm.appendChild(element);
  72. }
  73. }
  74. return data;
  75. },
  76. /**
  77. * Merge multiple 1-dimensional objects.
  78. *
  79. * @return object
  80. */
  81. extend: function() {
  82. var objects=arguments, object={};
  83. for(var i=0;i<objects.length;i++) {
  84. if(objects[i]) {
  85. for(var property in objects[i])
  86. object[property]=objects[i][property];
  87. }
  88. }
  89. return object;
  90. },
  91. /**
  92. * Generate a unique key.
  93. *
  94. * @param string prefix
  95. * @param string suffix
  96. * @return string
  97. */
  98. getKey: function(prefix,suffix) {
  99. if(!prefix)
  100. prefix='';
  101. if(!suffix)
  102. suffix='';
  103. var min=(this.lastTimestamp?this.lastTimestamp:(new Date()).getTime()),
  104. max=this.lastTimestamp=(new Date()).getTime()+1;
  105. return prefix+(Math.floor(Math.random()*(max-min+1))+min).toString()+suffix;
  106. },
  107. /**
  108. * Generic use lambda.
  109. *
  110. * @return void
  111. */
  112. lamda: function() { },
  113. };/**
  114. * ChangeOrgApiException
  115. *
  116. * Library exception wrapper.
  117. *
  118. * @package ChangeOrgApi
  119. * @author Rick Buczynski <richard.buczynski@gmail.com>
  120. */
  121. var ChangeOrgApiException=function(message) {
  122. return {
  123. name : 'ChangeOrgApiException',
  124. 'message' : message,
  125. toString : function() {
  126. return this.name+': '+this.message;
  127. }
  128. };
  129. };/**
  130. * ChangeOrgApiClient
  131. *
  132. * A standardized object for transporting Change.org API developer credentials.
  133. *
  134. * @see https://github.com/change/api_docs/blob/master/v1/documentation/authentication.md
  135. *
  136. * @package ChangeOrgApi
  137. * @author Rick Buczynski <richard.buczynski@gmail.com>
  138. */
  139. var ChangeOrgApiClient=function(data) {
  140. this._data={};
  141. /**
  142. * Get default client properties.
  143. *
  144. * @return object
  145. */
  146. this._getDefaults=function() {
  147. return {
  148. api_key : '',
  149. secret : ''
  150. };
  151. };
  152. /**
  153. * Get the client API key.
  154. *
  155. * @return string
  156. */
  157. this.getApiKey=function() {
  158. return this._data.api_key;
  159. };
  160. /**
  161. * Get the client secret.
  162. *
  163. * @return string
  164. */
  165. this.getSecret=function() {
  166. return this._data.secret;
  167. };
  168. /**
  169. * Set the client API key.
  170. *
  171. * @param string apiKey
  172. * @return ChangeOrgApiClient
  173. */
  174. this.setApiKey=function(apiKey) {
  175. this._data[api_key]=apiKey;
  176. return this;
  177. };
  178. /**
  179. * Set the client secret.
  180. *
  181. * @param string secret
  182. * @return ChangeOrgApiClient
  183. */
  184. this.setSecret=function(secret) {
  185. this._data[secret]=secret;
  186. return this;
  187. };
  188. /**
  189. * Initialization
  190. ******************/
  191. this._data=this._getDefaults();
  192. if(typeof data=='object') {
  193. for(var key in data) {
  194. this._data[key]=data[key];
  195. }
  196. }
  197. };/**
  198. * ChangeOrgApiPetitionConnection
  199. *
  200. * A standardized connection object for interacting with the Change.org API.
  201. *
  202. * @package ChangeOrgApi
  203. * @author Rick Buczynski <richard.buczynski@gmail.com>
  204. */
  205. var ChangeOrgApiConnection=function(options) {
  206. this._api='https://api.change.org';
  207. this._data={};
  208. /* @var _connection XMLHttpRequest */
  209. this._connection=null;
  210. this._params={};
  211. this._proxy='';
  212. /**
  213. * Get default connection options.
  214. *
  215. * @return object
  216. */
  217. this._getDefaults=function() {
  218. return {
  219. content_type : 'application/x-www-form-urlencoded',
  220. endpoint : '',
  221. method : 'GET',
  222. onSuccess : ChangeOrgApiUtils.lambda
  223. };
  224. };
  225. /**
  226. * Get the real [full] endpoint as a URL.
  227. *
  228. * @param boolean endpointOnly
  229. * @return string
  230. */
  231. this._getEndpoint=function(endpointOnly) {
  232. var params=[];
  233. for(var key in this._params)
  234. params.push(key+'='+encodeURIComponent(this._params[key]));
  235. params=(params.length?'?':'')+params.join('&');
  236. return (
  237. (endpointOnly?'':this._api)
  238. +this._data.endpoint
  239. +params
  240. );
  241. };
  242. /**
  243. * Get the proxy URL for the given request.
  244. *
  245. * @param object data
  246. * @return string
  247. */
  248. this._getEndpointProxied=function(data) {
  249. return ChangeOrgApiUtils.addParams(this._proxy,{
  250. r : this._getEndpoint(),
  251. p : data,
  252. m : this._data.method,
  253. c : this._data.content_type,
  254. d : ChangeOrgApiUtils.debugMode?'1':'0'
  255. });
  256. };
  257. /**
  258. * Create a new connection.
  259. *
  260. * @param object data
  261. * @return XMLHttpRequest
  262. */
  263. this._getXmlHttpRequest=function(data) {
  264. var object=new XMLHttpRequest();
  265. var self=this;
  266. object.onreadystatechange=function() {
  267. if(self.getIsDone()) {
  268. var response=new ChangeOrgApiResponse(self);
  269. data.onSuccess.call(null,response);
  270. }
  271. };
  272. return object;
  273. };
  274. /**
  275. * Prepare the connection.
  276. *
  277. * @return ChangeOrgApiConnection
  278. */
  279. this._prepareConnection=function() {
  280. this._connection=this._getXmlHttpRequest(this._data);
  281. return this;
  282. };
  283. /**
  284. * Get the underlying connection object.
  285. *
  286. * @return XMLHttpRequest
  287. */
  288. this.getConnection=function() {
  289. return this._connection;
  290. };
  291. /**
  292. * Get the request content type.
  293. *
  294. * @return string
  295. */
  296. this.getContentType=function() {
  297. return this._data.content_type;
  298. };
  299. /**
  300. * Get the request endpoint.
  301. *
  302. * @param boolean withParams
  303. * @return string
  304. */
  305. this.getEndpoint=function(withParams) {
  306. return (withParams?this._getEndpoint(true):this._data.endpoint);
  307. };
  308. /**
  309. * Check if the request headers have been received.
  310. *
  311. * @return boolean
  312. */
  313. this.getHasReceivedHeaders=function() {
  314. return (this._connection.readyState===2);
  315. };
  316. /**
  317. * Check if the request has completed.
  318. *
  319. * @return boolean
  320. */
  321. this.getIsDone=function() {
  322. return (this._connection.readyState===4);
  323. };
  324. /**
  325. * Check if the request is loading.
  326. *
  327. * @return boolean
  328. */
  329. this.getIsLoading=function() {
  330. return (this._connection.readyState===3);
  331. };
  332. /**
  333. * Check if the request has been opened.
  334. *
  335. * @return boolean
  336. */
  337. this.getIsOpened=function() {
  338. return (this._connection.readyState===1);
  339. };
  340. /**
  341. * Check if the request has not been sent.
  342. *
  343. * @return boolean
  344. */
  345. this.getIsUnsent=function() {
  346. return (this._connection.readyState===0);
  347. };
  348. /**
  349. * Get the request method.
  350. *
  351. * @return string
  352. */
  353. this.getMethod=function() {
  354. return this._data.method;
  355. };
  356. /**
  357. * Get the request success handler.
  358. *
  359. * @return function
  360. */
  361. this.getOnSuccess=function() {
  362. return this._data.onSuccess;
  363. };
  364. /**
  365. * Get the request URL parameters.
  366. * @return object
  367. */
  368. this.getParams=function() {
  369. return this._params;
  370. };
  371. /**
  372. * Get the parsed response.
  373. *
  374. * @return object|null
  375. */
  376. this.getResponse=function() {
  377. var response=null;
  378. try {
  379. response=JSON.parse(this._connection.responseText);
  380. }
  381. catch(error) {
  382. response=null;
  383. }
  384. return response;
  385. };
  386. /**
  387. * Get the raw connection ready state.
  388. *
  389. * @return number
  390. */
  391. this.getReadyState=function() {
  392. return this._connection.readyState;
  393. };
  394. /**
  395. * Get the connection status.
  396. *
  397. * @return string
  398. */
  399. this.getStatus=function() {
  400. return this._connection.status;
  401. }
  402. /**
  403. * Send the request.
  404. *
  405. * @param string data
  406. * @return ChangeOrgApiConnection
  407. */
  408. this.send=function(data) {
  409. if(!this._connection || this.getIsUnsent())
  410. this._prepareConnection();
  411. if(!this.getIsOpened()) {
  412. var endpoint=this._getEndpoint();
  413. if(this._proxy) {
  414. endpoint=this._getEndpointProxied(data);
  415. this._data.method='GET';
  416. }
  417. this._connection.open(this._data.method,endpoint,true);
  418. }
  419. if(this._connection.setRequestHeader)
  420. this._connection.setRequestHeader('Content-type',this._data.content_type);
  421. this._connection.send(data);
  422. return this;
  423. };
  424. /**
  425. * Set the API URL.
  426. *
  427. * @param string url
  428. */
  429. this.setApi=function(url) {
  430. this._api=url;
  431. return this;
  432. }
  433. /**
  434. * Set the request content type.
  435. *
  436. * @param string contentType
  437. * @return ChangeOrgApiConnection
  438. */
  439. this.setContentType=function(contentType) {
  440. this._data.content_type=contentType;
  441. return this;
  442. };
  443. /**
  444. * Set the request endpoint.
  445. *
  446. * @param string endpoint
  447. * @param object params
  448. * @return ChangeOrgApiConnection
  449. */
  450. this.setEndpoint=function(endpoint,params) {
  451. this._data.endpoint=endpoint;
  452. if(typeof params=='object')
  453. this._params=params;
  454. return this;
  455. };
  456. /**
  457. * Set the request method.
  458. *
  459. * @param string method
  460. * @return ChangeOrgApiConnection
  461. */
  462. this.setMethod=function(method) {
  463. this._data.method=method.toUpperCase();
  464. return this;
  465. };
  466. /**
  467. * Set the request success handler.
  468. *
  469. * @param object callback
  470. * @return ChangeOrgApiConnection
  471. */
  472. this.setOnSuccess=function(callback) {
  473. if(typeof callback=='function')
  474. this._data.onSuccess=callback;
  475. return this;
  476. };
  477. /**
  478. * Set the request URL parameters.
  479. *
  480. * @param object params
  481. * @return ChangeOrgApiConnection
  482. */
  483. this.setParams=function(params) {
  484. this._params=params;
  485. return this;
  486. };
  487. /**
  488. * Initialization
  489. ******************/
  490. this._data=this._getDefaults();
  491. if(typeof options=='object') {
  492. for(var key in options) {
  493. if(typeof this._data[key]!='undefined')
  494. this._data[key]=options[key];
  495. }
  496. }
  497. if(ChangeOrgApiUtils.proxy)
  498. this._proxy=ChangeOrgApiUtils.proxy;
  499. };/**
  500. * ChangeOrgApiRequest
  501. *
  502. * A standardized request object for interacting with the Change.org API.
  503. *
  504. * @package ChangeOrgApi
  505. * @author Rick Buczynski <richard.buczynski@gmail.com>
  506. */
  507. var ChangeOrgApiRequest=function(client) {
  508. /* @var _authKey string */
  509. this._authKey=null;
  510. /* @var _connection ChangeOrgApiConnection */
  511. this._connection=null;
  512. this._data={};
  513. this._useSignature=true;
  514. this._useAuthKeyInSignature=true;
  515. /**
  516. * Get default options.
  517. *
  518. * @return object
  519. */
  520. this._getDefaults=function() {
  521. return {
  522. api_key : '',
  523. endpoint : '',
  524. timestamp : ''
  525. };
  526. };
  527. /**
  528. * Add client information to the request.
  529. *
  530. * @return ChangeOrgApiRequest
  531. */
  532. this.addClient=function() {
  533. this._data.api_key=this._client.getApiKey();
  534. return this;
  535. };
  536. /**
  537. * Add bulk data to the request. Overwrites existing data.
  538. *
  539. * @param object data
  540. */
  541. this.addData=function(data) {
  542. if(typeof this._data=='object') {
  543. for(var key in data) {
  544. if(key=='auth_key')
  545. this.setAuthKey(data[key]);
  546. else
  547. this._data[key]=data[key];
  548. }
  549. }
  550. return this;
  551. };
  552. /**
  553. * Sign the request and add the signature as a parameter.
  554. *
  555. * @see https://github.com/change/api_docs/blob/master/v1/documentation/requests.md#request-signature
  556. *
  557. * @return ChangeOrgApiRequest
  558. */
  559. this.addSignature=function() {
  560. try {
  561. if(!this.getClient().getSecret() && this.getSignatureRequiredFlag()) { // Mark signature with flag to sign server-side
  562. this._data.server_sign=1;
  563. this._data.include_auth_key=this.getSignatureAuthKeyRequiredFlag()?1:0;
  564. if(this._data.include_auth_key)
  565. this._data.auth_key=this._authKey;
  566. }
  567. else if(typeof this._data.rsig=='undefined' || !this._data.rsig.length) {
  568. var body=[], signature='';
  569. for(var key in this._data) {
  570. if(key!='rsig')
  571. body.push(key+'='+encodeURIComponent(this._data[key]));
  572. }
  573. this._data.rsig=
  574. CryptoJS.SHA256(
  575. body.join('&')
  576. +this.getClient().getSecret()
  577. +(this._useAuthKeyInSignature===true?this._authKey:'')
  578. );
  579. }
  580. }
  581. catch(error) { }
  582. return this;
  583. };
  584. /**
  585. * Add the timestamp to the request.
  586. *
  587. * @return ChangeOrgApiRequest
  588. */
  589. this.addTimestamp=function() {
  590. this._data.timestamp=this.newTimestamp();
  591. return this;
  592. };
  593. /**
  594. * Build the request body.
  595. *
  596. * @return string
  597. */
  598. this.buildRequest=function() {
  599. this.addClient();
  600. this.addTimestamp();
  601. if(this._useSignature===true)
  602. this.addSignature();
  603. else if(typeof this._data.rsig!='undefined')
  604. delete this._data.rsig;
  605. var body=[];
  606. for(var key in this._data)
  607. body.push(key+'='+encodeURIComponent(this._data[key]));
  608. return body.join('&');
  609. };
  610. /**
  611. * Get the stored authorization key.
  612. *
  613. * @return string|null
  614. */
  615. this.getAuthKey=function() {
  616. return this._authKey;
  617. };
  618. /**
  619. * Get the stored client instance.
  620. *
  621. * @return ChangeOrgApiClient
  622. */
  623. this.getClient=function() {
  624. return this._client;
  625. };
  626. /**
  627. * Get the resource connection.
  628. *
  629. * @param object options
  630. * @return ChangeOrgApiConnection
  631. */
  632. this.getConnection=function(options) {
  633. if(!this._connection)
  634. this._connection=new ChangeOrgApiConnection(options);
  635. return this._connection;
  636. };
  637. /**
  638. * Get request data.
  639. *
  640. * @param string key
  641. * @return mixed
  642. */
  643. this.getData=function(key) {
  644. if(!key)
  645. return this._data;
  646. if(typeof this._data[key]!='undefined')
  647. return this._data[key];
  648. return null;
  649. };
  650. /**
  651. * Get the request endpoint.
  652. *
  653. * @return string
  654. */
  655. this.getEndpoint=function() {
  656. return this._data.endpoint;
  657. };
  658. /**
  659. * Get the request connection method.
  660. *
  661. * @return string
  662. */
  663. this.getMethod=function() {
  664. return this.getConnection().getMethod();
  665. };
  666. /**
  667. * Get the authorization key signature flag.
  668. *
  669. * @return boolean
  670. */
  671. this.getSignatureAuthKeyRequiredFlag=function() {
  672. return this._useAuthKeyInSignature;
  673. };
  674. /**
  675. * Get the signature flag.
  676. *
  677. * @return boolean
  678. */
  679. this.getSignatureRequiredFlag=function() {
  680. return this._useSignature;
  681. };
  682. /**
  683. * Get the request timestamp.
  684. *
  685. * @return string
  686. */
  687. this.getTimestamp=function() {
  688. return this._data.timestamp;
  689. };
  690. /**
  691. * Generate a new ISO-8601 timestamp.
  692. *
  693. * @return string
  694. */
  695. this.newTimestamp=function() {
  696. return (new Date()).toISOString().replace(/(\.[0-9]+)/,'');
  697. };
  698. /**
  699. * Reset the connection object.
  700. *
  701. * @return ChangeOrgApiRequest
  702. */
  703. this.resetConnection=function() {
  704. this._connection=null;
  705. return this;
  706. };
  707. /**
  708. * Remove the signature from the request.
  709. *
  710. * @return ChangeOrgApiRequest
  711. */
  712. this.removeSignature=function() {
  713. if(typeof this._data.rsig!='undefined')
  714. delete this._data.rsig;
  715. return this;
  716. };
  717. /**
  718. * Submit the request.
  719. *
  720. * @return ChangeOrgApiRequest
  721. */
  722. this.send=function() {
  723. var connection=this.getConnection();
  724. connection.setEndpoint(this._data.endpoint);
  725. if(this._doFollowup===true)
  726. this._setFollowupCallback();
  727. this.getConnection().send(this.buildRequest());
  728. return this;
  729. };
  730. /**
  731. * Set the authorization key.
  732. *
  733. * @param string authKey
  734. * @return ChangeOrgApiRequest
  735. */
  736. this.setAuthKey=function(authKey) {
  737. this._authKey=authKey;
  738. return this;
  739. };
  740. /**
  741. * Set the client.
  742. *
  743. * @param ChangeOrgApiClient client
  744. */
  745. this.setClient=function(client) {
  746. if(!(client instanceof ChangeOrgApiClient))
  747. throw new ChangeOrgApiException('Client must be an instance of ChangeOrgApiClient.');
  748. this._client=client;
  749. return this;
  750. };
  751. /**
  752. * Set request data.
  753. *
  754. * @param string key
  755. * @param mixed value
  756. */
  757. this.setData=function(key,value) {
  758. this._data[key]=value;
  759. return this;
  760. };
  761. /**
  762. * Set the request endpoint.
  763. *
  764. * @param string endpoint
  765. */
  766. this.setEndpoint=function(endpoint) {
  767. this._data.endpoint=endpoint;
  768. return this;
  769. };
  770. /**
  771. * Set the request connection method.
  772. *
  773. * @param string method
  774. * @return ChangeOrgApiRequest
  775. */
  776. this.setMethod=function(method) {
  777. this.getConnection().setMethod(method);
  778. return this;
  779. };
  780. /**
  781. * Set the request success handler.
  782. *
  783. * @param function handler
  784. * @return ChangeOrgApiRequest
  785. */
  786. this.setOnSuccess=function(handler) {
  787. this.getConnection().setOnSuccess(handler);
  788. return this;
  789. };
  790. /**
  791. * Set the authorization key signature flag.
  792. *
  793. * @param boolean bool
  794. * @return ChangeOrgApiRequest
  795. */
  796. this.setSignatureAuthKeyRequiredFlag=function(bool) {
  797. this._useAuthKeyInSignature=bool;
  798. return this;
  799. };
  800. /**
  801. * Set the signature flag.
  802. *
  803. * @param boolean bool
  804. * @return ChangeOrgApiRequest
  805. */
  806. this.setSignatureRequiredFlag=function(bool) {
  807. this._useSignature=bool;
  808. return this;
  809. };
  810. /**
  811. * Set the request timestamp.
  812. *
  813. * @param string timestamp
  814. * @return ChangeOrgApiRequest
  815. */
  816. this.setTimestamp=function(timestamp) {
  817. this._data.timestamp=timestamp;
  818. return this;
  819. };
  820. /**
  821. * Unset request data
  822. * @param string key
  823. * @return ChangeOrgApiRequest
  824. */
  825. this.unsetData=function(key) {
  826. if(typeof this._data[key]!='undefined')
  827. delete this._data[key];
  828. return this;
  829. };
  830. /**
  831. * Initialization
  832. ******************/
  833. if(typeof CryptoJS=='undefined')
  834. throw new ChangeOrgApiException('ChangeOrgApiRequest cannot initialize because the required library CryptoJS was not found.');
  835. if(client)
  836. this.setClient(client);
  837. this._data=this._getDefaults();
  838. };/**
  839. * ChangeOrgApiResponse
  840. *
  841. * A request response wrapper object.
  842. *
  843. * @package ChangeOrgApi
  844. * @author Rick Buczynski <richard.buczynski@gmail.com>
  845. */
  846. var ChangeOrgApiResponse=function(data) {
  847. this._data={};
  848. /**
  849. * Get response data.
  850. *
  851. * @param string key
  852. * @return mixed
  853. */
  854. this.getData=function(key) {
  855. if(!key)
  856. return this._data;
  857. if(typeof this._data[key]!='undefined')
  858. return this._data[key];
  859. return null;
  860. };
  861. /**
  862. * Set the response data.
  863. *
  864. * @param object data
  865. * @return ChangeOrgApiResponse
  866. */
  867. this.setData=function(data) {
  868. try {
  869. if(data instanceof ChangeOrgApiConnection)
  870. this._data=data.getResponse();
  871. else if(typeof data=='string')
  872. this._data=JSON.parse(data);
  873. else if(typeof data=='object')
  874. this._data=data;
  875. else
  876. throw new ChangeOrgApiException('Data format not recognized.');
  877. }
  878. catch(error) {
  879. this._data={};
  880. }
  881. return this;
  882. }
  883. /**
  884. * Initialization
  885. ******************/
  886. this.setData(data);
  887. };/**
  888. * ChangeOrgApiPetition Interface
  889. *
  890. * Provides an interface for petitions on Change.org.
  891. *
  892. * @see https://github.com/change/api_docs/blob/master/v1/documentation/resources/petitions.md
  893. *
  894. * @package ChangeOrgApi
  895. * @author Rick Buczynski <richard.buczynski@gmail.com>
  896. */
  897. var ChangeOrgApiPetition=function(client) {
  898. /* @var _authorization ChangeOrgApiAuthorization */
  899. this._authorization=null;
  900. /* @var _callback function */
  901. this._callback=null;
  902. /* @var _client ChangeOrgApiClient */
  903. this._client=null;
  904. this._endpoints=[
  905. '/v1/petitions/:petition_id/signatures', // addSignature
  906. '/v1/petitions/:petition_id/signatures', // getSignatures
  907. '/v1/petitions/:petition_id/signatures/recent', // getRecentSignatures
  908. '/v1/petitions/:petition_id/targets', // getTargets
  909. '/v1/petitions/:petition_id/reasons', // getReasons
  910. '/v1/petitions/:petition_id/updates', // getUpdates
  911. '/v1/petitions', // getPetitions
  912. '/v1/petitions/:petition_id', // get
  913. '/v1/petitions/get_id' // getId
  914. ];
  915. /**
  916. * Get default request options.
  917. *
  918. * @todo Future use.
  919. *
  920. * @return object
  921. */
  922. this._getDefaults=function() {
  923. return {};
  924. };
  925. /**
  926. * Adds a signature to a petition.
  927. *
  928. * @see https://github.com/change/api_docs/blob/master/v1/documentation/resources/petitions/signatures.md#post-signatures
  929. *
  930. * @param object data
  931. * @param function callback
  932. * @return ChangeOrgApiPetition
  933. */
  934. this.addSignature=function(data,callback) {
  935. if(typeof data.auth_key=='undefined')
  936. throw new ChangeOrgApiException('An authorization key is required to sign a petition.');
  937. var request=new ChangeOrgApiRequest(this.getClient())
  938. .setMethod('POST')
  939. .addData(data)
  940. .setEndpoint(this.getEndpoint(0,data))
  941. ;
  942. if(callback)
  943. request.setOnSuccess(callback);
  944. else if(this._callback)
  945. request.setOnSuccess(this._callback);
  946. request.send();
  947. return this;
  948. };
  949. /**
  950. * Get a petition authorization instance.
  951. *
  952. * @see https://github.com/change/api_docs/blob/master/v1/documentation/resources/petitions/auth_keys.md
  953. *
  954. * @return ChangeOrgApiPetitionAuthorization
  955. */
  956. this.getAuthorization=function() {
  957. if(!this._authorization) {
  958. this._authorization=new ChangeOrgApiPetitionAuthorization(this._client);
  959. }
  960. return this._authorization;
  961. };
  962. /**
  963. * Get the stored client instance.
  964. *
  965. * @return ChangeOrgApiClient
  966. */
  967. this.getClient=function() {
  968. return this._client;
  969. };
  970. /**
  971. * Get the prepared endpoint for a requested resource.
  972. *
  973. * @param number index
  974. * @param mixed data
  975. * @return string
  976. */
  977. this.getEndpoint=function(index,data) {
  978. if(typeof this._endpoints[index]=='undefined')
  979. return false;
  980. return ChangeOrgApiUtils.bind(this._endpoints[index],data);
  981. };
  982. /**
  983. * Returns information about the specified petition.
  984. *
  985. * @see https://github.com/change/api_docs/blob/master/v1/documentation/resources/petitions.md#get-petitionspetition_id
  986. *
  987. * @param number|object data
  988. * @param function callback
  989. * @return ChangeOrgApiPetition
  990. */
  991. this.get=function(data,callback) {
  992. if(typeof data=='number' || typeof data=='string')
  993. data={petition_id:data};
  994. var request=new ChangeOrgApiRequest(this.getClient())
  995. .setMethod('GET')
  996. .addData(data)
  997. .setEndpoint(this.getEndpoint(7,data))
  998. .setSignatureRequiredFlag(false)
  999. ;
  1000. if(callback)
  1001. request.setOnSuccess(callback);
  1002. else if(this._callback)
  1003. request.setOnSuccess(this._callback);
  1004. request.send();
  1005. return this;
  1006. };
  1007. /**
  1008. * Returns the unique Change.org ID for the specified petition.
  1009. *
  1010. * @see https://github.com/change/api_docs/blob/master/v1/documentation/resources/petitions.md#get-petitionsget_id
  1011. *
  1012. * @param string|object data
  1013. * @param function callback
  1014. * @return ChangeOrgApiPetition
  1015. */
  1016. this.getId=function(data,callback) {
  1017. if(typeof data=='string')
  1018. data={petition_url:data};
  1019. var request=new ChangeOrgApiRequest(this.getClient())
  1020. .setMethod('GET')
  1021. .addData(data)
  1022. .setEndpoint(this.getEndpoint(8,data))
  1023. .setSignatureRequiredFlag(false)
  1024. ;
  1025. if(callback)
  1026. request.setOnSuccess(callback);
  1027. else if(this._callback)
  1028. request.setOnSuccess(this._callback);
  1029. request.send();
  1030. return this;
  1031. };
  1032. /**
  1033. * Returns the array of petition data objects corresponding to the petition IDs submitted.
  1034. *
  1035. * @see https://github.com/change/api_docs/blob/master/v1/documentation/resources/petitions.md#get-petitions
  1036. *
  1037. * @param string|object data
  1038. * @param function callback
  1039. * @return ChangeOrgApiPetition
  1040. */
  1041. this.getPetitions=function(data,callback) {
  1042. if(typeof data=='number')
  1043. return this.getPetition(data,callback);
  1044. if(typeof data=='string')
  1045. data={petition_ids:data};
  1046. var request=new ChangeOrgApiRequest(this.getClient())
  1047. .setMethod('GET')
  1048. .addData(data)
  1049. .setEndpoint(this.getEndpoint(6,data))
  1050. .setSignatureRequiredFlag(false)
  1051. ;
  1052. if(callback)
  1053. request.setOnSuccess(callback);
  1054. else if(this._callback)
  1055. request.setOnSuccess(this._callback);
  1056. request.send();
  1057. return this;
  1058. };
  1059. /**
  1060. * Returns an array of the 10 most recent signatures on a petition.
  1061. *
  1062. * @see https://github.com/change/api_docs/blob/master/v1/documentation/resources/petitions/signatures.md#get-signatures-recent
  1063. *
  1064. * @param object data
  1065. * @param function callback
  1066. * @return ChangeOrgApiPetition
  1067. */
  1068. this.getRecentSignatures=function(data,callback) {
  1069. if(typeof data=='number' || typeof data=='string')
  1070. data={petition_id:data};
  1071. var request=new ChangeOrgApiRequest(this.getClient())
  1072. .setMethod('GET')
  1073. .addData(data)
  1074. .setEndpoint(this.getEndpoint(2,data))
  1075. .setSignatureRequiredFlag(false)
  1076. ;
  1077. if(callback)
  1078. request.setOnSuccess(callback);
  1079. else if(this._callback)
  1080. request.setOnSuccess(this._callback);
  1081. request.send();
  1082. };
  1083. /**
  1084. * Returns signatures on a petition.
  1085. *
  1086. * @see https://github.com/change/api_docs/blob/master/v1/documentation/resources/petitions/signatures.md#signatures-on-petitions
  1087. *
  1088. * @param object data
  1089. * @param function callback
  1090. * @return ChangeOrgApiPetition
  1091. */
  1092. this.getSignatures=function(data,callback) {
  1093. if(typeof data=='number' || typeof data=='string')
  1094. data={petition_id:data};
  1095. var request=new ChangeOrgApiRequest(this.getClient())
  1096. .setMethod('GET')
  1097. .addData(data)
  1098. .setEndpoint(this.getEndpoint(1,data))
  1099. .setSignatureRequiredFlag(false)
  1100. ;
  1101. if(callback)
  1102. request.setOnSuccess(callback);
  1103. else if(this._callback)
  1104. request.setOnSuccess(this._callback);
  1105. request.send();
  1106. return this;
  1107. };
  1108. /**
  1109. * Returns the reasons given by signers of a petition for having signed.
  1110. *
  1111. * @see https://github.com/change/api_docs/blob/master/v1/documentation/resources/petitions/reasons.md
  1112. *
  1113. * @param object data
  1114. * @param function callback
  1115. * @return ChangeOrgApiPetition
  1116. */
  1117. this.getReasons=function(data,callback) {
  1118. if(typeof data=='number' || typeof data=='string')
  1119. data={petition_id:data};
  1120. var request=new ChangeOrgApiRequest(this.getClient())
  1121. .setMethod('GET')
  1122. .addData(data)
  1123. .setEndpoint(this.getEndpoint(4,data))
  1124. .setSignatureRequiredFlag(false)
  1125. ;
  1126. if(callback)
  1127. request.setOnSuccess(callback);
  1128. else if(this._callback)
  1129. request.setOnSuccess(this._callback);
  1130. request.send();
  1131. return this;
  1132. };
  1133. /**
  1134. * Returns the target(s) of a petition.
  1135. *
  1136. * @see https://github.com/change/api_docs/blob/master/v1/documentation/resources/petitions/targets.md
  1137. *
  1138. * @param object data
  1139. * @param function callback
  1140. * @return ChangeOrgApiPetition
  1141. */
  1142. this.getTargets=function(data,callback) {
  1143. if(typeof data=='number' || typeof data=='string')
  1144. data={petition_id:data};
  1145. var request=new ChangeOrgApiRequest(this.getClient())
  1146. .setMethod('GET')
  1147. .addData(data)
  1148. .setEndpoint(this.getEndpoint(3,data))
  1149. .setSignatureRequiredFlag(false)
  1150. ;
  1151. if(callback)
  1152. request.setOnSuccess(callback);
  1153. else if(this._callback)
  1154. request.setOnSuccess(this._callback);
  1155. request.send();
  1156. return this;
  1157. };
  1158. /**
  1159. * Returns the news updates on a petition.
  1160. *
  1161. * @see https://github.com/change/api_docs/blob/master/v1/documentation/resources/petitions/updates.md
  1162. *
  1163. * @param object data
  1164. * @param function callback
  1165. * @return ChangeOrgApiPetition
  1166. */
  1167. this.getUpdates=function(data,callback) {
  1168. if(typeof data=='number' || typeof data=='string')
  1169. data={petition_id:data};
  1170. var request=new ChangeOrgApiRequest(this.getClient())
  1171. .setMethod('GET')
  1172. .addData(data)
  1173. .setEndpoint(this.getEndpoint(5,data))
  1174. .setSignatureRequiredFlag(false)
  1175. ;
  1176. if(callback)
  1177. request.setOnSuccess(callback);
  1178. else if(this._callback)
  1179. request.setOnSuccess(this._callback);
  1180. request.send();
  1181. };
  1182. /**
  1183. * Set the request callback.
  1184. *
  1185. * @param function callback
  1186. * @return ChangeOrgApiPetition
  1187. */
  1188. this.setCallback=function(callback) {
  1189. this._callback=callback;
  1190. return this;
  1191. };
  1192. /**
  1193. * Set the client.
  1194. *
  1195. * @param ChangeOrgApiClient client
  1196. * @return ChangeOrgApiUser
  1197. */
  1198. this.setClient=function(client) {
  1199. if(!(client instanceof ChangeOrgApiClient))
  1200. throw new ChangeOrgApiException('Client must be an instance of ChangeOrgApiClient.');
  1201. this._client=client;
  1202. return this;
  1203. };
  1204. /**
  1205. * Initialization
  1206. ******************/
  1207. if(client)
  1208. this.setClient(client);
  1209. };/**
  1210. * ChangeOrgApiPetitionAuthorization Interface
  1211. *
  1212. * Provides an interface for petition authorization on Change.org.
  1213. *
  1214. * @package ChangeOrgApi
  1215. * @author Rick Buczynski <richard.buczynski@gmail.com>
  1216. */
  1217. var ChangeOrgApiPetitionAuthorization=function(client) {
  1218. /* @var _authorizationCallback function */
  1219. this._authorizationCallback=ChangeOrgApiUtils.lambda;
  1220. /* @var _client ChangeOrgApiClient */
  1221. this._client=null;
  1222. /* @var _client ChangeOrgApiConnection */
  1223. this._connection=null;
  1224. this._data={};
  1225. this._doFollowup=false;
  1226. this._endpoint='/v1/petitions/:petition_id/auth_keys';
  1227. this._petition_id='';
  1228. /**
  1229. * Resolve the authorization callback before sending the request.
  1230. *
  1231. * @return function
  1232. */
  1233. this._getAuthorizationCallback=function() {
  1234. if(this._doFollowup===true)
  1235. return this._getFollowupCallback(this._authorizationCallback);
  1236. return this._authorizationCallback;
  1237. };
  1238. /**
  1239. * Get default request options.
  1240. *
  1241. * @return object
  1242. */
  1243. this._getDefaults=function() {
  1244. return {
  1245. callback_endpoint : '',
  1246. requester_email : '',
  1247. source : '',
  1248. source_description : ''
  1249. };
  1250. };
  1251. /**
  1252. * Get the follow-up callback wrapper.
  1253. *
  1254. * @param function originalCallback
  1255. * @return function
  1256. */
  1257. this._getFollowupCallback=function(originalCallback) {
  1258. var self=this;
  1259. return function(response) {
  1260. self.setFollowupFlag(false);
  1261. if(response.getData('status')!='granted' || !response.getData('auth_key'))
  1262. self.authorize(originalCallback);
  1263. else
  1264. originalCallback.call(originalCallback,response);
  1265. };
  1266. };
  1267. /**
  1268. * Sends a request for a petition authorization key on behalf of an individual.
  1269. *
  1270. * @see https://github.com/change/api_docs/blob/master/v1/documentation/resources/petitions/auth_keys.md#post-petitionspetition_idauth_keys
  1271. *
  1272. * @param function callback
  1273. * @return ChangeOrgApiPetitionAuthorization
  1274. */
  1275. this.authorize=function(callback) {
  1276. this.setCallback(callback);
  1277. var request=new ChangeOrgApiRequest(this._client);
  1278. request.addData(this._data)
  1279. .setMethod('POST')
  1280. .setEndpoint(ChangeOrgApiUtils.bind(this._endpoint,this._petition_id))
  1281. .setSignatureAuthKeyRequiredFlag(false)
  1282. .setOnSuccess(this._getAuthorizationCallback())
  1283. ;
  1284. request.send();
  1285. return this;
  1286. };
  1287. /**
  1288. * Get the request callback endpoint.
  1289. *
  1290. * @return string
  1291. */
  1292. this.getCallbackEndpoint=function() {
  1293. return this._data.callback_endpoint;
  1294. };
  1295. /**
  1296. * Get the stored client instance.
  1297. *
  1298. * @return ChangeOrgApiClient
  1299. */
  1300. this.getClient=function() {
  1301. return this._client;
  1302. };
  1303. /**
  1304. * Get the request endpoint.
  1305. *
  1306. * @param object data
  1307. * @return string
  1308. */
  1309. this.getEndpoint=function(data) {
  1310. return ChangeOrgApiUtils.bind(this._endpoint,data);
  1311. };
  1312. /**
  1313. * Get the request follow-up flag.
  1314. *
  1315. * @return boolean
  1316. */
  1317. this.getFollowupFlag=function() {
  1318. return this._doFollowup;
  1319. }
  1320. /**
  1321. * Get the requester.
  1322. *
  1323. * @return string
  1324. */
  1325. this.getRequesterEmail=function() {
  1326. return this._data.requester_email;
  1327. };
  1328. /**
  1329. * Get the request source.
  1330. *
  1331. * @return string
  1332. */
  1333. this.getSource=function() {
  1334. return this._data.source;
  1335. };
  1336. /**
  1337. * Get the request source description.
  1338. *
  1339. * @return string
  1340. */
  1341. this.getSourceDescription=function() {
  1342. return this._data.source_description;
  1343. };
  1344. /**
  1345. * Set the request callback.
  1346. *
  1347. * @param function callback
  1348. * @return ChangeOrgApiPetitionAuthorization
  1349. */
  1350. this.setCallback=function(callback) {
  1351. if(typeof callback=='function')
  1352. this._authorizationCallback=callback;
  1353. return this;
  1354. };
  1355. /**
  1356. * Set the request callback endpoint.
  1357. *
  1358. * @param string endpoint
  1359. * @return ChangeOrgApiPetitionAuthorization
  1360. */
  1361. this.setCallbackEndpoint=function(endpoint) {
  1362. this._data.callback_endpoint=endpoint;
  1363. return this;
  1364. };
  1365. /**
  1366. * Set the client.
  1367. *
  1368. * @param ChangeOrgApiClient
  1369. * @return ChangeOrgApiPetitionAuthorization
  1370. */
  1371. this.setClient=function(client) {
  1372. if(!(client instanceof ChangeOrgApiClient))
  1373. throw new ChangeOrgApiException('Client must be an instance of ChangeOrgApiClient.');
  1374. this._client=client;
  1375. return this;
  1376. };
  1377. /**
  1378. * Set the request follow-up flag.
  1379. *
  1380. * @param boolean bool
  1381. * @return ChangeOrgApiRequest
  1382. */
  1383. this.setFollowupFlag=function(bool) {
  1384. this._doFollowup=bool;
  1385. return this;
  1386. };
  1387. /**
  1388. * Set the petition ID.
  1389. *
  1390. * @param number id
  1391. * @return ChangeOrgApiPetitionAuthorization
  1392. */
  1393. this.setPetitionId=function(id) {
  1394. this._petition_id=id;
  1395. return this;
  1396. };
  1397. /**
  1398. * Set the requester.
  1399. *
  1400. * @param string email.
  1401. * @return ChangeOrgApiPetitionAuthorization
  1402. */
  1403. this.setRequesterEmail=function(email) {
  1404. this._data.requester_email=email;
  1405. return this;
  1406. };
  1407. /**
  1408. * Set the request source.
  1409. *
  1410. * @param string source
  1411. * @return ChangeOrgApiPetitionAuthorization
  1412. */
  1413. this.setSource=function(source) {
  1414. this._data.source=source;
  1415. return this;
  1416. };
  1417. /**
  1418. * Set the request source description.
  1419. *
  1420. * @param string description
  1421. * @return ChangeOrgApiPetitionAuthorization
  1422. */
  1423. this.setSourceDescription=function(description) {
  1424. this._data.source_description=description;
  1425. return this;
  1426. };
  1427. /**
  1428. * Initialization
  1429. ******************/
  1430. this._data=this._getDefaults();
  1431. if(client)
  1432. this.setClient(client);
  1433. };/**
  1434. * ChangeOrgApiOrganization Interface
  1435. *
  1436. * Provides an interface for organizations on Change.org.
  1437. *
  1438. * @see https://github.com/change/api_docs/blob/master/v1/documentation/resources/organizations.md
  1439. *
  1440. * @package ChangeOrgApi
  1441. * @author Rick Buczynski <richard.buczynski@gmail.com>
  1442. */
  1443. var ChangeOrgApiOrganization=function(client) {
  1444. /* @var _callback function */
  1445. this._callback=null;
  1446. /* @var _client ChangeOrgApiClient */
  1447. this._client=null;
  1448. this._endpoints=[
  1449. '/v1/organizations/:organization_id', // get
  1450. '/v1/organizations/:organization_id/petitions', // getPetitions
  1451. '/v1/organizations/get_id' // getId
  1452. ];
  1453. /**
  1454. * Get default request options.
  1455. *
  1456. * @todo Future use.
  1457. *
  1458. * @return object
  1459. */
  1460. this._getDefaults=function() {
  1461. return {};
  1462. };
  1463. /**
  1464. * Get the stored client instance.
  1465. *
  1466. * @return ChangeOrgApiClient
  1467. */
  1468. this.getClient=function() {
  1469. return this._client;
  1470. };
  1471. /**
  1472. * Get the prepared endpoint for a requested resource.
  1473. *
  1474. * @param number index
  1475. * @param mixed data
  1476. * @return string
  1477. */
  1478. this.getEndpoint=function(index,data) {
  1479. if(typeof this._endpoints[index]=='undefined')
  1480. return false;
  1481. return ChangeOrgApiUtils.bind(this._endpoints[index],data);
  1482. };
  1483. /**
  1484. * Returns information about the specified organization.
  1485. *
  1486. * @see https://github.com/change/api_docs/blob/master/v1/documentation/resources/organizations.md#get-organizationsorganization_id
  1487. *
  1488. * @param number|object data
  1489. * @param function callback
  1490. * @return ChangeOrgApiOrganization
  1491. */
  1492. this.get=function(data,callback) {
  1493. if(typeof data=='number')
  1494. data={organization_id:data};
  1495. var request=new ChangeOrgApiRequest(this.getClient())
  1496. .setMethod('GET')
  1497. .addData(data)
  1498. .setEndpoint(this.getEndpoint(0,data))
  1499. .setSignatureRequiredFlag(false)
  1500. ;
  1501. if(callback)
  1502. request.setOnSuccess(callback);
  1503. else if(this._callback)
  1504. request.setOnSuccess(this._callback);
  1505. request.send();
  1506. return this;
  1507. };
  1508. /**
  1509. * Returns the unique Change.org ID for the specified organization.
  1510. *
  1511. * @see https://github.com/change/api_docs/blob/master/v1/documentation/resources/organizations.md#get-organizationsget_id
  1512. *
  1513. * @param string|object data
  1514. * @param function callback
  1515. * @return ChangeOrgApiOrganization
  1516. */
  1517. this.getId=function(data,callback) {
  1518. if(typeof data=='string')
  1519. data={organization_url:data};
  1520. var request=new ChangeOrgApiRequest(this.getClient())
  1521. .setMethod('GET')
  1522. .addData(data)
  1523. .setEndpoint(this.getEndpoint(2,data))
  1524. .setSignatureRequiredFlag(false)
  1525. ;
  1526. if(callback)
  1527. request.setOnSuccess(callback);
  1528. else if(this._callback)
  1529. request.setOnSuccess(this._callback);
  1530. request.send();
  1531. return this;
  1532. };
  1533. /**
  1534. * Returns the array of petitions that were created by the specified organization.
  1535. *
  1536. * @see https://github.com/change/api_docs/blob/master/v1/documentation/resources/organizations.md#get-organizationsorganization_idpetitions
  1537. *
  1538. * @param number|object data
  1539. * @param function callback
  1540. * @return ChangeOrgApiOrganization
  1541. */
  1542. this.getPetitions=function(data,callback) {
  1543. if(typeof data=='number')
  1544. data={organization_id:data};
  1545. var request=new ChangeOrgApiRequest(this.getClient())
  1546. .setMethod('GET')
  1547. .addData(data)
  1548. .setEndpoint(this.getEndpoint(1,data))
  1549. .setSignatureRequiredFlag(false)
  1550. ;
  1551. if(callback)
  1552. request.setOnSuccess(callback);
  1553. else if(this._callback)
  1554. request.setOnSuccess(this._callback);
  1555. request.send();
  1556. };
  1557. /**
  1558. * Set the request callback.
  1559. *
  1560. * @param function callback
  1561. * @return ChangeOrgApiOrganization
  1562. */
  1563. this.setCallback=function(callback) {
  1564. this._callback=callback;
  1565. return this;
  1566. };
  1567. /**
  1568. * Set the client.
  1569. *
  1570. * @param ChangeOrgApiClient client
  1571. * @return ChangeOrgApiOrganization
  1572. */
  1573. this.setClient=function(client) {
  1574. if(!(client instanceof ChangeOrgApiClient))
  1575. throw new ChangeOrgApiException('Client must be an instance of ChangeOrgApiClient.');
  1576. this._client=client;
  1577. return this;
  1578. };
  1579. /**
  1580. * Initialization
  1581. ******************/
  1582. if(client)
  1583. this.setClient(client);
  1584. };/**
  1585. * ChangeOrgApiUser Interface
  1586. *
  1587. * Provides an interface for users on Change.org.
  1588. *
  1589. * @see https://github.com/change/api_docs/blob/master/v1/documentation/resources/users.md
  1590. *
  1591. * @package ChangeOrgApi
  1592. * @author Rick Buczynski <richard.buczynski@gmail.com>
  1593. */
  1594. var ChangeOrgApiUser=function(client) {
  1595. /* @var _callback function */
  1596. this._callback=null;
  1597. /* @var _client ChangeOrgApiClient */
  1598. this._client=null;
  1599. this._endpoints=[
  1600. '/v1/users/:user_id', // get
  1601. '/v1/users/:user_id/petitions', // getPetitions
  1602. '/v1/users/:user_id/signatures/petitions', // getSignedPetitions
  1603. '/v1/users/get_id' // getId
  1604. ];
  1605. /**
  1606. * Get default request options.
  1607. *
  1608. * @todo Future use.
  1609. *
  1610. * @return object
  1611. */
  1612. this._getDefaults=function() {
  1613. return {};
  1614. };
  1615. /**
  1616. * Get the stored client instance.
  1617. *
  1618. * @return ChangeOrgApiClient
  1619. */
  1620. this.getClient=function() {
  1621. return this._client;
  1622. };
  1623. /**
  1624. * Get the prepared endpoint for a requested resource.
  1625. *
  1626. * @param number index
  1627. * @param mixed data
  1628. * @return string
  1629. */
  1630. this.getEndpoint=function(index,data) {
  1631. if(typeof this._endpoints[index]=='undefined')
  1632. return false;
  1633. return ChangeOrgApiUtils.bind(this._endpoints[index],data);
  1634. };
  1635. /**
  1636. * Returns information about the specified user.
  1637. *
  1638. * @see https://github.com/change/api_docs/blob/master/v1/documentation/resources/users.md#get-usersuser_id
  1639. *
  1640. * @param number|object data
  1641. * @param function callback
  1642. * @return ChangeOrgApiUser
  1643. */
  1644. this.get=function(data,callback) {
  1645. if(typeof data=='number')
  1646. data={user_id:data};
  1647. var request=new ChangeOrgApiRequest(this.getClient())
  1648. .setMethod('GET')
  1649. .addData(data)
  1650. .setEndpoint(this.getEndpoint(0,data))
  1651. .setSignatureRequiredFlag(false)
  1652. ;
  1653. if(callback)
  1654. request.setOnSuccess(callback);
  1655. else if(this._callback)
  1656. request.setOnSuccess(this._callback);
  1657. request.send();
  1658. };
  1659. /**
  1660. * Returns the unique Change.org ID for the specified user.
  1661. *
  1662. * @see https://github.com/change/api_docs/blob/master/v1/documentation/resources/users.md#get-usersget_id
  1663. *
  1664. * @param string|object data
  1665. * @param function callback
  1666. * @return ChangeOrgApiUser
  1667. */
  1668. this.getId=function(data,callback) {
  1669. if(typeof data=='string')
  1670. data={user_url:data};
  1671. var request=new ChangeOrgApiRequest(this.getClient())
  1672. .setMethod('GET')
  1673. .addData(data)
  1674. .setEndpoint(this.getEndpoint(3,data))
  1675. .setSignatureRequiredFlag(false)
  1676. ;
  1677. if(callback)
  1678. request.setOnSuccess(callback);
  1679. else if(this._callback)
  1680. request.setOnSuccess(this._callback);
  1681. request.send();
  1682. };
  1683. /**
  1684. * Returns the array of petitions that were created by the specified user.
  1685. *
  1686. * @see https://github.com/change/api_docs/blob/master/v1/documentation/resources/users.md#get-usersuser_idpetitions
  1687. *
  1688. * @param number|object data
  1689. * @param function callback
  1690. * @return ChangeOrgApiUser
  1691. */
  1692. this.getPetitions=function(data,callback) {
  1693. if(typeof data=='number')
  1694. data={user_id:data};
  1695. var request=new ChangeOrgApiRequest(this.getClient())
  1696. .setMethod('GET')
  1697. .addData(data)
  1698. .setEndpoint(this.getEndpoint(1,data))
  1699. .setSignatureRequiredFlag(false)
  1700. ;
  1701. if(callback)
  1702. request.setOnSuccess(callback);
  1703. else if(this._callback)
  1704. request.setOnSuccess(this._callback);
  1705. request.send();
  1706. };
  1707. /**
  1708. * Returns the array of petitions that were signed by the specified user.
  1709. *
  1710. * @see https://github.com/change/api_docs/blob/master/v1/documentation/resources/users.md#get-usersuser_idsignaturespetitions
  1711. *
  1712. * @param number|object data
  1713. * @param function callback
  1714. * @return ChangeOrgApiUser
  1715. */
  1716. this.getSignedPetitions=function(data,callback) {
  1717. if(typeof data=='number')
  1718. data={user_id:data};
  1719. var request=new ChangeOrgApiRequest(this.getClient())
  1720. .setMethod('GET')
  1721. .addData(data)
  1722. .setEndpoint(this.getEndpoint(2,data))
  1723. .setSignatureRequiredFlag(false)
  1724. ;
  1725. if(callback)
  1726. request.setOnSuccess(callback);
  1727. else if(this._callback)
  1728. request.setOnSuccess(this._callback);
  1729. request.send();
  1730. };
  1731. /**
  1732. * Set the request callback.
  1733. *
  1734. * @param function callback
  1735. * @return ChangeOrgApiUser
  1736. */
  1737. this.setCallback=function(callback) {
  1738. this._callback=callback;
  1739. return this;
  1740. };
  1741. /**
  1742. * Set the client.
  1743. *
  1744. * @param ChangeOrgApiClient client
  1745. * @return ChangeOrgApiPetition
  1746. */
  1747. this.setClient=function(client) {
  1748. if(!(client instanceof ChangeOrgApiClient))
  1749. throw new ChangeOrgApiException('Client must be an instance of ChangeOrgApiClient.');
  1750. this._client=client;
  1751. return this;
  1752. };
  1753. /**
  1754. * Initialization
  1755. ******************/
  1756. if(client)
  1757. this.setClient(client);
  1758. };