pastebin-js
Advanced tools
Comparing version 0.2.1 to 0.3.0
@@ -10,8 +10,10 @@ 'use strict'; | ||
var _ = require('underscore'), | ||
fs = require('fs'), | ||
xml2js = require('xml2js'), | ||
Q = require('q'), | ||
method = require('../lib/methods'), | ||
conf = require('../lib/config'); | ||
var _ = require('underscore'); | ||
var fs = require('fs'); | ||
var xml2js = require('xml2js'); | ||
var Q = require('q'); | ||
var method = require('../lib/methods'); | ||
var conf = require('../lib/config'); | ||
var debug = function (msg) { console.log(msg); }; | ||
var noop = function () {}; | ||
@@ -28,2 +30,5 @@ /** | ||
this.config = _.extend(conf.defaults, config); | ||
if (!this.config.debug) { | ||
debug = noop; | ||
} | ||
@@ -38,16 +43,11 @@ return this; | ||
Pastebin.prototype.getPaste = function (id) { | ||
var _this = this, | ||
deferred = Q.defer(), | ||
getUrl = conf.net.protocol + conf.net.base + conf.net.endpoint.raw + id; | ||
debug('[pastebin-js::getPaste] called'); | ||
if (!id) { | ||
var deferred = Q.defer(); | ||
deferred.reject(new Error('[-] getPaste : No paste id given!')); | ||
return deferred.promise; | ||
} | ||
method | ||
.get(getUrl, null) | ||
.then(deferred.resolve) | ||
.fail(deferred.reject); | ||
return deferred.promise; | ||
return method.get(conf.net.protocol + conf.net.base + conf.net.endpoint.raw + id, null); | ||
}; | ||
@@ -64,18 +64,15 @@ | ||
Pastebin.prototype.createPaste = function (text, title, format, privacy, expiration) { | ||
var _this = this, | ||
deferred = Q.defer(), | ||
p = {}, | ||
optional = {}, | ||
formats = null, | ||
expirations = null, | ||
noKeyNeeded = true; | ||
var deferred = Q.defer(); | ||
var p = { | ||
api_option : 'paste', | ||
api_dev_key : this.config.api_dev_key, | ||
api_paste_code : text, | ||
}; | ||
var noKeyNeeded = true; | ||
if (typeof text !== 'string') { | ||
deferred.reject(new Error('Error! Paste can only be a text!')); | ||
return deferred.promise; | ||
} | ||
p.api_option = 'paste'; | ||
p.api_dev_key = _this.config.api_dev_key; | ||
p.api_paste_code = text; | ||
if (typeof title !== 'undefined' && title !== null) { | ||
@@ -86,7 +83,7 @@ p.api_paste_name = title; | ||
if (typeof format !== 'undefined' && format !== null) { | ||
formats = conf.formats; | ||
if (formats[format]) { | ||
if (conf.formats[format]) { | ||
p.api_paste_format = format; | ||
} else { | ||
deferred.reject(new Error('Error! Paste format ' + format + ' is unknown.')); | ||
return deferred.promise; | ||
} | ||
@@ -99,19 +96,20 @@ } | ||
} else if (privacy === 2) { | ||
if (_this.config.api_user_key) { | ||
p.api_user_key = _this.config.api_user_key; | ||
} else if (_this.config.api_user_name !== null && _this.config.api_user_password !== null) { | ||
if (this.config.api_user_key) { | ||
p.api_user_key = this.config.api_user_key; | ||
} else if (this.config.api_user_name !== null && this.config.api_user_password !== null) { | ||
noKeyNeeded = false; | ||
_this | ||
.createAPIuserKey() | ||
this.createAPIuserKey() | ||
.then(function () { | ||
_this | ||
.createPaste(text, title, format, privacy, expiration) | ||
.then(deferred.resolve) | ||
.fail(deferred.reject); | ||
}); | ||
this.createPaste(text, title, format, privacy, expiration) | ||
.then(deferred.resolve) | ||
.fail(deferred.reject); | ||
return deferred.promise; | ||
}.bind(this)); | ||
} else { | ||
deferred.reject(new Error('Error! For this privacy level you need to be logged in! Provide username and password!')); | ||
return deferred.promise; | ||
} | ||
} else { | ||
deferred.reject(new Error('Error! Privacy level is unknown!')); | ||
return deferred.promise; | ||
} | ||
@@ -121,19 +119,13 @@ } | ||
if (typeof expiration !== 'undefined' && expiration !== null) { | ||
expirations = conf.expiration; | ||
if (expirations[expiration]) { | ||
p.api_paste_expire_date = expiration; | ||
} else { | ||
if (!conf.expiration[expiration]) { | ||
deferred.reject(new Error('Error! Paste expiration ' + expiration + ' is unknown.')); | ||
return deferred.promise; | ||
} | ||
p.api_paste_expire_date = expiration; | ||
} | ||
if (noKeyNeeded) { | ||
_this | ||
._postApi(conf.net.protocol + conf.net.base + conf.net.endpoint.post, p) | ||
.then(deferred.resolve) | ||
.fail(deferred.reject); | ||
return this._postApi(conf.net.protocol + conf.net.base + conf.net.endpoint.post, p); | ||
} | ||
return deferred.promise; | ||
}; | ||
@@ -150,4 +142,3 @@ | ||
Pastebin.prototype.createPasteFromFile = function (filename, title, format, privacy, expiration) { | ||
var _this = this, | ||
deferred = Q.defer(); | ||
var deferred = Q.defer(); | ||
@@ -163,6 +154,5 @@ if (!filename) { | ||
_this | ||
.createPaste(data, title, format, privacy, expiration) | ||
.then(deferred.resolve) | ||
.fail(deferred.reject); | ||
this.createPaste(data, title, format, privacy, expiration) | ||
.then(deferred.resolve) | ||
.fail(deferred.reject); | ||
@@ -179,32 +169,28 @@ }); | ||
Pastebin.prototype.deletePaste = function (pasteID) { | ||
var _this = this, | ||
p = {}, | ||
deferred = Q.defer(); | ||
var deferred = Q.defer(); | ||
if (!pasteID) { | ||
deferred.reject(new Error('Please provide a paste ID to delete')); | ||
return deferred.promise; | ||
} | ||
p.api_option = 'delete'; | ||
p.api_dev_key = _this.config.api_dev_key; | ||
p.api_paste_key = pasteID; | ||
var params = { | ||
api_option : 'delete', | ||
api_dev_key : this.config.api_dev_key, | ||
api_paste_key : pasteID | ||
}; | ||
if (_this.config.api_user_key) { | ||
p.api_user_key = _this.config.api_user_key; | ||
_this | ||
._postApi(conf.net.protocol + conf.net.base + conf.net.endpoint.post, p) | ||
.then(deferred.resolve) | ||
.fail(deferred.reject); | ||
} else if (_this.config.api_user_name !== null && _this.config.api_user_password !== null) { | ||
_this | ||
.createAPIuserKey() | ||
.then(function () { | ||
_this | ||
.deletePaste(pasteID) | ||
.then(deferred.resolve) | ||
.fail(deferred.reject); | ||
}); | ||
if (this.config.api_user_key) { | ||
params.api_user_key = this.config.api_user_key; | ||
this._postApi(conf.net.protocol + conf.net.base + conf.net.endpoint.post, params) | ||
.then(deferred.resolve) | ||
.fail(deferred.reject); | ||
} else if (this.config.api_user_name !== null && this.config.api_user_password !== null) { | ||
this.createAPIuserKey() | ||
.then(function () { | ||
this.deletePaste(pasteID) | ||
.then(deferred.resolve) | ||
.fail(deferred.reject); | ||
}.bind(this)); | ||
} else { | ||
@@ -221,10 +207,8 @@ deferred.reject(new Error('Error! Deleting a paste created by the user needs username and password')); | ||
Pastebin.prototype.createAPIuserKey = function () { | ||
var _this = this, | ||
deferred = Q.defer(); | ||
debug('[pastebin-js::createAPIuserKey] called'); | ||
var deferred = Q.defer(); | ||
_this | ||
._getRequired(['api_dev_key', 'api_user_name', 'api_user_password']) | ||
this._getRequired(['api_dev_key', 'api_user_name', 'api_user_password']) | ||
.then(function (params) { | ||
_this | ||
._postApi(conf.net.protocol + conf.net.base + conf.net.endpoint.login, params) | ||
this._postApi(conf.net.protocol + conf.net.base + conf.net.endpoint.login, params) | ||
.then(function (data) { | ||
@@ -234,8 +218,8 @@ if (data.length !== 32) { | ||
} else { | ||
_this.config.api_user_key = data; | ||
this.config.api_user_key = data; | ||
deferred.resolve(true); | ||
} | ||
}) | ||
}.bind(this)) | ||
.fail(deferred.reject); | ||
}) | ||
}.bind(this)) | ||
.fail(deferred.reject); | ||
@@ -251,34 +235,26 @@ | ||
Pastebin.prototype.listUserPastes = function (limit) { | ||
var _this = this, | ||
deferred = Q.defer(), | ||
params = {}, | ||
l = limit || 50; | ||
var deferred = Q.defer(); | ||
var l = limit || 50; | ||
if (!_this.config.api_user_key) { | ||
_this | ||
.createAPIuserKey() | ||
.then(function () { | ||
_this | ||
.listUserPastes(limit) | ||
.then(deferred.resolve) | ||
.fail(deferred.reject); | ||
}) | ||
.fail(deferred.reject); | ||
if (!this.config.api_user_key) { | ||
this.createAPIuserKey() | ||
.then(function () { | ||
this.listUserPastes(limit) | ||
.then(deferred.resolve) | ||
.fail(deferred.reject); | ||
}.bind(this)) | ||
.fail(deferred.reject); | ||
} else { | ||
params.api_dev_key = _this.config.api_dev_key; | ||
params.api_user_key = _this.config.api_user_key; | ||
params.api_results_limit = l; | ||
params.api_option = 'list'; | ||
_this | ||
._postApi(conf.net.protocol + conf.net.base + conf.net.endpoint.post, params) | ||
var params = { | ||
api_dev_key : this.config.api_dev_key, | ||
api_user_key : this.config.api_user_key, | ||
api_results_limit : l, | ||
api_option : 'list' | ||
}; | ||
this._postApi(conf.net.protocol + conf.net.base + conf.net.endpoint.post, params) | ||
.then(function (data) { | ||
_this | ||
._parsePastes(data) | ||
.then(deferred.resolve) | ||
.fail(deferred.reject); | ||
}) | ||
this._parsePastes(data) | ||
.then(deferred.resolve) | ||
.fail(deferred.reject); | ||
}.bind(this)) | ||
.fail(deferred.reject); | ||
@@ -294,18 +270,17 @@ } | ||
Pastebin.prototype.listTrendingPastes = function () { | ||
var _this = this, | ||
deferred = Q.defer(), | ||
params = {}; | ||
debug('[pastebin-js::listTrendingPastes] called'); | ||
params.api_option = 'trends'; | ||
params.api_dev_key = _this.config.api_dev_key; | ||
var deferred = Q.defer(); | ||
var params = { | ||
api_option : 'trends', | ||
api_dev_key : this.config.api_dev_key | ||
}; | ||
_this | ||
._postApi(conf.net.protocol + conf.net.base + conf.net.endpoint.post, params) | ||
.then(function (data) { | ||
_this | ||
._parsePastes(data) | ||
.then(deferred.resolve) | ||
.fail(deferred.reject); | ||
}) | ||
.fail(deferred.reject); | ||
this._postApi(conf.net.protocol + conf.net.base + conf.net.endpoint.post, params) | ||
.then(function (data) { | ||
this._parsePastes(data) | ||
.then(deferred.resolve) | ||
.fail(deferred.reject); | ||
}.bind(this)) | ||
.fail(deferred.reject); | ||
@@ -319,30 +294,26 @@ return deferred.promise; | ||
Pastebin.prototype.getUserInfo = function () { | ||
var _this = this, | ||
deferred = Q.defer(), | ||
params = {}; | ||
debug('[pastebin-js::getUserInfo] called'); | ||
params.api_option = 'userdetails'; | ||
params.api_dev_key = _this.config.api_dev_key; | ||
var deferred = Q.defer(); | ||
var params = { | ||
api_option : 'userdetails', | ||
api_dev_key : this.config.api_dev_key | ||
}; | ||
if (!_this.config.api_user_key) { | ||
_this | ||
.createAPIuserKey() | ||
.then(function () { | ||
_this | ||
.getUserInfo() | ||
.then(deferred.resolve) | ||
.fail(deferred.reject); | ||
}) | ||
.fail(deferred.reject); | ||
if (!this.config.api_user_key) { | ||
this.createAPIuserKey() | ||
.then(function () { | ||
this.getUserInfo() | ||
.then(deferred.resolve) | ||
.fail(deferred.reject); | ||
}.bind(this)) | ||
.fail(deferred.reject); | ||
} else { | ||
params.api_user_key = _this.config.api_user_key; | ||
_this | ||
._postApi(conf.net.protocol + conf.net.base + conf.net.endpoint.post, params) | ||
params.api_user_key = this.config.api_user_key; | ||
this._postApi(conf.net.protocol + conf.net.base + conf.net.endpoint.post, params) | ||
.then(function (data) { | ||
_this | ||
._parseUser(data) | ||
this._parseUser(data) | ||
.then(deferred.resolve) | ||
.fail(deferred.reject); | ||
}) | ||
}.bind(this)) | ||
.fail(deferred.reject); | ||
@@ -358,11 +329,9 @@ } | ||
Pastebin.prototype._parsePastes = function (xml) { | ||
var _this = this, | ||
deferred = Q.defer(); | ||
var deferred = Q.defer(); | ||
_this | ||
._parseXML(xml) | ||
this._parseXML(xml) | ||
.then(function (data) { | ||
if (data) { | ||
var rootObj = data.paste, | ||
normalize = _.map(rootObj, function (paste) { | ||
var rootObj = data.paste; | ||
var normalize = _.map(rootObj, function (paste) { | ||
var obj = {}; | ||
@@ -374,3 +343,2 @@ _.map(_.keys(paste), function (key) { | ||
}); | ||
deferred.resolve(normalize); | ||
@@ -390,14 +358,10 @@ } else { | ||
Pastebin.prototype._parseUser = function (xml) { | ||
var _this = this, | ||
deferred = Q.defer(); | ||
var deferred = Q.defer(); | ||
_this | ||
._parseXML(xml) | ||
this._parseXML(xml) | ||
.then(function (data) { | ||
if (data) { | ||
var rootObj = data.user[0], | ||
normalize = {}; | ||
var rootObj = data.user[0]; | ||
var normalize = {}; | ||
_.each(Object.keys(rootObj), function (key) { normalize[key] = rootObj[key][0]; }); | ||
deferred.resolve(normalize); | ||
@@ -417,12 +381,14 @@ } else { | ||
Pastebin.prototype._parseXML = function (xml) { | ||
var _this = this, | ||
deferred = Q.defer(), | ||
xmlString = '', | ||
parser = new xml2js.Parser({ | ||
'trim' : true, | ||
'explicitRoot' : false | ||
}); | ||
debug('[pastebin-js::_parseXML] called'); | ||
var deferred = Q.defer(); | ||
var xmlString = ''; | ||
var parser = new xml2js.Parser({ | ||
'trim' : true, | ||
'explicitRoot' : false | ||
}); | ||
if (!xml) { | ||
deferred.reject(new Error('No xml provided!')); | ||
return deferred.promise; | ||
} | ||
@@ -446,11 +412,15 @@ | ||
Pastebin.prototype._getRequired = function (paramlist) { | ||
var _this = this, | ||
deferred = Q.defer(), | ||
ret = {}; | ||
debug('[pastebin-js::_getRequired] called with ' + paramlist); | ||
ret = _.pick(_this.config, paramlist); | ||
var deferred = Q.defer(); | ||
var ret = _.filter(paramlist, function(param) { | ||
return this.config[param] !== null; | ||
}.bind(this)); | ||
if (Object.keys(ret).length !== paramlist.length) { | ||
deferred.reject(new Error('Missing parameters! ' + _.difference(paramlist, Object.keys(ret)))); | ||
debug('[pastebin-js::_getRequired] reject'); | ||
deferred.reject(new Error('Missing parameters! ' + _.difference(paramlist, ret))); | ||
} else { | ||
debug('[pastebin-js::_getRequired] resolved'); | ||
ret = _.pick(this.config, paramlist); | ||
deferred.resolve(ret); | ||
@@ -457,0 +427,0 @@ } |
@@ -18,2 +18,10 @@ 'use strict'; | ||
}, | ||
shell: { | ||
options: { | ||
stderr: false | ||
}, | ||
shrinkwrap : { | ||
command: 'npm-shrinkwrap' | ||
} | ||
}, | ||
watch : { | ||
@@ -23,12 +31,9 @@ jshint : { | ||
tasks: ['jshint'] | ||
}, | ||
simplemocha : { | ||
files : '<%= jshint.all %>' | ||
} | ||
}, | ||
simplemocha: { | ||
options: { | ||
globals: ['expect'], | ||
timeout: 3000, | ||
ignoreLeaks: false, | ||
ui: 'bdd', | ||
reporter: 'tap' | ||
}, | ||
options: {}, | ||
all: { src: ['tests/*.js'] } | ||
@@ -41,2 +46,3 @@ } | ||
grunt.loadNpmTasks('grunt-simple-mocha'); | ||
grunt.loadNpmTasks('grunt-shell'); | ||
@@ -47,4 +53,5 @@ // Default task. | ||
grunt.registerTask('test', ['jshint', 'simplemocha']); | ||
grunt.registerTask('build', ['test', 'shell:shrinkwrap']); | ||
grunt.registerTask('dev', ['jshint', 'watch']); | ||
grunt.registerTask('dev', ['test', 'watch']); | ||
}; |
@@ -1,7 +0,11 @@ | ||
0.2.0 / 2014-09-02 | ||
[0.3.0](https://github.com/j3lte/pastebin-js/releases/tag/v0.3.0) / 2014-12-19 | ||
================== | ||
* Refactoring all the _this && this | ||
0.2.0-[0.2.1](https://github.com/j3lte/pastebin-js/releases/tag/v0.2.1) / 2014-09-02 | ||
================== | ||
* Adding tests && Travis | ||
0.1.6 / 2014-07-03 | ||
@@ -8,0 +12,0 @@ ================== |
@@ -0,0 +0,0 @@ 'use strict'; |
@@ -0,0 +0,0 @@ 'use strict'; |
@@ -9,18 +9,20 @@ 'use strict'; | ||
*/ | ||
var request = require('request'), | ||
config = require('./config'), | ||
Q = require('q'), | ||
TIMEOUT = 4000, | ||
HEADERS = { | ||
'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/31.0.1650.57 Safari/537.36', | ||
'Cache-Control:' : 'no-cache' | ||
}; | ||
var request = require('request'); | ||
var config = require('./config'); | ||
var pkg = require('../package'); | ||
var Q = require('q'); | ||
var TIMEOUT = 4000; | ||
var HEADERS = { | ||
'User-Agent': 'Pastebin-js/' + pkg.version, | ||
'Cache-Control:' : 'no-cache' | ||
}; | ||
var methods = module.exports = { | ||
get : function (path, params) { | ||
var _this = this, | ||
deferred = Q.defer(); | ||
console.log('[pastebin-js::GET] : ' + path + ' params: ' + params); | ||
var deferred = Q.defer(); | ||
if (!path) { | ||
deferred.reject(new Error('No path provided')); | ||
return deferred.promise; | ||
} | ||
@@ -53,3 +55,3 @@ if (!params) { | ||
if (body.indexOf('Bad API request') !== -1) { | ||
deferred.reject(new Error('Error: ' + body)); | ||
deferred.reject(new Error(body)); | ||
} | ||
@@ -62,7 +64,8 @@ deferred.resolve(body); | ||
post : function (path, params, callback) { | ||
var _this = this, | ||
deferred = Q.defer(); | ||
console.log('[pastebin-js::POST] : ' + path + ' params: ' + JSON.stringify(params)); | ||
var deferred = Q.defer(); | ||
if (!path) { | ||
deferred.reject(new Error('No path provided')); | ||
return deferred.promise; | ||
} | ||
@@ -94,2 +97,5 @@ if (!params) { | ||
} | ||
if (body.indexOf('Post limit') !== -1) { | ||
deferred.reject(new Error('Error: ' + body)); | ||
} | ||
if (body.indexOf('http') === 0) { | ||
@@ -96,0 +102,0 @@ // return an ID instead of the url |
{ | ||
"name": "pastebin-js", | ||
"version": "0.2.1", | ||
"npm-shrinkwrap-version": "3.1.8", | ||
"version": "0.3.0", | ||
"npm-shrinkwrap-version": "5.1.0", | ||
"dependencies": { | ||
@@ -6,0 +6,0 @@ "q": { |
{ | ||
"name": "pastebin-js", | ||
"version": "0.2.1", | ||
"version": "0.3.0", | ||
"description": "NodeJS module for Pastebin API", | ||
@@ -27,2 +27,3 @@ "main": "index", | ||
"grunt-jsdoc-to-markdown": "*", | ||
"grunt-shell": "^1.1.1", | ||
"grunt-simple-mocha": "*", | ||
@@ -45,3 +46,8 @@ "jshint-stylish": "*", | ||
}, | ||
"license": "MIT" | ||
"licenses": [ | ||
{ | ||
"type": "MIT", | ||
"url": "https://raw.githubusercontent.com/j3lte/pastebin-js/master/LICENSE" | ||
} | ||
] | ||
} |
@@ -1,2 +0,2 @@ | ||
pastebin-js [![Build Status](https://travis-ci.org/j3lte/pastebin-js.svg?branch=master)](https://travis-ci.org/j3lte/pastebin-js) | ||
pastebin-js | ||
=========== | ||
@@ -6,2 +6,8 @@ | ||
[![Build Status](https://travis-ci.org/j3lte/pastebin-js.svg?branch=master)](https://travis-ci.org/j3lte/pastebin-js) | ||
[![DAVID](https://david-dm.org/j3lte/pastebin-js.png)](https://david-dm.org/j3lte/pastebin-js) | ||
[![npm version](https://badge.fury.io/js/pastebin-js.svg)](http://badge.fury.io/js/pastebin-js) | ||
[![Development Dependency Status](https://david-dm.org/j3lte/pastebin-js/dev-status.svg?theme=shields.io)](https://david-dm.org/j3lte/pastebin-js#info=devDependencies) | ||
[![Code Climate](https://codeclimate.com/github/j3lte/pastebin-js/badges/gpa.svg)](https://codeclimate.com/github/j3lte/pastebin-js) | ||
NodeJS module to access the Pastebin API | ||
@@ -29,3 +35,3 @@ | ||
```js | ||
var PastebinAPI = require('./index'), | ||
var PastebinAPI = require('pastebin-js'), | ||
pastebin = new PastebinAPI({ | ||
@@ -53,3 +59,3 @@ 'api_dev_key' : 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx', | ||
```js | ||
var PastebinAPI = require('./index'); | ||
var PastebinAPI = require('pastebin-js'); | ||
@@ -56,0 +62,0 @@ // Without any parameter you can only use getPaste! |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
40773
148
10
937