Skip to content

Commit

Permalink
Added delete, update, and get credit methods, created tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
sparkyfen committed Dec 2, 2014
1 parent 685f9a4 commit daed46b
Show file tree
Hide file tree
Showing 6 changed files with 259 additions and 12 deletions.
12 changes: 10 additions & 2 deletions .gitignore
Expand Up @@ -153,11 +153,19 @@ pip-log.txt
.coverage
.tox

#Translations
# Translations
*.mo

#Mr Developer
# Mr Developer
.mr.developer.cfg

# Mac crap
.DS_Store


############
## NodeJS
############

# Modules directory
node_modules/
79 changes: 79 additions & 0 deletions Gruntfile.js
@@ -0,0 +1,79 @@
module.exports = function (grunt) {
'use strict';
// Project configuration
grunt.initConfig({
// Metadata
pkg: grunt.file.readJSON('package.json'),
banner: '/*! <%= pkg.name %> - v<%= pkg.version %> - ' +
'<%= grunt.template.today("yyyy-mm-dd") %>\n' +
'<%= pkg.homepage ? "* " + pkg.homepage + "\\n" : "" %>' +
'* Copyright (c) <%= grunt.template.today("yyyy") %> <%= pkg.author.name %>;' +
' Licensed <%= pkg.licenses[0].type %> */\n',
// Task configuration
concat: {
options: {
banner: '<%= banner %>',
stripBanners: true
},
dist: {
src: ['lib/**/*.js'],
dest: 'dist/imgur.js'
}
},
uglify: {
options: {
banner: '<%= banner %>'
},
dist: {
src: '<%= concat.dist.dest %>',
dest: 'dist/imgur.min.js'
}
},
jshint: {
options: {
node: true,
curly: true,
eqeqeq: true,
immed: true,
latedef: true,
newcap: true,
noarg: true,
sub: true,
undef: true,
unused: true,
eqnull: true,
boss: true
},
gruntfile: {
src: 'gruntfile.js'
},
lib_test: {
src: ['lib/**/*.js', 'test/**/*.js']
}
},
nodeunit: {
files: ['test/**/*.spec.js']
},
watch: {
gruntfile: {
files: '<%= jshint.gruntfile.src %>',
tasks: ['jshint:gruntfile']
},
lib_test: {
files: '<%= jshint.lib_test.src %>',
tasks: ['jshint:lib_test', 'nodeunit']
}
}
});

// These plugins provide necessary tasks
grunt.loadNpmTasks('grunt-contrib-concat');
grunt.loadNpmTasks('grunt-contrib-uglify');
grunt.loadNpmTasks('grunt-contrib-nodeunit');
grunt.loadNpmTasks('grunt-contrib-jshint');
grunt.loadNpmTasks('grunt-contrib-watch');

// Default task
grunt.registerTask('default', ['jshint', 'nodeunit', 'concat', 'uglify']);
};

74 changes: 64 additions & 10 deletions lib/imgur.js
Expand Up @@ -8,9 +8,8 @@

'use strict';

