Skip to content

Commit 1c36ca7

Browse files
committed
Add more documentation
1 parent f4c604d commit 1c36ca7

4 files changed

Lines changed: 135 additions & 25 deletions

File tree

public/css/custom.css

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,12 @@ section.triplesec {
233233

234234
#documentationNav {
235235
position: fixed;
236+
height: 80%;
237+
display: inline-block;
238+
overflow: hidden;
239+
overflow-y: auto;
240+
border: #CECECE solid 1px;
241+
padding: 5px;
236242
}
237243

238244
@media screen and (max-width: 1000px) {
@@ -412,6 +418,9 @@ input:read-only {
412418
.nav {
413419
display: none !important;
414420
}
421+
#navbar {
422+
display: none !important;
423+
}
415424

416425
.navLogo {
417426
display: none !important;

src/js/documentation.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,14 @@ $(() => {
3333
$(val).removeClass('docGreen');
3434
}
3535
});
36+
37+
// Yes, I actually put the effort into making the timestamp live
38+
updateTimestamp();
39+
setInterval(updateTimestamp, 1000);
40+
41+
function updateTimestamp() {
42+
$('.timestamp').html(`timestamp(); <span class="hljs-comment">//${Math.floor(new Date().getTime()/1000)}</span>`);
43+
}
3644
}
3745

3846
// ===== Scroll to Top ====

views/pages/documentation.ejs

