-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmintpresso-0.2.js
More file actions
14 lines (11 loc) · 9.4 KB
/
mintpresso-0.2.js
File metadata and controls
14 lines (11 loc) · 9.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
// Generated by CoffeeScript 1.6.1
/*
mintpresso-0.2.coffee
Author: Jinhyuk Lee <eces at mstock.org>
Organization: MINTPRESSO <support at mintpresso.com>
Repository: https://github.com/mintpresso/javascript-api
Description:
This is a JavaScript API library for MINTPRESSO Data Cloud.
Supports basic graph operations.
Documentation: docs.mintpresso.com/javascript-api/index.html
*/(function(){var e,t,n,r,i,s,o,u,a,f;String.prototype.format===void 0&&(String.prototype.format=function(){var e;e=arguments;return this.replace(/{(\d+)}/g,function(t,n){return typeof e[n]!="undefined"?e[n]:t})});String.prototype.endsWith=function(e){return this.substr(this.length-e.length)===e};String.prototype.startsWith=function(e){return this.substr(0,e.length)===e};try{a={log:"[MINTPRESSO] ",version:"/v1"};t={name:"JS 0.2 API",key:"",id:"",useDebugCallback:!0,getAPI:function(){return"api_token="+t.key}};f={list:[],iteration:0,get:function(){return f.list[f.iteration]},isReady:!1,timeout:5e3,urls:{getPoint:"/account/{0}/point/{1}?",getPointByTypeOrIdentifier:"/account/{0}/point?type={1}&identifier={2}&",addPoint:"/post/account/{0}/point?json={1}&updateIfExists={2}&",findEdge:"/account/{0}/edge?subjectId={1}&subjectType={2}&subjectIdentifier={3}&verb={4}&objectId={5}&objectType={6}&objectIdentifier={7}&getInnerModels={8}&",linkWithEdge:"/post/account/{0}/edge?json={1}&"},dataType:"jsonp",useCallback:!0,callbackName:"JSAPIMINTPRESSOCALLBACK"};n={pageTracker:!1};u={verbs:new Array("do","does","did","verb"),mark:"?",subjectMark:"$",objectMark:"_",point:{prototype:new Array("type","identifier","data")},edge:{prototype:new Array("subjectId","subjectType","verb","objectId","objectType")}};i=function(e,n){return jQuery.ajax({url:f.get()+a.version+f.urls.getPoint.format(t.id,e)+t.getAPI(),type:"GET",async:!0,cache:!1,crossDomain:!0,dataType:f.dataType,jsonpCallback:f.callbackName,timeout:f.timeout,success:function(e){var t,r;t=void 0;if(e.status.code===200&&e.point.data!==void 0){for(r in e.point.data)r!=="data"?e.point[r]=e.point.data[r]:t=e.point.data[r];t===void 0?delete e.point.data:e.point.data=t}return n(e)},error:function(e,t,r){console.error(""+a.log+" Response("+t+") "+r);return n({status:{code:t,message:"Response("+t+") "+r}})}})};s=function(e,n){var r,i,s,o;r=0;o="";s="";for(i in e){if(r>0){console.log(""+a.log+"Too many arguments are given to be an informative query though no question marks are found - mintpresso.get");break}if(i.length===0||i!==u.mark)o=encodeURIComponent(i);if(e[i].length===0||e[i]===u.mark){console.log(a.log+(""+a.log+"No question mark is allowed on 'identifier' field - mintpresso.get"));return!1}s=encodeURIComponent(e[i]);r++}return jQuery.ajax({url:f.get()+a.version+f.urls.getPointByTypeOrIdentifier.format(t.id,o,s)+t.getAPI(),type:"GET",async:!0,cache:!1,crossDomain:!0,dataType:f.dataType,jsonpCallback:f.callbackName,timeout:f.timeout,success:function(e){var t,s,o,u;t=void 0;if(e.status.code===200)if(e.point!==void 0){if(e.point.data!==void 0){for(i in e.point.data)i!=="data"?e.point[i]=e.point.data[i]:t=e.point.data[i];t===void 0?delete e.point.data:e.point.data=t}}else if(e.points!==void 0)for(r=o=0,u=e.points.length-1;o<=u;r=o+=1){s=e.points[r];if(s.data!==void 0){for(i in s.data)i!=="data"?e.points[r][i]=s.data[i]:t=s.data[i];t===void 0?delete e.points[r].data:e.points[r].data=t}}else console.error(a.log+"Found results neither point nor points - mintpresso._getPointByTypeOrIdentifier");return n(e)},error:function(e,t,r){console.error(""+a.log+" Response("+t+") "+r);return n(JSON.parse(e.responseText))}})};r=function(e,n,r){var i,s,o,l,c,h,p,d,v;r==null&&(r=!1);i=1;d="";h=-1;p="";v="";c="";o=-1;l="";for(s in e){switch(i){case 1:s!==void 0&&s.indexOf(u.subjectMark)===0?d=s.substring(1,s.length):d=s;d=encodeURIComponent(d);e[s]!==u.mark&&typeof e[s]=="number"&&(h=encodeURIComponent(e[s]));e[s]!==u.mark&&typeof e[s]=="string"&&(p=encodeURIComponent(e[s]));break;case 2:u.verbs.indexOf(s)===-1?console.log(a.log+"Verb isn't match with do/does/did/verb. - mintpresso.get"):e[s]!==u.mark&&(v=encodeURIComponent(e[s]));break;case 3:s!==void 0&&s.indexOf(u.objectMark)===0?c=s.substring(1,s.length):c=s;c=encodeURIComponent(c);e[s]!==u.mark&&typeof e[s]=="number"&&(o=encodeURIComponent(e[s]));e[s]!==u.mark&&typeof e[s]=="string"&&(l=encodeURIComponent(e[s]));break;default:console.log(a.log+"Too many arguments are given to be a form of subject/verb/object query - mintpresso.get");return!1}i++}return jQuery.ajax({url:f.get()+a.version+f.urls.findEdge.format(t.id,h,d,p,v,o,c,l,r)+t.getAPI(),type:"GET",async:!0,cache:!1,crossDomain:!0,dataType:f.dataType,jsonpCallback:f.callbackName,timeout:f.timeout,success:function(e){return n(e)},error:function(e,t,r){console.error(""+a.log+" Response("+t+") "+r);return n(JSON.parse(e.responseText))}})};e=function(e,n,r){var i,s;r==null&&(r=!1);s={};s.point={};s.point.data={};for(i in e)u.point.prototype.indexOf(i)!==-1?s.point[i]=e[i]:s.point.data[i]=e[i];return jQuery.ajax({url:f.get()+a.version+f.urls.addPoint.format(t.id,encodeURIComponent(JSON.stringify(s)),r)+t.getAPI(),type:"GET",async:!0,cache:!1,crossDomain:!0,dataType:f.dataType,jsonpCallback:f.callbackName,timeout:f.timeout,success:function(e){return n(e)},error:function(e,t,r){console.error(""+a.log+" Response("+t+") "+r);return n(JSON.parse(e.responseText))}})};o=function(e,n){var r,i,s;s={};s.edge={};r=1;for(i in e){switch(r){case 1:i!==u.mark&&u.edge.prototype.indexOf(i)===-1&&(s.edge.subjectType=i);e[i]!==u.mark&&(s.edge.subjectId=e[i]);s.edge.subjectType!==void 0&&s.edge.subjectType.indexOf(u.subjectMark)===0&&(s.edge.subjectType=s.edge.subjectType.substring(1,s.edge.subjectType.length));break;case 2:u.verbs.indexOf(i)===-1?console.log(""+a.log+" Verb isn't match with do/does/did/verb. - mintpresso.set"):s.edge.verb=e[i];break;case 3:i!==u.mark&&u.edge.prototype.indexOf(i)===-1&&(s.edge.objectType=i);e[i]!==u.mark&&(s.edge.objectId=e[i]);s.edge.objectType!==void 0&&s.edge.objectType.indexOf(u.objectMark)===0&&(s.edge.objectType=s.edge.objectType.substring(1,s.edge.objectType.length));break;default:console.log(""+a.log+" Too many arguments are given to be a form of subject/verb/object query - mintpresso.set");return!1}r++}return jQuery.ajax({url:f.get()+a.version+f.urls.linkWithEdge.format(t.id,encodeURIComponent(JSON.stringify(s)))+t.getAPI(),type:"GET",async:!0,cache:!1,crossDomain:!0,dataType:f.dataType,jsonpCallback:f.callbackName,timeout:f.timeout,success:function(e){return n(e)},error:function(e,t,r){console.error(""+a.log+" Response("+t+") "+r);return n(JSON.parse(e.responseText))}})};window.mintpresso={};window.mintpresso={get:function(){var e,t,n,o,u,l;if(f.isReady===!1)return console.warn(""+a.log+"Not initialized. Add mintpress.init in your code with API key.");if(arguments.length===0)return console.warn(""+a.log+"An argument is required for mintpresso.get method.");if(arguments.length<=2){n=!1;arguments[2]!==void 0&&"string"&&(n=Boolean(arguments[2]===!0));arguments[1]!==void 0&&typeof arguments[1]=="function"?e=arguments[1]:e=window.mintpresso.callback;if(typeof arguments[0]=="number")return i(arguments[0],e);if(typeof arguments[0]=="object"){u=arguments[0];o=!1;t=0;for(l in u){t++;if(l==="?"||u[l]==="?")o=!0}return o&&t>1||t===3?r(arguments[0],e,n):s(arguments[0],e)}return console.warn(""+a.log+" An argument type of Number or String is required for mintresso.get method.")}return console.warn(""+a.log+" Too many arguments in mintpresso.get method.")},set:function(){var t,n,r,i;if(f.isReady===!1)return console.warn(""+a.log+"Not initialized. Add mintpress.init in your code with API key.");if(arguments.length===0)console.warn(""+a.log+"An argument is required for mintpresso.set method.");else if(arguments.length<=3)if(typeof arguments[0]=="object"){n=!1;for(r in arguments[0])if(u.verbs.indexOf(r)!==-1){n=!0;break}i=!1;t=window.mintpresso.callback;if(arguments[1]!==void 0)if(typeof arguments[1]=="function"){t=arguments[1];arguments[2]!==void 0&&typeof arguments=="boolean"&&(i=Boolean(arguments[2]===!0))}else typeof arguments[1]=="boolean"&&(i=Boolean(arguments[1]===!0));n===!0?o(arguments[0],t):arguments[1]===void 0||arguments[1]===!1?e(arguments[0],t,i):e(arguments[0],t,i)}else console.warn(""+a.log+"An JSON object is required for mintpresso.set method.");else console.warn(""+a.log+"Too many arguments in mintpresso.get method.");return!0},callback:function(e){var n;if(t.useDebugCallback===!0)return(e!=null?(n=e.status)!=null?n.code:void 0:void 0)>201?console.log(""+a.log+"Response("+e.status.code+"): "+e.status.message,e):console.log(""+a.log+"Response("+e.status.code+"): ",e)},init:function(e,n,r){var i;if(typeof e!="string")return console.warn(""+a.log+" Not initialized. Invalid API key. (required: String)");t.key=e;t.id=n;if(r.withoutCallback!==void 0&&r.withoutCallback===!0){f.dataType="json";f.callbackName=void 0;f.useCallback=!1}else f.useCallback=!0;i="//api.mintpresso.com";if(r.useLocalhost!==void 0&&r.useLocalhost===!0){console.log(""+a.log+"Using localhost server (http://localhost:15100)");i="//localhost:15100"}"https:"===document.location.protocol?f.list.push("https:"+i):f.list.push("http:"+i);if(r.callbackFunction!==void 0&&r.callbackFunction.length>0&&r.callbackFunction in window){window[r.callbackFunction](window.mintpresso);console.log(""+a.log+"window."+r.callbackFunction+" is called.")}r.disableDebugCallback!==void 0&&r.disableDebugCallback===!0?t.useDebugCallback=!1:t.useDebugCallback=!0;f.isReady=!0;return!0}}}catch(l){if(window.mintpresso!==void 0&&!0)throw l;console.warn(""+a.log+" Failed to load API.")}!0}).call(this);