UNPKG

seeq

Version:

Search or detect name on some resource (GitHub, NPM, Bower, ...)

380 lines (302 loc) 10.5 kB
<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <title>seeq Source: resource/module/github.js</title> <!--[if lt IE 9]> <script src="//html5shiv.googlecode.com/svn/trunk/html5.js"></script> <![endif]--> <link type="text/css" rel="stylesheet" href="styles/sunlight.default.css"> <link type="text/css" rel="stylesheet" href="styles/site.spacelab.css"> </head> <body> <div class="container-fluid"> <div class="navbar navbar-fixed-top "> <div class="navbar-inner"> <a class="brand" href="index.html">seeq</a> <ul class="nav"> <li class="dropdown"> <a href="modules.list.html" class="dropdown-toggle" data-toggle="dropdown">Modules<b class="caret"></b></a> <ul class="dropdown-menu "> <li> <a href="module-bower.html">bower</a> </li> <li> <a href="module-cdn.html">cdn</a> </li> <li> <a href="module-cdnjs.html">cdnjs</a> </li> <li> <a href="module-cli.html">cli</a> </li> <li> <a href="module-component.html">component</a> </li> <li> <a href="module-customelements.html">customelements</a> </li> <li> <a href="module-format.html">format</a> </li> <li> <a href="util.html">format/util</a> </li> <li> <a href="module-github.html">github</a> </li> <li> <a href="module-grunt.html">grunt</a> </li> <li> <a href="module-gulp.html">gulp</a> </li> <li> <a href="module-jam.html">jam</a> </li> <li> <a href="module-jsdelivr.html">jsdelivr</a> </li> <li> <a href="module-json.html">json</a> </li> <li> <a href="module-jster.html">jster</a> </li> <li> <a href="module-markdown.html">markdown</a> </li> <li> <a href="module-microjs.html">microjs</a> </li> <li> <a href="module-npm.html">npm</a> </li> <li> <a href="module-raw.html">raw</a> </li> <li> <a href="module-resource.html">resource</a> </li> <li> <a href="util_.html">resource/util</a> </li> <li> <a href="module-seeq.html">seeq</a> </li> <li> <a href="module-spm.html">spm</a> </li> <li> <a href="module-text.html">text</a> </li> </ul> </li> <li class="dropdown"> <a href="classes.list.html" class="dropdown-toggle" data-toggle="dropdown">Classes<b class="caret"></b></a> <ul class="dropdown-menu "> <li> <a href="util-FailedHttpRequestError.html">FailedHttpRequestError</a> </li> <li> <a href="util-IncorrectResponseError.html">IncorrectResponseError</a> </li> </ul> </li> </ul> </div> </div> <div class="row-fluid"> <div class="span12"> <div id="main"> <h1 class="page-title">Source: resource/module/github.js</h1> <section> <article> <pre class="sunlight-highlight-javascript linenums">/** * Module that provides means to check/search in {@link https://github.com GitHub}. * * @module github */ "use strict"; var github = require("octonode"), util = require("../util"), remainingLimit = 1, delay = 60001; // Process search result function handler(err, data, headers) { /*jshint camelcase:false, validthis:true*/ var result = [], bRealSearch, item, itemList, nI, nL, nLimit, settings, sSearchName; if (! err) { remainingLimit = Number(headers["x-ratelimit-remaining"]); delay = (Number(headers["x-ratelimit-reset"]) * 1000) - (new Date()).getTime() + 1; settings = this.settings; bRealSearch = util.isRealSearchSet(settings); nLimit = util.getLimit(settings, 100, 100); sSearchName = this.name; itemList = data.items; for (nI = 0, nL = itemList.length; nI &lt; nL; nI++) { item = itemList[nI]; if ( bRealSearch || util.isStringMatch(item.name, sSearchName, settings) ) { item.api_url = item.url; item.url = item.homepage || item.html_url; item.repository = item.clone_url; item.stars = item.stargazers_count; result.push(item); if (result.length === nLimit) { break; } } } this.callback(err, result); } else if (err.message === "Search repos error") { setTimeout(this.send, delay); } else { this.callback(err, result); } } // Send search request function send() { /*jshint validthis:true*/ var settings = this.settings, sLanguage = settings.lang || settings.language, sQuery = this.name, nPageSize = settings.pageSize; if (! util.isRealSearchSet(settings)) { sQuery += "+in:name"; } if (sLanguage) { sQuery += "+language:" + sLanguage; } if (typeof nPageSize !== "number" || nPageSize &lt;= 0 || nPageSize > 100) { nPageSize = 100; } this.search.repos({q: sQuery, "per_page": nPageSize}, this.handler); } /** * Supported operation settings/options. * * Conform to format of {@link https://github.com/harthur/nomnom nomnom} options. */ exports.settings = { lang: { help: "Search GitHub repositories that are written in the specified language" }, limit: { help: "Limit of quantity of GitHub results; default and maximum value is 100" }, user: { help: "GitHub username that should be used for authentication" }, password: { help: "GitHub account password" }, token: { help: "GitHub OAuth2 token that should be used for authentication instead of username and password" } }; /** * Check whether repository with the specified name is existent. * * Data about found repositories will be passed into callback as array. * If no repository is found, empty array will be passed into callback. * * @param {String} name * Name of the repository to check. * @param {Function} callback * Function that should be called to process operation's result. * @param {Object} [settings] * Operation settings. * The following settings are supported (name - type - description): &lt;ul> &lt;li>&lt;code>caseSensitive&lt;/code> - &lt;code>Boolean&lt;/code> - Whether case-sensitive search should be used &lt;li>&lt;code>partialMatch&lt;/code> - &lt;code>Integer&lt;/code> - Allow partial matching: 0 - disallow (by default), 1 - allow at the beginning of matching strings, 2 - allow substring matching &lt;li>&lt;code>lang, language&lt;/code> - &lt;code>String&lt;/code> - Search repositories that are written in the specified language &lt;li>&lt;code>user&lt;/code> - &lt;code>String&lt;/code> - GitHub username that should be used for authentication &lt;li>&lt;code>password&lt;/code> - &lt;code>String&lt;/code> - GitHub account password &lt;li>&lt;code>token&lt;/code> - &lt;code>String&lt;/code> - OAuth2 token that should be used for authentication instead of username and password &lt;li>&lt;code>limit&lt;/code> - &lt;code>Integer&lt;/code> - Limit of quantity of results; default and maximum value is 100 &lt;li>&lt;code>pageSize&lt;/code> - &lt;code>Integer&lt;/code> - Quantity of results per page; default and maximum value is 100 &lt;/ul> */ exports.detect = function(name, callback, settings) { var client, request; if (! settings) { settings = {}; } if (settings.token) { client = github.client(settings.token); } else if (settings.user && settings.password) { client = github.client({ username: settings.user, password: settings.password }); } else { client = github.client(); } request = { search: client.search(), name: settings.caseSensitive ? name : name.toLowerCase(), callback: callback, settings: settings }; request.send = send.bind(request); request.handler = handler.bind(request); if (remainingLimit) { request.send(); } else { setTimeout(request.send, delay); } }; </pre> </article> </section> </div> <div class="clearfix"></div> <footer> <span class="copyright"> Copyright (c) 2014 Denis Sikuler </span> <br /> <span class="jsdoc-message"> Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.2.2</a> on Thu Aug 21 2014 23:46:51 GMT+0400 (MSK) using the <a href="https://github.com/terryweiss/docstrap">DocStrap template</a>. </span> </footer> </div> <br clear="both"> </div> </div> <script src="scripts/sunlight.js"></script> <script src="scripts/sunlight.javascript.js"></script> <script src="scripts/sunlight-plugin.doclinks.js"></script> <script src="scripts/sunlight-plugin.linenumbers.js"></script> <script src="scripts/sunlight-plugin.menu.js"></script> <script src="scripts/jquery.min.js"></script> <script src="scripts/jquery.scrollTo.js"></script> <script src="scripts/jquery.localScroll.js"></script> <script src="scripts/bootstrap-dropdown.js"></script> <script src="scripts/toc.js"></script> <script> Sunlight.highlightAll({lineNumbers:true, showMenu: true, enableDoclinks :true}); </script> <script> $( function () { $( "#toc" ).toc( { selectors : "h1,h2,h3,h4", showAndHide : false, scrollTo : 60 } ); $( "#toc>ul" ).addClass( "nav nav-pills nav-stacked" ); $( "#main span[id^='toc']" ).addClass( "toc-shim" ); } ); </script> </body> </html>