-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathrestfulie.min.js
1 lines (1 loc) · 10.4 KB
/
restfulie.min.js
1
var Links={build:function(a){if(typeof a!="object")return a;for(var b in a)a[b]=this.build(a[b]);if(!a.link)return a;if(!(a.link instanceof Array)){var c=a.link;a.link=[],a.link[0]=c}a.links={};for(var d in a.link)this._createRestfulieLinkOn(a,d);delete a.link;return a},_createRestfulieLinkOn:function(a,b){var c=a.link[b].rel,d=a.link[b].href,e=a.link[b].type;var f=Restfulie.at(d);e&&f.accepts(e),a.links[c]=f}};var PlainConverter={marshal:function(a){return a},unmarshal:function(a){return a.responseText}};var XmlConverter={marshal:function(a){return json2xml(a)},unmarshal:function(a){var b=a.responseText;if(!b)return{};var c=xml2json(parseXml(b)," ");return Links.build(JSON.parse(c))}};var JsonConverter={marshal:function(a){return JSON.stringify(a)},unmarshal:function(a){var b=a.responseText;if(!b)return{};return Links.build(JSON.parse(b))}};var Converters={mediaTypes:{},register:function(a,b){this.mediaTypes[a]=b},getMediaType:function(a){var b=this.mediaTypes[a];return b||PlainConverter}};Converters.register("application/json",JsonConverter),Converters.register("text/json",JsonConverter),Converters.register("json",JsonConverter),Converters.register("application/xml",XmlConverter),Converters.register("text/xml",XmlConverter),Converters.register("xml",XmlConverter),Converters.register("text/plain",PlainConverter),this.JSON||(this.JSON={}),function(){function f(a){return a<10?"0"+a:a}typeof Date.prototype.toJSON!=="function"&&(Date.prototype.toJSON=function(a){return isFinite(this.valueOf())?this.getUTCFullYear()+"-"+f(this.getUTCMonth()+1)+"-"+f(this.getUTCDate())+"T"+f(this.getUTCHours())+":"+f(this.getUTCMinutes())+":"+f(this.getUTCSeconds())+"Z":null},String.prototype.toJSON=Number.prototype.toJSON=Boolean.prototype.toJSON=function(a){return this.valueOf()});var cx=/[\u0000\u00ad\u0600-\u0604\u070f\u17b4\u17b5\u200c-\u200f\u2028-\u202f\u2060-\u206f\ufeff\ufff0-\uffff]/g,escapable=/[\\\"\x00-\x1f\x7f-\x9f\u00ad\u0600-\u0604\u070f\u17b4\u17b5\u200c-\u200f\u2028-\u202f\u2060-\u206f\ufeff\ufff0-\uffff]/g,gap,indent,meta={"\b":"\\b","\t":"\\t","\n":"\\n","\f":"\\f","\r":"\\r","\"":"\\\"","\\":"\\\\"},rep;function quote(a){escapable.lastIndex=0;return escapable.test(a)?"\""+a.replace(escapable,function(a){var b=meta[a];return typeof b==="string"?b:"\\u"+("0000"+a.charCodeAt(0).toString(16)).slice(-4)})+"\"":"\""+a+"\""}function str(a,b){var c,d,e,f,g=gap,h,i=b[a];i&&typeof i==="object"&&typeof i.toJSON==="function"&&(i=i.toJSON(a)),typeof rep==="function"&&(i=rep.call(b,a,i));switch(typeof i){case"string":return quote(i);case"number":return isFinite(i)?String(i):"null";case"boolean":case"null":return String(i);case"object":if(!i)return"null";gap+=indent,h=[];if(Object.prototype.toString.apply(i)==="[object Array]"){f=i.length;for(c=0;c<f;c+=1)h[c]=str(c,i)||"null";e=h.length===0?"[]":gap?"[\n"+gap+h.join(",\n"+gap)+"\n"+g+"]":"["+h.join(",")+"]",gap=g;return e}if(rep&&typeof rep==="object"){f=rep.length;for(c=0;c<f;c+=1)d=rep[c],typeof d==="string"&&(e=str(d,i),e&&h.push(quote(d)+(gap?": ":":")+e))}else for(d in i)Object.hasOwnProperty.call(i,d)&&(e=str(d,i),e&&h.push(quote(d)+(gap?": ":":")+e));e=h.length===0?"{}":gap?"{\n"+gap+h.join(",\n"+gap)+"\n"+g+"}":"{"+h.join(",")+"}",gap=g;return e}}typeof JSON.stringify!=="function"&&(JSON.stringify=function(a,b,c){var d;gap="",indent="";if(typeof c==="number")for(d=0;d<c;d+=1)indent+=" ";else typeof c==="string"&&(indent=c);rep=b;if(b&&typeof b!=="function"&&(typeof b!=="object"||typeof b.length!=="number"))throw new Error("JSON.stringify");return str("",{"":a})}),typeof JSON.parse!=="function"&&(JSON.parse=function(text,reviver){var j;function walk(a,b){var c,d,e=a[b];if(e&&typeof e==="object")for(c in e)Object.hasOwnProperty.call(e,c)&&(d=walk(e,c),d!==undefined?e[c]=d:delete e[c]);return reviver.call(a,b,e)}text=String(text),cx.lastIndex=0,cx.test(text)&&(text=text.replace(cx,function(a){return"\\u"+("0000"+a.charCodeAt(0).toString(16)).slice(-4)}));if(/^[\],:{}\s]*$/.test(text.replace(/\\(?:["\\\/bfnrt]|u[0-9a-fA-F]{4})/g,"@").replace(/"[^"\\\n\r]*"|true|false|null|-?\d+(?:\.\d*)?(?:[eE][+\-]?\d+)?/g,"]").replace(/(?:^|:|,)(?:\s*\[)+/g,""))){j=eval("("+text+")");return typeof reviver==="function"?walk({"":j},""):j}throw new SyntaxError("JSON.parse")})}();function json2xml(a,b){var c=function(a,b,d){var e="";if(a instanceof Array)for(var f=0,g=a.length;f<g;f++)e+=d+c(a[f],b,d+"\t")+"\n";else if(typeof a=="object"){var h=false;e+=d+"<"+b;for(var i in a)i.charAt(0)=="@"?e+=" "+i.substr(1)+"=\""+a[i].toString()+"\"":h=true;e+=h?">":"/>";if(h){for(var i in a)i=="#text"?e+=a[i]:i=="#cdata"?e+="<![CDATA["+a[i]+"]]>":i.charAt(0)!="@"&&(e+=c(a[i],i,d+"\t"));e+=(e.charAt(e.length-1)=="\n"?d:"")+"</"+b+">"}}else e+=d+"<"+b+">"+a.toString()+"</"+b+">";return e},d="";for(var e in a)d+=c(a[e],e,"");return b?d.replace(/\t/g,b):d.replace(/\t|\n/g,"")}var Restfulie={};(function(a){AjaxRequest={ajax:function(a,b,c,d){return jQuery.ajax({beforeSend:function(a){for(var b in d)a.setRequestHeader(b,d[b]);return a},data:c,type:a,url:b,async:false})}},a.at=function(a){return new b(a)};function b(a){this.uri=a,this.headers={Accept:"","Content-Type":"application/xml"};for(var b in Converters.mediaTypes)"register getMediaType".indexOf(b)==-1&&(this.headers.Accept+=(this.headers.Accept==""?"":", ")+b);this.accepts=function(a){this.headers.Accept=a;return this},this.as=function(a){this.headers["Content-Type"]=a;return this},this.requestAjax=function(a){var b=AjaxRequest.ajax(a,this.uri,"",this.headers);return SerializeXHR.serialize(b)},this.get=function(){return this.requestAjax("GET")},this.trace=function(){return this.requestAjax("TRACE")},this.head=function(){return this.requestAjax("HEAD")},this._delete=function(){return this.requestAjax("DELETE")},this.options=function(){return this.requestAjax("OPTIONS")},this.requestWithPayload=function(a,b){var c;var d;var e;c=b.response,delete b.response;if(typeof b=="string")d=b;else{var f=Converters.getMediaType(this.headers["Content-Type"]);d=f.marshal(b)}b.response=c,e=AjaxRequest.ajax(a,this.uri,d,this.headers);return SerializeXHR.serialize(e,this)},this.post=function(a){return this.requestWithPayload("POST",a)},this.patch=function(a){return this.requestWithPayload("PATCH",a)},this.put=function(a){return this.requestWithPayload("PUT",a)}}SerializeXHR={serialize:function(a,b){var c=SerializerXHRResponse[a.status];c||(c=SerializerXHRResponse["default"]);return c(a,b)}},SerializerXHRResponse={"default":function(a,b){var c={};return d(c,a)},200:function(a,b){var c={};return d(c,a)},201:function(a,b){var c=a.getResponseHeader("Location");var d=b.headers.Accept;return Restfulie.at(c).accepts(d).get()}};function c(a){var b={};var c={};var d=a.getAllResponseHeaders().split("\n");for(headerName in d){var e=d[headerName].split(":");var f=e[0];var g=e[1];if(f=="Link"){var h=g.split(";");var i=h[0].trim().replace(/<|>/g,"");var j=h[1].split("=")[1].replace(/"+/g,"").trim();c[j]=Restfulie.at(i).accepts(a.getResponseHeader("Content-Type").split(";")[0])}else g&&(b[f]=g.trim())}b.links=c;return b}function d(a,b){a.body=b.responseText,a.code=b.status,a.headers=c(b),a.resource=function(){var a=Converters.getMediaType(b.getResponseHeader("Content-Type").split(";")[0]);return a.unmarshal(b)};return a}function e(a,b){this.description=a,this.err=b}})(Restfulie),String.prototype.trim||(String.prototype.trim=function(){this.replace(/^\s+|\s+$/g,"")});function parseXml(a){var b=null;if(window.DOMParser)try{b=(new DOMParser).parseFromString(a,"text/xml")}catch(c){b=null}else if(window.ActiveXObject)try{b=new ActiveXObject("Microsoft.XMLDOM"),b.async=false,b.loadXML(a)||window.alert(b.parseError.reason+b.parseError.srcText)}catch(c){b=null}else alert("oops");return b}function xml2json(a,b){var c={toObj:function(a){var b={};if(a.nodeType==1){if(a.attributes.length)for(var d=0;d<a.attributes.length;d++)b["@"+a.attributes[d].nodeName]=(a.attributes[d].nodeValue||"").toString();if(a.firstChild){var e=0,f=0,g=false;for(var h=a.firstChild;h;h=h.nextSibling)h.nodeType==1?g=true:h.nodeType==3&&h.nodeValue.match(/[^ \f\n\r\t\v]/)?e++:h.nodeType==4&&f++;if(g)if(e<2&&f<2){c.removeWhite(a);for(var h=a.firstChild;h;h=h.nextSibling)h.nodeType==3?b["#text"]=c.escape(h.nodeValue):h.nodeType==4?b["#cdata"]=c.escape(h.nodeValue):b[h.nodeName]?b[h.nodeName]instanceof Array?b[h.nodeName][b[h.nodeName].length]=c.toObj(h):b[h.nodeName]=[b[h.nodeName],c.toObj(h)]:b[h.nodeName]=c.toObj(h)}else a.attributes.length?b=transform(c.escape(c.innerXml(a))):b=transform(c.escape(c.innerXml(a)));else if(e)a.attributes.length?b=transform(c.escape(c.innerXml(a))):b=transform(c.escape(c.innerXml(a)));else if(f)if(f>1)b=c.escape(c.innerXml(a));else for(var h=a.firstChild;h;h=h.nextSibling)b["#cdata"]=c.escape(h.nodeValue)}!a.attributes.length&&!a.firstChild&&(b=null)}else a.nodeType==9?b=c.toObj(a.documentElement):alert("unhandled node type: "+a.nodeType);return b},toJson:function(a,b,d){var e=b?"\""+b+"\"":"";if(a instanceof Array){for(var f=0,g=a.length;f<g;f++)a[f]=c.toJson(a[f],"",d+"\t");e+=(b?":[":"[")+(a.length>1?"\n"+d+"\t"+a.join(",\n"+d+"\t")+"\n"+d:a.join(""))+"]"}else if(a==null)e+=(b&&":")+"null";else if(typeof a=="object"){var h=[];for(var i in a)h[h.length]=c.toJson(a[i],i,d+"\t");e+=(b?":{":"{")+(h.length>1?"\n"+d+"\t"+h.join(",\n"+d+"\t")+"\n"+d:h.join(""))+"}"}else typeof a=="string"?e+=(b&&":")+"\""+a.toString()+"\"":e+=(b&&":")+a.toString();return e},innerXml:function(a){var b="";if("innerHTML"in a)b=a.innerHTML;else{var c=function(a){var b="";if(a.nodeType==1){b+="<"+a.nodeName;for(var d=0;d<a.attributes.length;d++)b+=" "+a.attributes[d].nodeName+"=\""+(a.attributes[d].nodeValue||"").toString()+"\"";if(a.firstChild){b+=">";for(var e=a.firstChild;e;e=e.nextSibling)b+=c(e);b+="</"+a.nodeName+">"}else b+="/>"}else a.nodeType==3?b+=a.nodeValue:a.nodeType==4&&(b+="<![CDATA["+a.nodeValue+"]]>");return b};for(var d=a.firstChild;d;d=d.nextSibling)b+=c(d)}return b},escape:function(a){return a.replace(/[\\]/g,"\\\\").replace(/[\"]/g,"\\\"").replace(/[\n]/g,"\\n").replace(/[\r]/g,"\\r")},removeWhite:function(a){a.normalize();for(var b=a.firstChild;b;)if(b.nodeType==3)if(b.nodeValue.match(/[^ \f\n\r\t\v]/))b=b.nextSibling;else{var d=b.nextSibling;a.removeChild(b),b=d}else b.nodeType==1?(c.removeWhite(b),b=b.nextSibling):b=b.nextSibling;return a}};a.nodeType==9&&(a=a.documentElement);var d=c.toJson(c.toObj(c.removeWhite(a)),a.nodeName,"\t");return"{\n"+b+(b?d.replace(/\t/g,b):d.replace(/\t|\n/g,""))+"\n}"}function transform(a){if(isNumber(a))return parseFloat(a);return a}function isNumber(a){return!isNaN(parseFloat(a))&&isFinite(a)}