Skip to content

Instantly share code, notes, and snippets.

@xbenjii

xbenjii/ES5 Secret

Last active August 29, 2015 14:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save xbenjii/d83cfbfc64cf55631398 to your computer and use it in GitHub Desktop.
Save xbenjii/d83cfbfc64cf55631398 to your computer and use it in GitHub Desktop.
var request = require('request-promise'),
fs = require('fs'),
path = require('path');
var Imgur = function(options) {
this.options = {};
if(typeof options === 'undefined') {
throw new Error('No options specified');
}
if (typeof options.clientId === 'undefined' || !options.clientId) {
throw new Error('No client ID specified, register one at "https://api.imgur.com/oauth2/addclient"');
}
if (typeof options.clientSecret !== 'undefined') {
this.options.clientSecret = options.clientSecret;
}
this.options.clientId = options.clientId;
this.options.version = options.version || 3;
this.options.endPoint = 'https://api.imgur.com/' + this.options.version + '/';
fs
.readdirSync(path.join(__dirname, './endpoints'))
.forEach(function(file) {
if (~file.indexOf('.js')) {
this[file.substring(0, file.indexOf('.'))] = require(path.join(__dirname, './endpoints', file))(this);
}
}.bind(this));
};
Imgur.prototype.request = function(method, path, params) {
params = params || {};
if (typeof method === 'undefined' || ['get', 'post', 'head', 'delete'].indexOf(method.toLowerCase()) === -1) {
throw new Error('no method specified or method isn\'t in [get, post, head, delete]');
}
var options = {
method: method,
headers: {
Authorization: 'Client-ID ' + this.options.clientId
},
json: true
};
if (typeof params.data !== 'undefined' && method.toLowerCase() !== 'get') {
options.form = params.data;
}
if (typeof params.file !== 'undefined') {
options.formData = params.file;
}
if (typeof params.access_token !== 'undefined') {
options.headers.Authorization = 'Bearer ' + params.access_token;
}
for (var key in params) {
if (['data', 'file', 'access_token'].indexOf(key) != -1)
continue;
if (params.hasOwnProperty(key)) {
path = path.replace(new RegExp('\{' + key + '\}', 'g'), params[key]);
}
}
options.url = /^https?:\/\//.test(path) ? path : this.options.endPoint + path;
return request(options);
};
module.exports = Imgur;
import readDirSync from 'fs';
import join from 'path';
import * as request from 'request-promise';
class Imgur {
constructor(options = {}) {
this.options = options;
if (typeof options.clientId === 'undefined' || !options.clientId) {
throw new Error('No client ID specified, register one at "https://api.imgur.com/oauth2/addclient"');
}
this.options.version = options.version || 3;
this.options.endPoint = `https://api.imgur.com/${this.options.version}`;
readDirSync(join(__dirname, './endpoints'))
.forEach(file => {
if (~file.indexOf('.js'))
this[file.substring(0, file.indexOf('.'))] = require(join(__dirname, './endpoints', file))(this);
});
}
request(method, path, params = {}) {
if (typeof method === 'undefined' || ['get', 'post', 'head', 'delete'].indexOf(method.toLowerCase()) === -1) {
throw new Error('no method specified or method isn\'t in [get, post, head, delete]');
}
var options = {
method: method,
headers: {
Authorization: `Client-ID ${this.options.clientId}`
},
json: true
};
for(let key in params) {
switch(key) {
case 'data':
if(method.toLowerCase() !== 'get')
options.form = params.data;
break;
case 'file':
options.formData = params.file;
break;
case 'access_token':
options.headers.Authorization = `Bearer ${params.access_token}`;
break;
default:
if (params.hasOwnProperty(key))
path = path.replace(new RegExp(`\{${key}\}`, g), params[key]);
break;
}
}
options.url = /^https?:\/\//.test(path) ? path : this.options.endPoint + path;
return request(options);
}
}
export default Imgur;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment