Skip to content

Commit

Permalink
Merge pull request #9 from declandewet/develop
Browse files Browse the repository at this point in the history
Develop
  • Loading branch information
zspecza committed Dec 13, 2015
2 parents 6104a49 + 543fe20 commit 0c8be50
Show file tree
Hide file tree
Showing 7 changed files with 67 additions and 18 deletions.
28 changes: 28 additions & 0 deletions .editorconfig
@@ -0,0 +1,28 @@
# EditorConfig is awesome: http://EditorConfig.org

# top-most EditorConfig file
root = true

# Unix-style newlines with a newline ending every file
[*]
end_of_line = lf
insert_final_newline = true

# Matches multiple files with brace expansion notation
# Set default charset
[*.{js}]
charset = utf-8

# Tab indentation (no size specified)
[Makefile]
indent_style = tab

# Indentation override for all JS under lib directory
[lib/**.js]
indent_style = space
indent_size = 2

# Matches the exact files either package.json or .travis.yml
[{package.json,.travis.yml}]
indent_style = space
indent_size = 2
16 changes: 12 additions & 4 deletions lib/base-api.js → lib/base.js
Expand Up @@ -6,7 +6,7 @@ import fetch,
{Response} from 'node-fetch';
import WebSocket from 'ws';
import WEB_METHODS from './web-methods';
import singleLine from './utils/singleline';
import singleLine from './tags/singleline';

const VALID_TOKEN = /^([a-z]*)\-([0-9]*)\-([0-9a-zA-Z]*)/;
const VALID_URL = /^(https?|ftp):\/\/(((([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:)*@)?(((\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5])\.(\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5])\.(\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5])\.(\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5]))|((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.?)(:\d*)?)(\/((([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:|@)+(\/(([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:|@)*)*)?)?(\?((([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:|@)|[\uE000-\uF8FF]|\/|\?)*)?(\#((([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:|@)|\/|\?)*)?$/i;
Expand Down Expand Up @@ -141,6 +141,7 @@ export default class BaseAPI extends EventEmitter {
* @return {Promise} - a promise for the parsed JSON response
*/
async method(method, opts) {
let response;
// merge given opts with defaults
opts = { ...this.defaults, ...opts };
cleanOptProps(opts, 'webhook', 'slackbot');
Expand All @@ -156,9 +157,16 @@ export default class BaseAPI extends EventEmitter {
opts[option] = JSON.stringify(opts[option]);
}
}
let response = await this.api(
`${this.baseURL + method}?${qs.stringify(opts)}`
);
if (method === 'files.upload') {
response = await this.api(`${this.baseURL + method}`, {
method: 'post',
body: JSON.stringify(opts)
});
} else {
response = await this.api(
`${this.baseURL + method}?${qs.stringify(opts)}`
);
}
if (!response.ok) {
let errors = WEB_METHODS.find(web => web.name === method).errors;
throw new Error(
Expand Down
4 changes: 3 additions & 1 deletion lib/index.js
@@ -1,5 +1,7 @@

// import dependencies
import Bacon from 'baconjs';
import BaseAPI from './base-api';
import BaseAPI from './base';
import RTM_EVENTS from './rtm-events';
import WEB_METHODS from './web-methods';

Expand Down
22 changes: 22 additions & 0 deletions lib/tags/singleline.js
@@ -0,0 +1,22 @@

/**
* template tag. allows you to write
* a string out on multiple lines
* and it will reduce it to a one-line
* string by normalizing newlines to spaces
* and removing unecessary indentation
* @param {Array} lits template literals
* @param {Arguments} ...subs template interpolated variables
* @return {String} the single-line normalized string
*/
export default function singleLine(lits, ...subs) {
let result = "";
// run the loop only for the substitution count
for (let i = 0; i < subs.length; i++) {
result += lits[i];
result += subs[i];
}
// add the last literal
result += lits[lits.length - 1];
return result.replace(/(?:\s+)/g, ' ');
};
11 changes: 0 additions & 11 deletions lib/utils/singleline.js

This file was deleted.

2 changes: 1 addition & 1 deletion test/base-api.js
Expand Up @@ -3,7 +3,7 @@ import {EventEmitter} from 'events';
import chai from 'chai';
import asPromised from 'chai-as-promised';
import nock from 'nock';
import BaseAPI from '../lib/base-api';
import BaseAPI from '../lib/base';
import MockSocket from 'mock-socket/dist/mock-socket';

chai.use(asPromised);
Expand Down
2 changes: 1 addition & 1 deletion test/frp.js
@@ -1,6 +1,6 @@
import chai from 'chai';
import Slack from '../lib';
import BaseAPI from '../lib/base-api';
import BaseAPI from '../lib/base';

const {expect} = chai;
const token = 'xoxb-9545181767-KLtao5iiYssThypRBQ5CBHeX';
Expand Down

0 comments on commit 0c8be50

Please sign in to comment.