Comparing version 0.0.5 to 0.0.6
var yode = require('../index'); | ||
yo = new yode({ | ||
'logging': true, | ||
'api_token': '046e3c0a-8706-e403-5fc8-025b3c301ed8' | ||
}); | ||
yo.getSubscriberCount(function (data){ | ||
console.log(data); | ||
}); |
@@ -1,2 +0,4 @@ | ||
var request = require('request'); | ||
var request = require('request'), | ||
colors = require('colors'), | ||
helpers = require('./helpers'); | ||
@@ -6,2 +8,3 @@ var e = module.exports = function(options) { | ||
this.config = { | ||
'logging': options.logging ? options.logging : false, | ||
'api_token': options.api_token ? options.api_token : undefined | ||
@@ -21,2 +24,3 @@ }; | ||
var self = this; | ||
helpers.log(self, 'Sending request to Yo API...', 'white'); | ||
request.get('http://api.justyo.co/subscribers_count/?api_token=' + api_token, function(err, response, body) { | ||
@@ -27,2 +31,3 @@ if (err) throw err; | ||
self.config.api_token = api_token; | ||
helpers.log(self, 'Request was successful', 'green'); | ||
return cb ? cb({ | ||
@@ -37,6 +42,8 @@ "status": 1, | ||
else { | ||
helpers.log(self, 'Request was unsuccessful', 'red'); | ||
helpers.log(self, 'Error: API token is invalid.', 'red'); | ||
return cb ? cb({ | ||
"status": 0, | ||
"response": null, | ||
"errors": ["API key is invalid."] | ||
"errors": ["API token is invalid."] | ||
}) : false; | ||
@@ -49,2 +56,3 @@ } | ||
var self = this; | ||
helpers.log(self, 'Sending request to Yo API...', 'white'); | ||
if (self.config.api_token != undefined) { | ||
@@ -60,3 +68,5 @@ if (options.usernames) { | ||
var data = JSON.parse(body); | ||
if (data.result == "OK") { | ||
if (data.result != "OK") { | ||
helpers.log(self, 'Request was unsuccessful.', 'red'); | ||
helpers.log(self, 'Error: ' + [data.error] + '.', 'red'); | ||
return cb ? cb({ | ||
@@ -69,2 +79,3 @@ "status": 0, | ||
if (i == options.usernames.length - 1) { | ||
helpers.log(self, 'Request was successful.', 'green'); | ||
return cb ? cb({ | ||
@@ -90,2 +101,3 @@ "status": 1, | ||
if (data.result == "OK") { | ||
helpers.log(self, 'Request was successful.', 'green'); | ||
return cb ? cb({ | ||
@@ -100,2 +112,4 @@ "status": 1, | ||
else { | ||
helpers.log(self, 'Request was unsuccessful.', 'red'); | ||
helpers.log(self, 'Error: ' + [data.error] + '.', 'red'); | ||
return cb ? cb({ | ||
@@ -110,2 +124,4 @@ "status": 0, | ||
else { | ||
helpers.log(self, 'Request was unsuccessful.', 'red'); | ||
helpers.log(self, 'Error: No username(s) set to Yo.', 'red'); | ||
return cb ? cb({ | ||
@@ -119,6 +135,7 @@ "status": 0, | ||
else { | ||
helpers.log(self, 'Error: No API token has been set.', 'red'); | ||
return cb ? cb({ | ||
"status": 0, | ||
"response": null, | ||
"errors": ["No API key has been set."] | ||
"errors": ["No API token has been set."] | ||
}) : false; | ||
@@ -134,3 +151,7 @@ } | ||
} | ||
else if(options == undefined){ | ||
options = {}; | ||
} | ||
if (self.config.api_token != undefined) { | ||
helpers.log(self, 'Sending request to Yo API...', 'white'); | ||
request.post('http://api.justyo.co/yoall/', {form: { | ||
@@ -141,3 +162,4 @@ 'api_token': self.config.api_token, | ||
if (err) throw err; | ||
if (response.statusCode != 200) { | ||
if (response.statusCode == 201) { | ||
helpers.log(self, 'Request was successful.', 'green'); | ||
return cb ? cb({ | ||
@@ -152,2 +174,4 @@ "status": 1, | ||
else { | ||
helpers.log(self, 'Request was unsuccessful.', 'red'); | ||
helpers.log(self, 'Error: ' + data.error, 'red'); | ||
return cb ? cb({ | ||
@@ -162,6 +186,7 @@ "status": 0, | ||
else { | ||
helpers.log(self, 'Could not send a request to the Yo API: no API token has been set.', 'red'); | ||
return cb ? cb({ | ||
"status": 0, | ||
"response": null, | ||
"errors": ["No API key has been set."] | ||
"errors": ["No API token has been set."] | ||
}) : false; | ||
@@ -174,15 +199,19 @@ } | ||
if (self.config.api_token != undefined) { | ||
helpers.log(self, 'Sending request to Yo API...', 'white'); | ||
request.get('http://api.justyo.co/subscribers_count/?api_token=' + self.config.api_token, function(err, response, body) { | ||
if (err) throw err; | ||
var data = JSON.parse(body); | ||
if (data.result == 1) { | ||
if (response.statusCode == 200) { | ||
helpers.log(self, 'Request was successful.', 'green'); | ||
return cb ? cb({ | ||
"status": 1, | ||
"data": data.data | ||
"data": data.result | ||
}) : true; | ||
} | ||
else { | ||
helpers.log(self, 'Request was unsuccessful', 'red'); | ||
helpers.log(self, 'Error: API token is invalid.', 'red'); | ||
return cb ? cb({ | ||
"status": 0, | ||
"error": "API key is invalid." | ||
"error": "API token is invalid." | ||
}) : false; | ||
@@ -193,8 +222,9 @@ } | ||
else { | ||
helpers.log(self, 'Could not send a request to the Yo API: no API token has been set.', 'red'); | ||
return cb ? cb({ | ||
"status": 0, | ||
"response": null, | ||
"errors": ["No API key has been set."] | ||
"errors": ["No API token has been set."] | ||
}) : false; | ||
} | ||
} |
{ | ||
"name": "yode", | ||
"version": "0.0.5", | ||
"version": "0.0.6", | ||
"description": "A Yo api wrapper for Node.js", | ||
@@ -21,4 +21,5 @@ "main": "index.js", | ||
"dependencies": { | ||
"request": ">=2.40" | ||
"request": ">=2.40", | ||
"colors": ">=0.6.0" | ||
} | ||
} |
7181
6
232
2
+ Addedcolors@>=0.6.0
+ Addedcolors@1.4.0(transitive)