Skip to content

Commit e783e03

Browse files
committed
first update package 0.0.5
1 parent 7f57150 commit e783e03

File tree

6 files changed

+242
-0
lines changed

6 files changed

+242
-0
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
.DS_Store
2+
node_modules

Gruntfile.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
module.exports = function(grunt) {
2+
3+
// Project configuration.
4+
grunt.initConfig({
5+
pkg: grunt.file.readJSON('package.json'),
6+
uglify: {
7+
options: {
8+
banner: '/*! <%= pkg.name %> <%= grunt.template.today("yyyy-mm-dd") %> */\n'
9+
},
10+
build: {
11+
src: '<%= pkg.name %>.js',
12+
dest: '<%= pkg.name %>.min.js'
13+
}
14+
}
15+
});
16+
17+
// Load the plugin that provides the "uglify" task.
18+
grunt.loadNpmTasks('grunt-contrib-uglify');
19+
20+
// Default task(s).
21+
grunt.registerTask('default', ['uglify']);
22+
23+
};

bower.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"name": "ngapi",
3+
"version": "0.1.0",
4+
"main": "./ngapi.js",
5+
"dependencies": {
6+
"angular": ">=1.0.8"
7+
}
8+
}

ngapi.js

Lines changed: 185 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,185 @@
1+
'use strict';
2+
angular.module('ngapi',[])
3+
.factory('apiroutes',['apiurl','$http',function(apiurl, $http){
4+
if(!!apiurl){
5+
return apiurl;
6+
}else{
7+
var routes = {};
8+
angular.forEach(apiurl,function(v,i,o){
9+
var h = /http:/;
10+
$http.get(v).success(function(data){
11+
routes[i] = data;
12+
if(h.test(v)){
13+
routes[i].url = 'http://'+v.replace('http://','').split('/')[0];
14+
}
15+
// console.log(routes);
16+
});
17+
})
18+
return routes;
19+
};
20+
}])
21+
.directive('ngApi',['apiroutes','apicrypto','apicnonce','apivalidate','$http',function(apiroutes, apicrypto, apicnonce, apivalidate, $http){
22+
/* validate method part , you can expand it by apivalidate*/
23+
var methods = {
24+
validate:{
25+
email:function(v){
26+
var _ckEmail = /.+\@.+\..+/;
27+
return _ckEmail.test(v);
28+
},
29+
number:function(v){
30+
var _cknumber = /[0-9]/g;
31+
return _cknumber.test(v);
32+
},
33+
string:function(v){
34+
var _chspec = /\w+/;
35+
if(v.match(_chspec)[0] == v){
36+
return true;
37+
}else{
38+
return false;
39+
}
40+
},
41+
nospace:function(v){
42+
var _ckspace = /\s/;
43+
if(_ckspace.test(v)){
44+
return false;
45+
}else{
46+
return true;
47+
}
48+
}
49+
}
50+
};
51+
52+
return {
53+
replace: true,
54+
restrict: "AE",
55+
link: function (scope, elem, attr) {
56+
var _this = scope;
57+
var _s = attr.ngApi;
58+
_s = _s.replace(' ','');
59+
var _d = /(\(.*\))(\(.*\))/;
60+
var _data = _s.match(_d);
61+
/* reg parse method */
62+
var _reg = {
63+
init:function(v){
64+
return v.split(/^\(/)[1].split(/\)$/)[0].split('||');
65+
},
66+
specColon:function(v){
67+
var _v = /:/;
68+
if(_v.test(v)){
69+
return v.split(/:/);
70+
}else{
71+
return false;
72+
}
73+
}
74+
}
75+
76+
elem.bind('click',function(){
77+
if(_data.length <=3){
78+
var _tmpdata = _reg.init(_data[1]);
79+
var _tmpapi = _reg.init(_data[2]);
80+
var _ckapi = /:/;
81+
82+
angular.forEach(_tmpdata,function(v,i,o){
83+
if(methods.validate[_reg.specColon(v)[1]]){
84+
if(!!!methods.validate[_reg.specColon(v)[1]](_this[_reg.specColon(v)[0]])){
85+
console.log('驗證不通過');
86+
return false;
87+
}
88+
}else{
89+
if(!!!apivalidate[_reg.specColon(v)[1]](_this[_reg.specColon(v)[0]])){
90+
console.log('驗證不通過');
91+
return false;
92+
}
93+
}
94+
});
95+
96+
var _m = _reg.specColon(_tmpapi[0])[0] || apiroutes[_tmpapi[0].split('.')[0]][_tmpapi[0].replace(_tmpapi[0].split('.')[0]+'.','')].methods[0];
97+
_m = _m.toLowerCase();
98+
99+
var _success = attr.apiSuccess;
100+
var _error = attr.apiError;
101+
102+
$http.defaults.useXDomain = true;
103+
104+
switch (_m){
105+
case 'get':
106+
if(_ckapi.test(_tmpapi)){
107+
$http.get(apiroutes+_reg.specColon(_tmpapi[0])[1])
108+
.success(function(data){
109+
if(!!_this[_success]){
110+
return _this[_success](data);
111+
}else{
112+
return ;
113+
};
114+
})
115+
.error(function(data){
116+
if(!!_this[_error]){
117+
return _this[_error](data);
118+
}else{
119+
return ;
120+
};
121+
})
122+
}else{
123+
$http.get(apiroutes[_tmpapi[0].split('.')[0]].url+apiroutes[_tmpapi[0].split('.')[0]][_tmpapi[0].replace(_tmpapi[0].split('.')[0]+'.','')].path)
124+
.success(function(data){
125+
if(!!_this[_success]){
126+
return _this[_success](data);
127+
}else{
128+
return ;
129+
};
130+
})
131+
.error(function(data){
132+
if(!!_this[_error]){
133+
return _this[_error](data);
134+
}else{
135+
return ;
136+
};
137+
})
138+
}
139+
break;
140+
case 'post':
141+
var _url = apiurl+_reg.specColon(_tmpapi[0])[1];
142+
$http.post(_url, {
143+
something: true,
144+
color: 'blue',
145+
one: 1
146+
}).success(function(data){
147+
if(!!_this[_success]){
148+
return _this[_success](data);
149+
}else{
150+
return ;
151+
};
152+
});
153+
break;
154+
case 'restfullogin':
155+
var _nonce = _reg.specColon(_tmpapi[0])[1];
156+
var _login = _tmpapi[1];
157+
$http.get(apiroutes+_nonce).success(function (data) {
158+
var password = apicrypto(_this.password);
159+
var hash = apicrypto([password, data.data.nonce, apicnonce].sort().join(''));
160+
$http.post(apiroutes+_login,{
161+
login: _this.user,
162+
cnonce: apicnonce,
163+
hash: hash,
164+
key: data.data.key
165+
}).success(function(data){
166+
console.log(data);
167+
if(!!_this[_success]){
168+
return _this[_success](data);
169+
}else{
170+
return ;
171+
};
172+
});
173+
});
174+
break;
175+
};
176+
}else{
177+
console.log('Your youmeb value is wrong! must be=>(data)(method:apiurl)')
178+
};
179+
});
180+
},
181+
};
182+
}]);
183+
184+
185+

ngapi.min.js

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
{
2+
"name": "ngapi",
3+
"version": "0.1.0",
4+
"description": "An AngularJS module that makes your APIs working in the Angular Way.",
5+
"main": "ngapi.js",
6+
"scripts": {
7+
"test": "echo \"Error: no test specified\" && exit 1"
8+
},
9+
"repository": {
10+
"type": "git",
11+
"url": "[email protected]:iamblue/ngApi.git"
12+
},
13+
"author": "iamblue",
14+
"license": "MIT",
15+
"bugs": {
16+
"url": "https://github.com/iamblue/ngapi/issues"
17+
},
18+
"devDependencies": {
19+
"grunt": "~0.4.1",
20+
"grunt-contrib-uglify": "~0.2.4"
21+
}
22+
}

0 commit comments

Comments
 (0)