Skip to content

Commit

Permalink
remove unnecessary code
Browse files Browse the repository at this point in the history
  • Loading branch information
jumtech authored and jumtech committed Aug 31, 2017
1 parent 5905dab commit e390b46
Show file tree
Hide file tree
Showing 10 changed files with 39 additions and 115 deletions.
13 changes: 4 additions & 9 deletions app.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,3 @@
/*
* decaffeinate suggestions:
* DS102: Remove unnecessary code created because of implicit returns
* Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
*/
const express = require('express');
const path = require('path');
const favicon = require('serve-favicon');
Expand All @@ -25,20 +20,20 @@ app.use(express.static(path.join(__dirname, 'public')));
app.use('/', routes);
app.use('/bot', bot);
// catch 404 and forward to error handler
app.use(function(req, res, next) {
app.use((req, res, next) => {
const err = new Error('Not Found');
err.status = 404;
next(err);
});
app.use(function(err, req, res, next) {
app.use((err, req, res, next) => {
console.log('err', err);
return next();
});
// error handlers
// development error handler
// will print stacktrace
if (app.get('env') === 'development') {
app.use(function(err, req, res, next) {
app.use((err, req, res, next) => {
res.status(err.status || 500);
res.render('error', {
message: err.message,
Expand All @@ -49,7 +44,7 @@ if (app.get('env') === 'development') {
}
// production error handler
// no stacktraces leaked to user
app.use(function(err, req, res, next) {
app.use((err, req, res, next) => {
res.status(err.status || 500);
res.render('error', {
message: err.message,
Expand Down
13 changes: 4 additions & 9 deletions bin/www.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,3 @@
/*
* decaffeinate suggestions:
* DS102: Remove unnecessary code created because of implicit returns
* Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
*/
/**
* Module dependencies.
*/
Expand All @@ -15,7 +10,7 @@ const http = require('http');
* Normalize a port into a number, string, or false.
*/

const normalizePort = function(val) {
const normalizePort = (val) => {
let port;
port = parseInt(val, 10);
if (isNaN(port)) {
Expand All @@ -40,7 +35,7 @@ const port = normalizePort(process.env.PORT || '5000');
* Event listener for HTTP server "error" event.
*/

const onError = function(error) {
const onError = (error) => {
if (error.syscall !== 'listen') {
throw error;
}
Expand All @@ -64,7 +59,7 @@ const onError = function(error) {
* Event listener for HTTP server "listening" event.
*/

const onListening = function() {
const onListening = () => {
const addr = server.address();
const bind = typeof addr === 'string' ? `pipe ${addr}` : `port ${addr.port}`;
debug(`Listening on ${bind}`);
Expand All @@ -76,7 +71,7 @@ app.set('port', port);
* Create HTTP server.
*/

var server = http.createServer(app);
const server = http.createServer(app);

/**
* Listen on provided port, on all network interfaces.
Expand Down
13 changes: 3 additions & 10 deletions logics/_a3rt.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,8 @@
/*
* decaffeinate suggestions:
* DS102: Remove unnecessary code created because of implicit returns
* DS207: Consider shorter variations of null checks
* Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
*/

const request = require('request');

module.exports = function(text, cb) {
module.exports = (text, cb) => {

if (cb == null) { cb = function(){}; }
if (!cb) { cb = () => {}; }
const {A3RT_API_KEY} = require('../config');

return request.post({
Expand All @@ -18,7 +11,7 @@ module.exports = function(text, cb) {
apikey: A3RT_API_KEY,
query: text
}
}, function(err, res, body) {
}, (err, res, body) => {

if (err) {
console.log(err);
Expand Down
16 changes: 5 additions & 11 deletions logics/_search.js
Original file line number Diff line number Diff line change
@@ -1,28 +1,22 @@
/*
* decaffeinate suggestions:
* DS102: Remove unnecessary code created because of implicit returns
* DS207: Consider shorter variations of null checks
* Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
*/
const algoliasearch = require('algoliasearch');

module.exports = function({text, count}, cb) {
if (cb == null) { cb = function(){}; }
return _search_by_algolia({text, count}, function(err, result) {
module.exports = ({text, count}, cb) => {
if (!cb) { cb = () => {}; }
return _search_by_algolia({text, count}, (err, result) => {
if (err) { return cb(err); }
return cb(null, result);
});
};

var _search_by_algolia = function({text, count}, cb) {
const _search_by_algolia = ({text, count}, cb) => {
const {ALGOLIA_APPLICATION_ID, ALGOLIA_API_KEY, ALGOLIA_INDEX_NAME} = require('../config');
const ag_client = algoliasearch(ALGOLIA_APPLICATION_ID, ALGOLIA_API_KEY);
const ag_index = ag_client.initIndex(ALGOLIA_INDEX_NAME);

return ag_index.search({
query: text,
hitsPerPage: count
}, function(err, content) {
}, (err, content) => {
if (err) { return cb(err); }
const result = {
count: 0,
Expand Down
13 changes: 3 additions & 10 deletions logics/_send.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,8 @@
/*
* decaffeinate suggestions:
* DS102: Remove unnecessary code created because of implicit returns
* DS207: Consider shorter variations of null checks
* Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
*/

const request = require('request');

module.exports = function(path, body, cb) {
module.exports = (path, body, cb) => {

if (cb == null) { cb = function(){}; }
if (!cb) { cb = () => {}; }
const {KARTE_URL, KARTE_BOT_APPLICATION_KEY} = require('../config');

const public_key = KARTE_BOT_APPLICATION_KEY;
Expand All @@ -21,7 +14,7 @@ module.exports = function(path, body, cb) {
'Content-Type': 'application/json',
'X-KARTE-App-Key': `${public_key}`
}
}, function(err, res, body) {
}, (err, res, body) => {

if (err) {
console.log(err);
Expand Down
15 changes: 4 additions & 11 deletions logics/_send_hmac.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,14 @@
/*
* decaffeinate suggestions:
* DS102: Remove unnecessary code created because of implicit returns
* DS207: Consider shorter variations of null checks
* Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
*/

const request = require('request');
const crypto = require('crypto');

const _signature = function(secret_key, body) {
const _signature = (secret_key, body) => {
const signature = crypto.createHmac('sha256', secret_key).update(new Buffer(body, 'utf8')).digest('base64');
return signature;
};

module.exports = function(path, body, cb) {
module.exports = (path, body, cb) => {

if (cb == null) { cb = function(){}; }
if (!cb) { cb = () => {}; }
const {KARTE_URL, KARTE_BOT_APPLICATION_KEY, KARTE_BOT_SECRET_KEY} = require('../config');

const public_key = KARTE_BOT_APPLICATION_KEY;
Expand All @@ -32,7 +25,7 @@ module.exports = function(path, body, cb) {
'X-KARTE-App-Key': `${public_key}`,
'Authorization': `KARTE0-HMAC-SHA256 TimeStamp=\"${timestamp}\",Signature=\"${signature}\"`
}
}, function(err, res, body) {
}, (err, res, body) => {

if (err) {
console.log(err);
Expand Down
33 changes: 13 additions & 20 deletions routes/bot.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,3 @@
/*
* decaffeinate suggestions:
* DS101: Remove unnecessary use of Array.from
* DS102: Remove unnecessary code created because of implicit returns
* DS207: Consider shorter variations of null checks
* Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
*/
const express = require('express');
const router = express.Router();
const algoliasearch = require('algoliasearch');
Expand All @@ -14,7 +7,7 @@ const _send_hmac = require('../logics/_send_hmac');
const _search = require('../logics/_search');

// webhook
router.post('/echo', function(req, res, next) {
router.post('/echo', (req, res, next) => {

let app_name, content;
const {KARTE_BOT_APPLICATION_KEY} = require('../config');
Expand All @@ -35,7 +28,7 @@ router.post('/echo', function(req, res, next) {
content: {
text: `僕はエコーサーバーです: ${content.text}`
}
}, function(err) {
}, (err) => {

if (err) {
return console.log(err);
Expand All @@ -46,7 +39,7 @@ router.post('/echo', function(req, res, next) {
_send('assign', {
user_id,
assignee: `bot-${KARTE_BOT_APPLICATION_KEY}`
}, function(err) {
}, (err) => {

if (err) {
return console.log(err);
Expand All @@ -73,7 +66,7 @@ router.post('/echo', function(req, res, next) {
});

// webhook
router.post('/a3rt', function(req, res, next) {
router.post('/a3rt', (req, res, next) => {

const {KARTE_BOT_APPLICATION_KEY} = require('../config');

Expand All @@ -85,7 +78,7 @@ router.post('/a3rt', function(req, res, next) {
const {app_name, message_id, thread_id, content} = data;

if (assignee === (`bot-${KARTE_BOT_APPLICATION_KEY}`)) {
_a3rt(content.text, function(err, text) {
_a3rt(content.text, (err, text) => {

if (err) {
console.log(err);
Expand All @@ -98,7 +91,7 @@ router.post('/a3rt', function(req, res, next) {
content: {
text
}
}, function(err) {
}, (err) => {

if (err) {
return console.log(err);
Expand All @@ -114,7 +107,7 @@ router.post('/a3rt', function(req, res, next) {
});

// webhook
router.post('/operator', function(req, res, next) {
router.post('/operator', (req, res, next) => {
const {KARTE_BOT_APPLICATION_KEY} = require('../config');
const {data, user, event_type} = req.body;
const {user_id, assignee} = user;
Expand Down Expand Up @@ -157,7 +150,7 @@ router.post('/operator', function(req, res, next) {
_search({
text: content.text,
count: 5
}, function(err, result) {
}, (err, result) => {
let texts;
if (err) { console.log(err); }
if ((result.count === 0) || err) {
Expand All @@ -184,7 +177,7 @@ router.post('/operator', function(req, res, next) {
});
});

var _unassign = user_id =>
const _unassign = user_id =>
// アサインを外す
_send('assign', {
user_id,
Expand All @@ -193,17 +186,17 @@ var _unassign = user_id =>
// 同時に「対応済み」にする
finish_responding: true
}
}, function(err) {
}, (err) => {
if (err) {
return console.log(err);
}
})
;

var _send_delayed_msgs = function(user_id, texts) {
const _send_delayed_msgs = (user_id, texts) => {
if (!texts) { return; }
const promises = texts.map((txt, i) =>
new Promise(function(resolve, reject) {
new Promise((resolve, reject) => {
return setTimeout(() =>
_send('message', {
app_name: 'webchat',
Expand All @@ -219,7 +212,7 @@ var _send_delayed_msgs = function(user_id, texts) {
return Promise.all(promises);
};

var _makeLinkMessageStr = function(links) {
const _makeLinkMessageStr = (links) => {
// link UI example:
// '`{"type":"links","links":[{"title":"PLAID公式サイト","url":"https://plaid.co.jp/"},{"title":"KARTE公式サイト","url":"https://karte.io/"}]}`'
let str = '`{"type":"links","links":[';
Expand Down
17 changes: 2 additions & 15 deletions routes/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,8 @@ const _send = require('../logics/_send');

/* GET home page. */

router.get('/', function(req, res, next) {

const {KARTE_BOT_APPLICATION_KEY} = require('../config');

_send('track', {
keys: {
user_id: 'bot'
},
event_name: 'bot_sample_server_view_post',
values: {
method: 'post'
}
});

return res.render('index', {title: 'karte io bot sample server', KARTE_BOT_APPLICATION_KEY});
router.get('/', (req, res, next) => {
return res.render('index', {title: 'karte io bot sample server'});
});

module.exports = router;
7 changes: 1 addition & 6 deletions views/index.jade
Original file line number Diff line number Diff line change
@@ -1,9 +1,4 @@
extends layout

block content
h1= title
h2 redirect tracking
a(href="http://localhost:8010/v0/track?app_key=#{KARTE_APP_PUBLIC_KEY}&response=redirect&redirect_to=https://google.co.jp&data=%7B%22keys%22%3A%7B%22user_id%22%3A%22bot%22%7D%2C%22event_name%22%3A%22bot_sample_server_view_redirect%22%2C%22values%22%3A%7B%7D%7D" target="_blank") redirect test -> google
h2 1x1 gif tracking
img(src="http://localhost:8010/v0/track?app_key=#{KARTE_APP_PUBLIC_KEY}&response=image&data=%7B%22keys%22%3A%7B%22user_id%22%3A%22bot%22%7D%2C%22event_name%22%3A%22bot_sample_server_view_image%22%2C%22values%22%3A%7B%7D%7D")

h1= title
14 changes: 0 additions & 14 deletions views/setting.jade

This file was deleted.

0 comments on commit e390b46

Please sign in to comment.