Lines changed: 109 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ function phoneNum(format) {
8080
<h4 class='title'><a name="availableFunctions">Available functions</a></h4>
8181
<p>RandomAPI includes some basic data generation functions that your API can make use of. All of these included functions respect the <b>seed</b> value that is sent in through an API request (except for timestamp since it returns the <b>current</b> time).</p>
8282

83-
<h5 class='subtitle'><a name="availableFunctionsRandom">Random</a></h5>
83+
<h5 class='subtitle'><a name="availableFunctionsRandom">random</a></h5>
8484
<p>Generates a random number within a range as well as a string of random characters from a predefined or custom charset.</p>
8585
<table class="u-full-width">
8686
<thead>
@@ -130,7 +130,7 @@ random.custom("œ∑´®†¥¨ˆøπåß∂ƒ©˙∆˚¬Ω≈ç√∫˜µ", 10)
130130
</tbody>
131131
</table>
132132

133-
<h5 class='subtitle'><a name="availableFunctionsList">List</a></h5>
133+
<h5 class='subtitle'><a name="availableFunctionsList">list</a></h5>
134134
<p>Chooses a random (or specified) item from either a list or provided array. If no lineNumber or indexNumber is provided, a random item will be chosen from the list or array respectively.</p>
135135
<table class="u-full-width">
136136
<thead>
@@ -161,7 +161,7 @@ list(["male", "female"], 1); // female</code></pre>Warning: Indexes are zero bas
161161
</tbody>
162162
</table>
163163

164-
<h5 class='subtitle'><a name="availableFunctionsHash">Hash</a></h5>
164+
<h5 class='subtitle'><a name="availableFunctionsHash">hash</a></h5>
165165
<p>Generate a hash from a string.</p>
166166
<table class="u-full-width">
167167
<thead>
@@ -193,11 +193,77 @@ hash.sha256('mysoupersekurepasswurd!');</code></pre></td>
193193
</tbody>
194194
</table>
195195

196-
<h5 class='subtitle'><a name="availableFunctionsTimestamp">Timestamp</a></h5>
196+
<h5 class='subtitle'><a name="availableFunctionsTimestamp">timestamp</a></h5>
197197
<p>Returns the current unix timestamp.</p>
198-
<pre><code class='javascript'>timestamp(); //1466196805</code></pre>
198+
<pre><code class='javascript timestamp'>timestamp(); //</code></pre>
199199

200-
<h4 class='title'><a name="globalRequire">Global Requires</a></h4>
200+
<h5 class='subtitle'><a name="availableFunctionsGetVar">getVar</a></h5>
201+
<p>Fetches the value of GET variables in the API query. Defaults to null if the requested GET variable is undefined.</p>
202+
<div class="row">
203+
<div class="six columns">
204+
<span class='caption'>API Code</span>
205+
<pre><code class='javascript'>api.max = Number(getVar("max")) || 10;
206+
api.num = random.numeric(0, api.max);</code></pre>
207+
</div>
208+
<div class="six columns">
209+
<span class='caption'>Result</span>
210+
<pre><code>// <%=basehref%>/api/123456?max=25
211+
{"results":[{"max": 100,"num": 21}]}
212+
213+
// <%=basehref%>/api/123456
214+
{"results":[{"max": 10,"num": 4}]}</code></pre>
215+
</div>
216+
</div>
217+
<p>You can also access some internal variables as well with getVar. This could be useful if you wanted access to the string/numeric seed that the generator is using or maybe wanted to know the format your API was being requested with.</p>
218+
<div class="row">
219+
<div class="six columns">
220+
<span class='caption'>API Code</span>
221+
<pre><code class='javascript'>api.seed = getVar('seed');
222+
api.numSeed = getVar('numericSeed');
223+
api.format = getVar('fmt');
224+
api.key = getVar('key');
225+
api.ref = getVar('ref');</code></pre>
226+
</div>
227+
<div class="six columns">
228+
<span class='caption'>Result</span>
229+
<pre><code>{
230+
"results": [
231+
{
232+
"seed": "7583c1fdce1943f4",
233+
"numericSeed": 4200665598,
234+
"format": "pretty",
235+
"key": "ABCD-1234-EFGH-5678",
236+
"ref": "1234abcd"
237+
}
238+
]
239+
}</code></pre>
240+
</div>
241+
</div>
242+
243+
<h5 class='subtitle'><a name="availableFunctionsMisc">Misc</a></h5>
244+
<p>Some other good to know things regarding the generator.</p>
245+
<ul>
246+
<li>The generator uses internal variables that start with _API, so avoid using variables in that namespace if you can or else unexpected behavior can happen.</li>
247+
</ul>
248+
<p>If you have made an API/Snippet that requires a PRNG, you can access the generators internal PRNG with the prng function. This will return, direct from the Mersenne Twister module the generator is using, a number on [0,1) real interval.
249+
<div class="row">
250+
<div class="six columns">
251+
<span class='caption'>API Code</span>
252+
<pre><code class='javascript'>api.a = prng();</code></pre>
253+
</div>
254+
<div class="six columns">
255+
<span class='caption'>Result</span>
256+
<pre><code>{
257+
"results": [
258+
{
259+
"a": 0.5300652552396059
260+
}
261+
]
262+
}</code></pre>
263+
</div>
264+
</div>
265+
266+
<h4 class='title'><a name="globalSnippets">Global Snippets</a></h4>
201267
<p>While native, built-in modules aren't accessible through require, some libraries that we have found helpful for generating data will be added to the RandomAPI generators for use in your APIs. Currently, the following modules are available using require:</p>
202268
<ul>
203269
<li><a href="https://www.npmjs.com/package/faker" class='green' target="_blank">Faker</a></li>
@@ -219,36 +285,45 @@ api.name = faker.name.findName();</code></pre>
219285
<p>If you have a suggestion for a module that we should add, send us a tweet or DM <a href="https://twitter.com/randomapi" class='green' target="_blank">@RandomAPI</a>.</p>
220286

221287
<h4 class='title'><a name="snippets">Snippets</a></h4>
222-
<p>Snippets are user defined pieces of code that you can include in your API. If you have an idea of a helpful function that you'd like to share for others to use, you can make a snippet!</p>
288+
<p>Snippets are user defined pieces of code that you can include in your API. If you've made a snippet that you'd like to share for others to use, you can publish your snippet!</p>
223289
<p>Snippets are coded in basically the same way as a normal API except for these key differences:</p>
224290
<ul>
225291
<li>Objects are attached to the <b>snippet</b> object instead of the <b>api</b> object.</li>
226-
<li>Only Global Requires can be used in your snippet. You can't require other snippets from your snippet.</li>
292+
<li>Only Global Snippets can be used in your snippet. You can't require other snippets from your snippet.</li>
227293
<li>Snippet names <b>must</b> be unique in regards to your account.</li>
228294
</ul>
295+
296+
<h5 class='subtitle'><a name="codingSnippets">Coding</a></h5>
229297
<div class="row">
230298
<div class="six columns">
231299
<span class='caption'>Coding a snippet</span>
232300
<pre><code class="javascript">// keiths stuff
233301
const faker = require('faker');
234-
faker.seed(getVar('numericSeed')); // Make Faker respect RandomAPI's seed
235302

303+
// Make Faker respect RandomAPI's seed
304+
faker.seed(getVar('numericSeed'));
305+
306+
// phoneNumber property attached to snippet
236307
snippet.phoneNumber = function(format) {
237308
format = format || "(xxx) xxx-xxxx";
238309
return String(format).split('').map(digit => {
239310
return digit === 'x' ? random.numeric(0, 9) : digit;
240311
}).join('');
241312
};
242313

314+
// randomName property attached to snippet
243315
snippet.randomName = function() {
244316
return faker.name.findName();
245317
};</code></pre>
246318
</div>
247319
<div class="six columns">
248320
<span class='caption'>Using a snippet</span>
249321
<pre><code>const keith = require('keith/keiths stuff');
250-
api.phone = keith.phoneNumber();
251-
api.name = keith.randomName();</code></pre>
322+
api.phone = keith.phoneNumber();
323+
api.name = keith.randomName();
324+
325+
// Also works
326+
api.phone2 = require('keith/keiths stuff/1').phoneNumber();</code></pre>
252327
<span class='caption'>Result</span>
253328
<pre><code>{
254329
"phoneNumber": "(605) 401-1008",
@@ -280,7 +355,7 @@ api.invoiceID = invoice();</code></pre>
280355
<div class="row">
281356
<div class="six columns">
282357
<span class='caption'>Coding a snippet</span>
283-
<pre><code class="javascript">// randomNum
358+
<pre><code class="javascript">// randomNum...kinda redundant
284359
snippet.randomNum = (min, max) => random.numeric(min, max);</code></pre>
285360
</div>
286361
<div class="six columns">
@@ -294,14 +369,28 @@ api.a = randomNum();</code></pre>
294369
</div>
295370
</div>
296371

372+
<h5 class='subtitle'><a name="publishingSnippets">Publishing</a></h5>
373+
<p>If you've made an awesome snippet that you think would be useful for others, you can publish it!<br>
374+
Here are some basic rules and tips on publishing snippets</p>
375+
<ul>
376+
<li>Once published, snippets can not be removed.</li>
377+
<ol>This is to prevent other users' APIs from breaking unexpectedly. Make sure that you really want to publish a snippet before you click confim.</ol>
378+
<li>Published snippets will be given a revision number that starts at 1.</li>
379+
<ol>Once a snippet is published, you must provide a version number in the snippet signature when you use the require function.</ol>
380+
<li>Source code of revisions that are published can not be modified. You can still modify the revision's description though.</li>
381+
<li>If you have made a mistake or want to make an improvement to a snippet, you can create a new revision.</li>
382+
<li>Users can search for snippets by clicking the search menu item.</li>
383+
<ol>Search depends on the tags/keywords that you give a snippet. Make sure that you provide good tags that users will most likely search for in order to have the best chance of your snippet being found.</ol>
384+
</ul>
385+
297386
<h4 class='title'><a name="callingAPIs">Calling APIs</a></h4>
298-
<p>APIs can be accessed through via the <a href="https://randomapi.com/api" class='green' target="_blank">https://randomapi.com/api</a> endpoint.</p>
387+
<p>APIs can be accessed via the <a href="<%=basehref%>api" class='green' target="_blank"><%=basehref%>api</a> endpoint.</p>
299388
<p>At a minimum, the endpoint requires a <b>key</b> and <b>ref</b> value or a public hash value. Key would be your API key and ref is the Ref ID of the API you want to access which can be found on the <a href="view/api" class='green' target="_blank">View APIs</a> page.</p>
300389
<p>Public hashes are another way that you can access your API without accidentally exposing your API Key and API Ref ID. The user segment of the info block from your results will also be removed by default when your API is called using this method. They can be found by clicking <b>Run API (public URL)</b> on the <a href="view/api" class='green' target="_blank">View APIs</a> page.</p>
301390
<p>APIs can be called in three different ways:</p>
302-
<pre><code class="html">https://randomapi.com/api/<span class='green'>1234abcd</span>?key=<span class='green'>ABCD-1234-EFGH-5678</span> // Ref value is implied
303-
https://randomapi.com/api/?key=<span class='green'>ABCD-1234-EFGH-5678</span>&ref=<span class='green'>1234abcd</span> // Ref value is explicitly stated
304-
https://randomapi.com/api/<span class='green'>1234567890abcdef1234567890abcdef</span> // Public hash</code></pre>
391+
<pre><code class="html"><%=basehref%>api/<span class='green'>1234abcd</span>?key=<span class='green'>ABCD-1234-EFGH-5678</span> // Ref value is implied
392+
<%=basehref%>api/?key=<span class='green'>ABCD-1234-EFGH-5678</span>&ref=<span class='green'>1234abcd</span> // Ref value is explicitly stated
393+
<%=basehref%>api/<span class='green'>1234567890abcdef1234567890abcdef</span> // Public hash</code></pre>
305394

306395
<h5 class='subtitle'><a name="options">Options</a></h5>
307396
<p>You can add extra parameters to your API call to transform your data into different formats, specify how many results you'd like to generate, specify a seed, and even communicate with your api through the <b>getVar</b> function</p>
@@ -383,10 +472,10 @@ https://randomapi.com/api/<span class='green'>1234567890abcdef1234567890abcdef</
383472
</tr>
384473
</tbody>
385474
</table>
386-
<pre><code class="html">https://randomapi.com/api/1234abcd?key=ABCD-1234-EFGH-5678&results=<span class='green'>25</span>&seed=<span class='green'>huskiesarecute</span>&page=<span class='green'>3</span>
387-
https://randomapi.com/api/?key=ABCD-1234-EFGH-5678&ref=1234abcd&callback=<span class='green'>fetchData</span>
388-
https://randomapi.com/api/?key=ABCD-1234-EFGH-5678&ref=1234abcd&fmt=<span class='green'>xml</span>&<span class='green'>dl</span>
389-
https://randomapi.com/api/?key=ABCD-1234-EFGH-5678&ref=1234abcd&fmt=<span class='green'>pretty</span>&<span class='green'>noinfo</span></code></pre>
475+
<pre><code class="html"><%=basehref%>api/1234abcd?key=ABCD-1234-EFGH-5678&results=<span class='green'>25</span>&seed=<span class='green'>huskiesarecute</span>&page=<span class='green'>3</span>
476+
<%=basehref%>api/?key=ABCD-1234-EFGH-5678&ref=1234abcd&callback=<span class='green'>fetchData</span>
477+
<%=basehref%>api/?key=ABCD-1234-EFGH-5678&ref=1234abcd&fmt=<span class='green'>xml</span>&<span class='green'>dl</span>
478+
<%=basehref%>api/?key=ABCD-1234-EFGH-5678&ref=1234abcd&fmt=<span class='green'>pretty</span>&<span class='green'>noinfo</span></code></pre>
390479

391480
<h5 class='subtitle'><a name="errors">Errors</a></h5>
392481
<table class="u-full-width">

views/snippets/documentationSubnav.ejs

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,16 @@
77
<li><a href="documentation#sandbox">&nbsp;&nbsp;&nbsp;&nbsp;Sandbox</a></li>
88
<li class='seperator'></li>
99
<li><a href="documentation#availableFunctions">Available functions</a></li>
10-
<li><a href="documentation#availableFunctionsRandom">&nbsp;&nbsp;&nbsp;&nbsp;Random</a></li>
11-
<li><a href="documentation#availableFunctionsList">&nbsp;&nbsp;&nbsp;&nbsp;List</a></li>
12-
<li><a href="documentation#availableFunctionsHash">&nbsp;&nbsp;&nbsp;&nbsp;Hash</a></li>
13-
<li><a href="documentation#availableFunctionsTimestamp">&nbsp;&nbsp;&nbsp;&nbsp;Timestamp</a></li>
14-
<li><a href="documentation#globalRequire">Global Requires</a></li>
10+
<li><a href="documentation#availableFunctionsRandom">&nbsp;&nbsp;&nbsp;&nbsp;random</a></li>
11+
<li><a href="documentation#availableFunctionsList">&nbsp;&nbsp;&nbsp;&nbsp;list</a></li>
12+
<li><a href="documentation#availableFunctionsHash">&nbsp;&nbsp;&nbsp;&nbsp;hash</a></li>
13+
<li><a href="documentation#availableFunctionsTimestamp">&nbsp;&nbsp;&nbsp;&nbsp;timestamp</a></li>
14+
<li><a href="documentation#availableFunctionsGetVar">&nbsp;&nbsp;&nbsp;&nbsp;getVar</a></li>
15+
<li><a href="documentation#availableFunctionsMisc">&nbsp;&nbsp;&nbsp;&nbsp;Misc.</a></li>
16+
<li><a href="documentation#globalSnippets">Global Snippets</a></li>
1517
<li><a href="documentation#snippets">Snippets</a></li>
18+
<li><a href="documentation#codingSnippets">&nbsp;&nbsp;&nbsp;&nbsp;Coding</a></li>
19+
<li><a href="documentation#publishingSnippets">&nbsp;&nbsp;&nbsp;&nbsp;Publishing</a></li>
1620
<li><a href="documentation#callingAPIs">Calling APIs</a></li>
1721
<li><a href="documentation#options">&nbsp;&nbsp;&nbsp;&nbsp;Options</a></li>
1822
<li><a href="documentation#errors">&nbsp;&nbsp;&nbsp;&nbsp;Errors</a></li>

0 commit comments

Comments
 (0)