var https = require('https'),
fs = require('fs'),
request = require('request');
var fs = require('fs');
var request = require('request');
var request = request.defaults({
json: true
});
Expand All @@ -20,18 +19,18 @@ var imgur = {
setClientID : function(clientID){
this._clientID = clientID;
},
upload : function(_file,_cb) {
if(this._clientID && _file){
upload: function(_file,_cb) {
if(this._clientID && _file) {
var options = {
url: 'https://api.imgur.com/3/upload',
headers: {
'Authorization': 'Client-ID ' + this._clientID
}
};
var post = request.post(options, function(err, req, body){
if(err)
_cb(err);

var post = request.post(options, function (err, req, body){
if(err) {
return _cb(err);
}
_cb(null, body);
});

Expand All @@ -44,8 +43,63 @@ var imgur = {
upload.append('image', fs.createReadStream(_file));
}
}
},
delete: function(_id, _cb) {
if(this._clientID && _id) {
var options = {
url: 'https://api.imgur.com/3/image/' + _id,
headers: {
'Authorization': 'Client-ID ' + this._clientID
}
};
request.del(options, function (err, req, body) {
if(err) {
return _cb(err);
}
_cb(null, body);
});
}
},
update: function(_params, _cb) {
if(this._clientID && _params.id && (_params.title || _params.description)) {
var options = {
url: 'https://api.imgur.com/3/image/' + _params.id,
headers: {
'Authorization': 'Client-ID ' + this._clientID
},
form: {
title: _params.title ? _params.title : null,
description: _params.description ? _params.description : null
}
};
request.post(options, function (err, req, body) {
if(err) {
return _cb(err);
}
_cb(null, body);
});
}
},
getCredits: function(_cb) {
if(this._clientID) {
var options = {
url: 'https://api.imgur.com/3/credits',
headers: {
'Authorization': 'Client-ID ' + this._clientID
}
};
request(options, function (err, req, body) {
if(err) {
return _cb(err);
}
_cb(null, body);
});
}
}
};

exports.upload = imgur.upload;
exports.setClientID = imgur.setClientID;
exports.upload = imgur.upload;
exports.update = imgur.update;
exports.delete = imgur.delete;
exports.getCredits = imgur.getCredits;
16 changes: 16 additions & 0 deletions package.json
Expand Up @@ -7,6 +7,10 @@
"name": "jamiees2",
"email": "jamiees2@gmail.com"
},
"contributors": {
"name": "brutalhonesty",
"email": "sparky1010+github@gmail.com"
},
"repository": {
"type": "git",
"url": "git://github.com/jamiees2/imgur-node-api.git"
Expand All @@ -32,5 +36,17 @@
],
"dependencies": {
"request": "~2.16.6"
},
"devDependencies": {
"grunt": "~0.4.2",
"grunt-contrib-concat": "~0.3.0",
"grunt-contrib-jshint": "~0.7.2",
"grunt-contrib-nodeunit": "~0.4.1",
"grunt-contrib-uglify": "~0.2.7",
"grunt-contrib-watch": "~0.5.3",
"nock": "^0.51.0"
},
"scripts": {
"test": "grunt nodeunit"
}
}
Binary file added test/W0JfyHW.gif
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
90 changes: 90 additions & 0 deletions test/imgur.spec.js
@@ -0,0 +1,90 @@
'use strict';

var nock = require('nock');
var path = require('path');
var imgur = require('../lib/imgur');
var clientId = 'myTestId';
var imageId = 'W0JfyHW';

exports.setUp = function(callback) {
imgur.setClientID(clientId);
nock('https://api.imgur.com').post('/3/upload').reply(200, {
success: true,
status: 200,
data: {
link: 'http://i.imgur.com/'+imageId+'.gif'
}
});
nock('https://api.imgur.com').get('/3/credits').reply(200, {
data: {
UserLimit: 500,
UserRemaining: 500,
UserReset: 1417550317,
ClientLimit: 12500,
ClientRemaining: 12268
},
success: true,
status: 200
});
nock('https://api.imgur.com').post('/3/image/' + imageId, {
title: 'MyTitle',
description: 'MyDescription'
}).reply(200, {
success: true,
status: 200
});
nock('https://api.imgur.com').delete('/3/image/' + imageId).reply(200, {
success: true,
status: 200
});
callback();
};

exports.testUrlUpload = function(test) {
imgur.upload('http://i.imgur.com/'+imageId+'.gif', function (error, res) {
test.equal(error, null);
test.ok(res.success, 'Should be a successful upload.');
test.equal(res.status, 200);
test.done();
});
};

exports.testFileUpload = function(test) {
imgur.upload(path.join(__dirname, (imageId + '.gif')), function (error, res) {
test.equal(error, null);
test.ok(res.success, 'Should be a successful upload.');
test.equal(res.status, 200);
test.done();
});
};

exports.delete = function(test) {
imgur.delete(imageId, function (error, res) {
test.equal(error, null);
test.ok(res.success, 'Should successfully delete the image.');
test.equal(res.status, 200);
test.done();
});
};

exports.update = function(test) {
imgur.update({
id: imageId,
title: 'MyTitle',
description: 'MyDescription',
}, function (error, res) {
test.equal(error, null);
test.ok(res.success, 'Should successfully update the image.');
test.equal(res.status, 200);
test.done();
});
};

exports.getCredits = function(test) {
imgur.getCredits(function (error, res) {
test.equal(error, null);
test.ok(res.success, 'Should successfully return the credits left.');
test.equal(res.status, 200);
test.done();
});
};

0 comments on commit daed46b

Please sign in to comment.