Skip to content

Commit

Permalink
Refactor some internal code and bump version
Browse files Browse the repository at this point in the history
  • Loading branch information
tanepiper committed Oct 28, 2015
1 parent 2139bc4 commit 62bb7db
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 45 deletions.
10 changes: 4 additions & 6 deletions package.json
@@ -1,17 +1,15 @@
{
"name": "bitly",
"description": "A Bit.ly API library for Node.JS",
"license": {
"type": "MIT",
"url": "http://www.opensource.org/licenses/mit-license.php"
},
"tags": [
"license": "MIT",
"keywords": [
"url",
"bitly",
"shortner",
"util"
],
"version": "4.0.1",
"homepage": "https://github.com/tanepiper/node-bitly",
"version": "4.1.0",
"author": {
"name": "Tane Piper",
"email": "piper.tane@gmail.com",
Expand Down
63 changes: 24 additions & 39 deletions src/bitly.js
Expand Up @@ -70,8 +70,13 @@ class Bitly {
* @return {void}
*/
sortUrlsAndHash (items, query) {
var shortUrl = [];
var hash = [];
let shortUrl = [];
let hash = [];

// If only passed one item, put in array for url checking
if (typeof items === 'string') {
items = [items];
}
items.forEach((item) => {
isUri(item) ? shortUrl.push(item) : hash.push(item);
});
Expand All @@ -88,7 +93,7 @@ class Bitly {
* Request to shorten one long url
* @param {String} longUrl The URL to be shortened
* @param {String=} domain The domain to use (optional)
* @return {Promise|void}
* @return {Promise}
*/
shorten (longUrl, domain) {
var query = {
Expand All @@ -103,59 +108,47 @@ class Bitly {
/**
* Request to expand a single short url, short hash or mixed array or items
* @param {String|Array} items The string or array of short urls and/or hashes to expand
* @return {Promise|void}
* @return {Promise}
*/
expand (items) {
var query = {
format: this.config.format,
domain: this.config.domain
};

if (typeof items === 'string') {
query[isUri(items) ? 'shortUrl' : 'hash'] = items;
} else {
this.sortUrlsAndHash(items, query);
}
this.sortUrlsAndHash(items, query);

return this.doRequest(this.generateNiceUrl(query, 'expand'));
}

/**
* Request to get clicks for a single short url, short hash or mixed array or items
* @param {String|Array} items The string or array of short urls and/or hashes to expand
* @return {Promise|void}
* @return {Promise}
*/
clicks (items) {
var query = {
format: this.config.format,
domain: this.config.domain
};

if (typeof items === 'string') {
query[isUri(items) ? 'shortUrl' : 'hash'] = items;
} else {
this.sortUrlsAndHash(items, query);
}
this.sortUrlsAndHash(items, query);

return this.doRequest(this.generateNiceUrl(query, 'clicks'));
}

/**
* Request to get clicks by minute for a single short url, short hash or mixed array or items
* @param {String|Array} items The string or array of short urls and/or hashes to expand
* @return {Promise|void}
* @return {Promise}
*/
clicksByMinute (items) {
var query = {
format: this.config.format,
domain: this.config.domain
};

if (typeof items === 'string') {
query[isUri(items) ? 'shortUrl' : 'hash'] = items;
} else {
this.sortUrlsAndHash(items, query);
}
this.sortUrlsAndHash(items, query);

return this.doRequest(this.generateNiceUrl(query, 'clicks_by_minute'));

Expand All @@ -164,27 +157,23 @@ class Bitly {
/**
* Request to get clicks by day for a single short url, short hash or mixed array or items
* @param {String|Array} items The string or array of short urls and/or hashes to expand
* @return {Promise|void}
* @return {Promise}
*/
clicksByDay (items) {
var query = {
format: this.config.format,
domain: this.config.domain
};

if (typeof items === 'string') {
query[isUri(items) ? 'shortUrl' : 'hash'] = items;
} else {
this.sortUrlsAndHash(items, query);
}
this.sortUrlsAndHash(items, query);

return this.doRequest(this.generateNiceUrl(query, 'clicks_by_day'));
}

/**
* Request to get look up an existing bitly link for a long url or array of urls
* @param {String|Array} links The string or array of long urls
* @return {Promise|void}
* @return {Promise}
*/
lookup (links) {
var query = {
Expand All @@ -200,19 +189,15 @@ class Bitly {
/**
* Request to get clicks by day for a single short url, short hash or mixed array or items
* @param {String|Array} items The string or array of short urls and/or hashes to expand
* @return {Promise|void}
* @return {Promise}
*/
info (items) {
var query = {
format: this.config.format,
domain: this.config.domain
};

if (typeof items === 'string') {
query[isUri(items) ? 'shortUrl' : 'hash'] = items;
} else {
this.sortUrlsAndHash(items, query);
}
this.sortUrlsAndHash(items, query);

return this.doRequest(this.generateNiceUrl(query, 'info'));
}
Expand All @@ -222,7 +207,7 @@ class Bitly {
* Request the informations on all referrers for a short url. This function only
* accepts one url (as per the limit of the bitly API)
* @param {String} link The link be checked
* @return {Promise|void}
* @return {Promise}
*/
referrers (link) {
var query = {
Expand All @@ -238,7 +223,7 @@ class Bitly {
/**
* Request the information on all countries for a short url. This function only
* accepts one url (as per the limit of the bitly API)
* @return {Promise|void}
* @return {Promise}
*/
countries (link) {
var query = {
Expand All @@ -254,7 +239,7 @@ class Bitly {
/**
* Request to confirm a pro-domain it set up with bitly
* @param {String} domain The domain to be checked
* @return {Promise|void}
* @return {Promise}
*/
bitlyProDomain (domain) {
var query = {
Expand All @@ -267,7 +252,7 @@ class Bitly {

/**
* Request entries from a user's link history in reverse chronological order
* @return {Promise|void}
* @return {Promise}
*/
history () {
var query = {
Expand All @@ -288,7 +273,7 @@ class Bitly {
* have to pass an array to new_value. The index have to match those in metadata_field, e.g. metadata_field[0] will
* be
* changed to new_value[0] etc.
* @return {Promise|void}
* @return {Promise}
*/
linkEdit (metadata_field, link, new_value) {
var query = {
Expand Down

0 comments on commit 62bb7db

Please sign in to comment.