randomorg-js
v1.0.0
Published
Streaming Random.org JSON-RPC Javascript API - for node, command line (cli) and the browser.
Downloads
10
Readme
Streaming Random.org JSON-RPC Javascript API - for node, command line (cli) and the browser.
Install
npm install randomorg-js
npm test
Include in html
After that you can use directly
new RandomJs()
class.
<script src="https://cdn.rawgit.com/tunnckoCore/randomorg-js/master/dist/randomorg-js.js"></script>
Usage in node.js
var RandomJs = require('randomorg-js');
var randomJs = new RandomJs();
var result = randomJs
.apikey('6b1e65b9-4186-45c2-8981-b77a9842c4f0') // your apikey here
.headers({'User-Agent': 'https://github.com/tunnckoCore/randomorg-js'})
.method('generateStrings')
.params({n:4,length:11})
.post(function(error, stream, body) {
console.log('==START==')
console.log('==error==')
console.log(error)
console.log('==stream==')
console.log(stream)
console.log('==body==')
console.log(body)
console.log('==END==')
});
Usage in browser
var randomJs = new RandomJs();
var result = randomJs
.apikey('6b1e65b9-4186-45c2-8981-b77a9842c4f0') // your apikey here
.method('generateStrings')
.params({n:4,length:11})
.post(function(xhr, stream, body) {
console.log('==START==')
console.log('==xhr==')
console.log(xhr)
console.log('==stream==')
console.log(stream)
console.log('==body==')
console.log(body)
console.log('==END==')
});
API
RandomJs(body)
Initialize a new
RandomJs
instance with[body]
(optional) object.
See also https://api.random.org/json-rpc/1/basic
members
- request(statusCb)
- apikey(value, statusCb)
- jsonrpc(value, statusCb)
- method(value, statusCb)
- params(value, statusCb)
- id(value, statusCb)
- url(value, statusCb)
- uri(value, statusCb)
- callback(fn, statusCb)
- headers(object, statusCb)
- post(done, statusCb)
params
[body]
{Object} body object that will send to api[statusCb]
{Function} callback that recieves request statusreturn
{RandomJs}
.request
Get status of request that will be send to API
params
[statusCb]
{Function} callback that recieves request statusreturn
{RandomJs|Object} returns self or RandomJs._request object
.apikey
Set your API key
params
<apikey>
{String} your api key with that you will auth to api[statusCb]
{Function} callback that recieves request statusreturn
{RandomJs}
.jsonrpc
Set version of Random.Org JSON RPC API
params
<value>
{String} default'2.0'
[statusCb]
{Function} callback that recieves request statusreturn
{RandomJs}
.method
Set which rpc method to use (see https://api.random.org/json-rpc/1/basic)
params
<value>
{String} default'generateIntegers'
[statusCb]
{Function} callback that recieves request statusreturn
{RandomJs}
.params
Set params object that will be attached to the request body
params
<value>
{Object} see defaults at index.js#L21-55,generateIntegers
's defaults[statusCb]
{Function} callback that recieves request statusreturn
{RandomJs}
.id
Set id request body
params
<value>
{Object} default(0 | Math.random() * 1000)
[statusCb]
{Function} callback that recieves request statusreturn
{RandomJs}
.url
Set url to the api endpoint it's always
https://api.random.org/json-rpc/1/invoke
params
<value>
{String} defaulthttps://api.random.org/json-rpc/1/invoke
[statusCb]
{Function} callback that recieves request statusreturn
{RandomJs}
.uri
Short-hand for
.url
params
<value>
{String} same as .url[statusCb]
{Function} callback that recieves request statusreturn
{RandomJs}
.callback
Callback that will handle the response.
params
<fn>
{Function} cb(xhrOrErr
,stream
,body
)[statusCb]
{Function} callback that recieves request statusreturn
{RandomJs}
You must provide function with 3 arguments that are
xhrOrErr
{Object} if browser, will bexhr request
, elseerror
stream
{Stream} if browser, will benull
, else [request's stream
][request-url]body
{Object} always, response body object of the request
.headers
Headers that will send with request.
Always append{'Content-Type': 'application/json'}
header to others.
params
<object>
{Object} default{'Content-Type': 'application/json'}
[statusCb]
{Function} callback that recieves request statusreturn
{RandomJs}
.post
Send request to the JSON-RPC API
params
[done]
{Boolean|Function} iffalse
, will use .callback[statusCb]
{Function} callback that recieves request statusreturn
{RandomJs}
Author
Charlike Mike Reagent
License
Copyright (c) 2014 Charlike Mike Reagent, contributors.
Released under the MIT
license.
Powered and automated by readdirp + hogan.js, December 30, 2014