/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

  1. var url = require('url'),
  2. http = require('http');
  3. var Bitly = function(login, api_key, options) {
  4. options = options || {
  5. format: 'json',
  6. api_url: 'api.bit.ly',
  7. api_version: 'v3',
  8. domain: 'bit.ly'
  9. };
  10. this.config = {
  11. login: login,
  12. api_key: api_key,
  13. format: options.format,
  14. api_url: options.api_url,
  15. api_version: options.api_version,
  16. domain: options.domain
  17. };
  18. };
  19. Bitly.prototype = {
  20. _parseChunks: function(chunks) {
  21. return chunks.join('');
  22. },
  23. _generateNiceUrl: function(query, method) {
  24. var result = url.parse(url.format({
  25. protocol: 'http',
  26. hostname: this.config.api_url,
  27. pathname: '/' + this.config.api_version + '/' + method,
  28. query: query
  29. }));
  30. return result;
  31. },
  32. _doRequest: function(request_query, cb) {
  33. var scope = this;
  34. var client = http.createClient(80, request_query.hostname);
  35. var request = client.request('GET', request_query.pathname + '?' + request_query.query, {
  36. 'host': request_query.hostname
  37. });
  38. request.end();
  39. request.on('response',
  40. function(response) {
  41. var data = [];
  42. response.on('data',
  43. function(chunk) {
  44. data.push(chunk);
  45. });
  46. response.on('end',
  47. function() {
  48. var urldata = scope._parseChunks(data);
  49. var result = JSON.parse(urldata)
  50. return cb(result);
  51. });
  52. });
  53. },
  54. _urlCheck: function(str) {
  55. var v = new RegExp();
  56. v.compile("^[A-Za-z]+://[A-Za-z0-9-_]+\\.[A-Za-z0-9-_%&\?\/.=]+$");
  57. if (!v.test(str)) return false;
  58. return true;
  59. },
  60. shorten: function(longUrl, cb) {
  61. var query = {
  62. login: this.config.login,
  63. apiKey: this.config.api_key,
  64. format: this.config.format,
  65. longUrl: longUrl,
  66. domain: this.config.domain
  67. };
  68. var request_query = this._generateNiceUrl(query, 'shorten');
  69. this._doRequest(request_query, cb);
  70. },
  71. expand: function(items, cb) {
  72. var shortUrl = [];
  73. var hash = [];
  74. while (items.length > 0) {
  75. var item_to_check = items.pop();
  76. if (this._urlCheck(item_to_check)) {
  77. shortUrl.push(item_to_check);
  78. } else {
  79. hash.push(item_to_check);
  80. }
  81. }
  82. var query = {
  83. login: this.config.login,
  84. apiKey: this.config.api_key,
  85. format: this.config.format,
  86. shortUrl: shortUrl,
  87. hash: hash,
  88. domain: this.config.domain
  89. };
  90. var request_query = this._generateNiceUrl(query, 'expand');
  91. this._doRequest(request_query, cb);
  92. },
  93. clicks: function(items, cb) {
  94. var shortUrl = [];
  95. var hash = [];
  96. while (items.length > 0) {
  97. var item_to_check = items.pop();
  98. if (this._urlCheck(item_to_check)) {
  99. shortUrl.push(item_to_check);
  100. } else {
  101. hash.push(item_to_check);
  102. }
  103. }
  104. var query = {
  105. login: this.config.login,
  106. apiKey: this.config.api_key,
  107. format: this.config.format,
  108. shortUrl: shortUrl,
  109. hash: hash,
  110. domain: this.config.domain
  111. };
  112. var request_query = this._generateNiceUrl(query, 'clicks');
  113. this._doRequest(request_query, cb);
  114. },
  115. lookup: function(links, cb) {
  116. var query = {
  117. login: this.config.login,
  118. apiKey: this.config.api_key,
  119. format: this.config.format,
  120. url: links,
  121. domain: this.config.domain
  122. };
  123. var request_query = this._generateNiceUrl(query, 'lookup');
  124. this._doRequest(request_query, cb);
  125. },
  126. info: function(items, cb) {
  127. var shortUrl = [];
  128. var hash = [];
  129. while (items.length > 0) {
  130. var item_to_check = items.pop();
  131. if (this._urlCheck(item_to_check)) {
  132. shortUrl.push(item_to_check);
  133. } else {
  134. hash.push(item_to_check);
  135. }
  136. }
  137. var query = {
  138. login: this.config.login,
  139. apiKey: this.config.api_key,
  140. format: this.config.format,
  141. shortUrl: shortUrl,
  142. hash: hash,
  143. domain: this.config.domain
  144. };
  145. var request_query = this._generateNiceUrl(query, 'info');
  146. this._doRequest(request_query, cb);
  147. },
  148. bitlyProDomain: function(domain, cb) {
  149. var query = {
  150. login: this.config.login,
  151. apiKey: this.config.api_key,
  152. format: this.config.format,
  153. domain: domain
  154. };
  155. var request_query = this._generateNiceUrl(query, 'bitly_pro_domain');
  156. this._doRequest(request_query, cb);
  157. },
  158. authenticate: function(x_login, x_password, cb) {
  159. var query = {
  160. login: this.config.login,
  161. apiKey: this.config.api_key,
  162. format: this.config.format,
  163. x_login: x_login,
  164. x_password: x_password
  165. };
  166. var request_query = this._generateNiceUrl(query, 'authenticate');
  167. this._doRequest(request_query, cb);
  168. }
  169. };
  170. exports.Bitly = Bitly;