randomorg-js

1.0.0 • Public • Published

npm mit license build status coverage status deps status

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

For more use-cases see the tests and examples

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

params

  • [body] {Object} body object that will send to api
  • [statusCb] {Function} callback that recieves request status
  • return {RandomJs}

.request

Get status of request that will be send to API

params

  • [statusCb] {Function} callback that recieves request status
  • return {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 status
  • return {RandomJs}

.jsonrpc

Set version of Random.Org JSON RPC API

params

  • <value> {String} default '2.0'
  • [statusCb] {Function} callback that recieves request status
  • return {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 status
  • return {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 status
  • return {RandomJs}

.id

Set id request body

params

  • <value> {Object} default (0 | Math.random() * 1000)
  • [statusCb] {Function} callback that recieves request status
  • return {RandomJs}

.url

Set url to the api endpoint it's always https://api.random.org/json-rpc/1/invoke

params

  • <value> {String} default https://api.random.org/json-rpc/1/invoke
  • [statusCb] {Function} callback that recieves request status
  • return {RandomJs}

.uri

Short-hand for .url

params

  • <value> {String} same as .url
  • [statusCb] {Function} callback that recieves request status
  • return {RandomJs}

.callback

Callback that will handle the response.

params

  • <fn> {Function} cb(xhrOrErr, stream, body)
  • [statusCb] {Function} callback that recieves request status
  • return {RandomJs}

You must provide function with 3 arguments that are

  • xhrOrErr {Object} if browser, will be xhr request, else error
  • stream {Stream} if browser, will be null, 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 status
  • return {RandomJs}

.post

Send request to the JSON-RPC API

params

  • [done] {Boolean|Function} if false, will use .callback
  • [statusCb] {Function} callback that recieves request status
  • return {RandomJs}

Author

Charlike Mike Reagent

License MIT license

Copyright (c) 2014 Charlike Mike Reagent, contributors.
Released under the MIT license.


Powered and automated by readdirp + hogan.js, December 30, 2014

Package Sidebar

Install

npm i randomorg-js

Weekly Downloads

0

Version

1.0.0

License

MIT

Last publish

Collaborators

  • vanchoy
  • tunnckocore