Skip to content
This repository was archived by the owner on May 29, 2018. It is now read-only.

Commit

Permalink
Merge pull request #6 from liquidg3/dev
Browse files Browse the repository at this point in the history
listeners
  • Loading branch information
liquidg3 authored Nov 7, 2017
2 parents 587df34 + 7f13dfa commit e531097
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 27 deletions.
21 changes: 21 additions & 0 deletions factories/listeners.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
const glob = require('glob')
const path = require('path')
const { camelCase } = require('lodash')

module.exports = (dir, router) => {
const matches = glob.sync(path.join(dir, '/**/*.js'))
return matches.reduce((ctx, match) => {
const m = require(match)
const eventName = camelCase(
match
.split('/events/')
.pop()
.split('.')
.shift()
)

ctx[eventName] = require(match)

return ctx
}, {})
}
22 changes: 8 additions & 14 deletions https/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,29 +2,23 @@ const url = require('../utilities/url')
const https = require('https')

module.exports = class Https {
constructor({
host,
apiKey,
skillId,
version,
allowSelfSignedCerts = false
}) {
if (!host || !apiKey || !skillId || !version) {
constructor({ host, apiKey, id, version, allowSelfSignedCerts = false }) {
if (!host || !apiKey || !id || !version) {
throw new Error(
'You gotta pass host, apiKey, skillId, and version to the Http constructor'
'You gotta pass host, apiKey, id, and version to the Http constructor'
)
}
this.host = host
this.apiKey = apiKey
this.skillId = skillId
this.id = id
this.version = version
this.allowSelfSignedCerts = allowSelfSignedCerts
}

/**
* GET an endpoint.
*
* @param {String} url Path to the endpoint you want to hit. Do NOT include /api/${version}/skills/${skillId}
* @param {String} url Path to the endpoint you want to hit. Do NOT include /api/${version}/skills/${id}
* @param {Object} query Vanilla object that is converted into a query string
* @returns {Promise}
*/
Expand All @@ -39,7 +33,7 @@ module.exports = class Https {
const request = https.request(
{
host: this.host,
path: url.build(path, query, this.version, this.skillId),
path: url.build(path, query, this.version, this.id),
rejectUnauthorized: !this.allowSelfSignedCerts,
headers
},
Expand Down Expand Up @@ -80,7 +74,7 @@ module.exports = class Https {
host: this.host,
headers,
rejectUnauthorized: !this.allowSelfSignedCerts,
path: url.build(path, query, this.version, this.skillId)
path: url.build(path, query, this.version, this.id)
},
response => {
this.handleResponse(request, response, resolve, reject)
Expand Down Expand Up @@ -126,7 +120,7 @@ module.exports = class Https {
host: this.host,
headers,
rejectUnauthorized: !this.allowSelfSignedCerts,
path: url.build(path, query, this.version, this.skillId)
path: url.build(path, query, this.version, this.id)
},
response => {
this.handleResponse(request, response, resolve, reject)
Expand Down
24 changes: 13 additions & 11 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,27 +1,28 @@
const context = require('./factories/context')
const routes = require('./factories/routes')
const wares = require('./factories/wares')
const listeners = require('./factories/listeners')
const Https = require('./https')

/**
* Politely tell someone they didn't define an arg
* @param {string} name
*/
function argError(name) {
function required(name) {
throw new Error(
`You are missing some params! Make sure you set ${name} properly`
)
}

class Sprucebot {
constructor({
apiKey = argError('apiKey'),
skillId = argError('apiKey'),
host = argError('apiKey'),
name = argError('apiKey'),
description = argError('apiKey'),
skillUrl = argError('apiKey'),
svgIcon = argError('apiKey'),
apiKey = required('apiKey'),
id = required('id'),
host = required('host'),
name = required('name'),
description = required('description'),
skillUrl = required('skillUrl'),
svgIcon = required('svgIcon'),
allowSelfSignedCerts = false
}) {
// Setup http(s) class with everything it needs to talk to api
Expand All @@ -36,22 +37,23 @@ class Sprucebot {
this.https = new Https({
host,
apiKey,
skillId,
id,
version: this.version,
allowSelfSignedCerts
})

console.log(
`🌲 Sprucebot🌲 Skills Kit API ${this
.version}\n\napiKey : ${apiKey}, \nhost : ${host}, \nskillId : ${skillId} \nname : ${name}\n---------------------------------`
.version}\n\napiKey : ${apiKey}, \nhost : ${host}, \nid : ${id} \nname : ${name}\n---------------------------------`
)

// Setup skillskit helpers
this.skillskit = {
factories: {
context,
routes,
wares
wares,
listeners
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ const TIMESTAMP = new Date().getTime()
const SPRUCE_ID = '1975559c-e071-4198-8ab3-eccbeb00e1d0'
const TAYLOR_ID = '78245981-5022-49a7-b2f2-6ac687e0f3d1'
const SKILL = {
skillId: '482D8B56-5223-43BF-8E7F-011509B9968A',
id: '482D8B56-5223-43BF-8E7F-011509B9968A',
apiKey: 'DD16373A-9482-4E27-A4A3-77B2664F6C82',
host: 'dev-api.sprucebot.com',
name: `Unit Test Skill - ${TIMESTAMP}`,
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "sprucebot-node",
"version": "0.0.1",
"version": "0.0.2",
"scripts": {
"lint": "eslint '**/**/*{.js}'",
"test": "jest --coverage",
Expand Down

0 comments on commit e531097

Please sign in to comment.