/lib/bitly/Bitly.js
https://github.com/zephrax/node-bitly · JavaScript · 192 lines · 170 code · 22 blank · 0 comment · 11 complexity · ae5b8406f817600cb666e1d7449999b6 MD5 · raw file
- var url = require('url'),
- http = require('http');
- var Bitly = function(login, api_key, options) {
- options = options || {
- format: 'json',
- api_url: 'api.bit.ly',
- api_version: 'v3',
- domain: 'bit.ly'
- };
- this.config = {
- login: login,
- api_key: api_key,
- format: options.format,
- api_url: options.api_url,
- api_version: options.api_version,
- domain: options.domain
- };
- };
- Bitly.prototype = {
- _parseChunks: function(chunks) {
- return chunks.join('');
- },
-
- _generateNiceUrl: function(query, method) {
- var result = url.parse(url.format({
- protocol: 'http',
- hostname: this.config.api_url,
- pathname: '/' + this.config.api_version + '/' + method,
- query: query
- }));
-
- return result;
- },
-
- _doRequest: function(request_query, cb) {
- var scope = this;
- var client = http.createClient(80, request_query.hostname);
- var request = client.request('GET', request_query.pathname + '?' + request_query.query, {
- 'host': request_query.hostname
- });
- request.end();
- request.on('response',
- function(response) {
- var data = [];
- response.on('data',
- function(chunk) {
- data.push(chunk);
- });
- response.on('end',
- function() {
- var urldata = scope._parseChunks(data);
- var result = JSON.parse(urldata)
- return cb(result);
- });
- });
- },
-
- _urlCheck: function(str) {
- var v = new RegExp();
- v.compile("^[A-Za-z]+://[A-Za-z0-9-_]+\\.[A-Za-z0-9-_%&\?\/.=]+$");
- if (!v.test(str)) return false;
- return true;
- },
-
- shorten: function(longUrl, cb) {
- var query = {
- login: this.config.login,
- apiKey: this.config.api_key,
- format: this.config.format,
- longUrl: longUrl,
- domain: this.config.domain
- };
-
- var request_query = this._generateNiceUrl(query, 'shorten');
- this._doRequest(request_query, cb);
- },
-
- expand: function(items, cb) {
- var shortUrl = [];
- var hash = [];
- while (items.length > 0) {
- var item_to_check = items.pop();
- if (this._urlCheck(item_to_check)) {
- shortUrl.push(item_to_check);
- } else {
- hash.push(item_to_check);
- }
- }
- var query = {
- login: this.config.login,
- apiKey: this.config.api_key,
- format: this.config.format,
- shortUrl: shortUrl,
- hash: hash,
- domain: this.config.domain
- };
-
- var request_query = this._generateNiceUrl(query, 'expand');
- this._doRequest(request_query, cb);
- },
-
- clicks: function(items, cb) {
- var shortUrl = [];
- var hash = [];
- while (items.length > 0) {
- var item_to_check = items.pop();
- if (this._urlCheck(item_to_check)) {
- shortUrl.push(item_to_check);
- } else {
- hash.push(item_to_check);
- }
- }
- var query = {
- login: this.config.login,
- apiKey: this.config.api_key,
- format: this.config.format,
- shortUrl: shortUrl,
- hash: hash,
- domain: this.config.domain
- };
- var request_query = this._generateNiceUrl(query, 'clicks');
- this._doRequest(request_query, cb);
- },
-
- lookup: function(links, cb) {
- var query = {
- login: this.config.login,
- apiKey: this.config.api_key,
- format: this.config.format,
- url: links,
- domain: this.config.domain
- };
-
- var request_query = this._generateNiceUrl(query, 'lookup');
- this._doRequest(request_query, cb);
- },
-
- info: function(items, cb) {
- var shortUrl = [];
- var hash = [];
- while (items.length > 0) {
- var item_to_check = items.pop();
- if (this._urlCheck(item_to_check)) {
- shortUrl.push(item_to_check);
- } else {
- hash.push(item_to_check);
- }
- }
- var query = {
- login: this.config.login,
- apiKey: this.config.api_key,
- format: this.config.format,
- shortUrl: shortUrl,
- hash: hash,
- domain: this.config.domain
- };
-
- var request_query = this._generateNiceUrl(query, 'info');
- this._doRequest(request_query, cb);
- },
-
- bitlyProDomain: function(domain, cb) {
- var query = {
- login: this.config.login,
- apiKey: this.config.api_key,
- format: this.config.format,
- domain: domain
- };
-
- var request_query = this._generateNiceUrl(query, 'bitly_pro_domain');
- this._doRequest(request_query, cb);
- },
-
- authenticate: function(x_login, x_password, cb) {
- var query = {
- login: this.config.login,
- apiKey: this.config.api_key,
- format: this.config.format,
- x_login: x_login,
- x_password: x_password
- };
-
- var request_query = this._generateNiceUrl(query, 'authenticate');
- this._doRequest(request_query, cb);
- }
- };
- exports.Bitly = Bitly;