Skip to content
This repository has been archived by the owner on Sep 1, 2019. It is now read-only.

Commit

Permalink
Basic implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
xrajishx committed Aug 18, 2017
1 parent 5a85ff4 commit bd311bb
Show file tree
Hide file tree
Showing 6 changed files with 164 additions and 5 deletions.
2 changes: 1 addition & 1 deletion .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ insert_final_newline = true

# Matches multiple files with brace expansion notation
# Set default charset
[*.{js,json}]
[*.{js}]
charset = utf-8
indent_style = space
indent_size = 2
1 change: 1 addition & 0 deletions .eslintrc
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
env:
es6: true
node: true
mocha: true
extends: 'eslint:recommended'
parserOptions:
sourceType: module
Expand Down
1 change: 0 additions & 1 deletion index.js

This file was deleted.

14 changes: 11 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
"name": "maildrop-api",
"version": "1.0.0",
"description": "Wrapper around maildrop API",
"main": "index.js",
"main": "src/maildrop.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
"test": "node node_modules/mocha/bin/mocha"
},
"repository": {
"type": "git",
Expand All @@ -18,5 +18,13 @@
"bugs": {
"url": "https://github.com/xrajishx/maildrop-api/issues"
},
"homepage": "https://github.com/xrajishx/maildrop-api#readme"
"homepage": "https://github.com/xrajishx/maildrop-api#readme",
"dependencies": {
"request": "^2.81.0"
},
"devDependencies": {
"chai": "^4.1.1",
"chai-as-promised": "^7.1.1",
"mocha": "^3.5.0"
}
}
93 changes: 93 additions & 0 deletions src/maildrop.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
const request = require('request');
var exports = module.exports = {};

const baseUrl = process.env.MAILDROP_URL || 'http://maildrop.cc';

exports.suggestion = () => {
return new Promise((resolve, reject) => {
let options = {
method: 'GET',
url: baseUrl + '/api/suggestion',
json: true,
headers: { 'cache-control': 'no-cache' }
};

request(options, (error, response, body) => {
if(error) return reject(error);
return resolve(body.suggestion);
});
});
};

exports.blocked = () => {
return new Promise((resolve, reject) => {
let options = {
method: 'GET',
url: baseUrl + '/api/blocked',
json: true,
headers: { 'cache-control': 'no-cache' }
};

request(options, (error, response, body) => {
if(error) return reject(error);
return resolve(body.blocked);
});
});
};

exports.getInbox = (mailBox) => {
return new Promise((resolve, reject) => {
if(!mailBox) return reject(new Error('mailBox required.'));

let options = {
method: 'GET',
url: baseUrl + '/api/inbox/' + mailBox,
json: true,
headers: { 'cache-control': 'no-cache' }
};

request(options, (error, response, body) => {
if(error) return reject(error);
return resolve(body);
});
});
};

exports.getEmail = (mailBox, emailId) => {
return new Promise((resolve, reject) => {
if(!(mailBox && emailId)) return reject(new Error('mailBox required.'));

let options = {
method: 'GET',
url: baseUrl + '/api/inbox/' + mailBox + '/' + emailId,
json: true,
headers: { 'cache-control': 'no-cache' }
};

request(options, (error, response, body) => {
if(error) return reject(error);
return resolve(body);
});
});
};

exports.deleteEmail = (mailBox, emailId) => {
return new Promise((resolve, reject) => {
if(!(mailBox && emailId)) return reject(
new Error('mailBox and emailId required.')
);

let options = {
method: 'DELETE',
url: baseUrl + '/api/inbox/' + mailBox + '/' + emailId,
json: true,
headers: { 'cache-control': 'no-cache' },
followRedirect: true
};

request(options, (error, response, body) => {
if(error) return reject(error);
return resolve(body);
});
});
};
58 changes: 58 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
const chai = require('chai');
const chaiAsPromised = require('chai-as-promised');
const path = require('path');

const maildrop = require(path.join(__dirname, '..', 'src', 'maildrop'));

chai.use(chaiAsPromised);
chai.should();

const mailBox = 'f';

describe('suggestion()', function() {
it('should return without error', function() {
return maildrop.suggestion().should.eventually.be.fulfilled;
});
});

describe('blocked()', function() {
it('should return without error', function() {
return maildrop.blocked().should.eventually.be.fulfilled;
});
});

describe('getInbox(mailBox)', function() {
it('should be fulfilled without error', function() {
return maildrop.getInbox(mailBox).should.eventually.be.fulfilled;
});
});

describe('getInbox()', function() {
it('should be rejected with error', function() {
return maildrop.getInbox().should.eventually.be.rejected;
});
});

describe('getEmail(mailBox, mailBox)', function() {
it('should be fulfilled without error', function() {
return maildrop.getEmail(mailBox, mailBox).should.eventually.be.fulfilled;
});
});

describe('getEmail()', function() {
it('should be rejected with error', function() {
return maildrop.getEmail().should.eventually.be.rejected;
});
});

describe('deleteEmail(mailBox, mailBox)', function() {
it('should be fulfilled without error', function() {
return maildrop.deleteEmail(mailBox, mailBox).should.eventually.be.fulfilled;
});
});

describe('deleteEmail()', function() {
it('should be rejected with error', function() {
return maildrop.deleteEmail().should.eventually.be.rejected;
});
});

0 comments on commit bd311bb

Please sign in to comment.