UNPKG

bible-search

Version:

A client Bible Search Api wrapper for NodeJs https://pt-br.bibles.org/pages/api

122 lines (98 loc) 3.73 kB
'use strict'; Object.defineProperty(exports, '__esModule', { value: true }); var _createClass = (function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ('value' in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; })(); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { 'default': obj }; } function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError('Cannot call a class as a function'); } } var _util = require('util'); var _util2 = _interopRequireDefault(_util); var _request = require('request'); var _request2 = _interopRequireDefault(_request); var _q = require('q'); var _q2 = _interopRequireDefault(_q); var promise = _q2['default'].Promise; /** * Bible Api resource class. * @class * @borrows passage as BibleApiResource */ var BibleApiResource = (function () { /** * Creates a new Bible Api Resource instance. * @constructs BibleApiResource. * @param {string} apiKey - The Bible Api Resource API Key. * @param {string} baseUrl - The base url (Default: https://pt-br.bibles.org/v2/). */ function BibleApiResource(apiKey, baseUrl) { _classCallCheck(this, BibleApiResource); /** * The Bible Api Resource API Key. * @member BibleApiResource#apiKey. */ this.apiKey = apiKey; /** * The Bible Api Resource Base URL. * @member BibleApiResource#baseUrl. */ this.baseUrl = baseUrl; } /** * Validate arguments * @param {Object} args - Arguments * @param {Array} requiredArgs - Required arguments */ _createClass(BibleApiResource, [{ key: 'validate', value: function validate(args, requiredArgs) { requiredArgs = requiredArgs || []; requiredArgs.forEach(function (arg) { if (typeof args[arg] === 'undefined') { throw new Error(_util2['default'].format('Argument %s not defined.', arg)); } }); } /** * Get url to consume bible api resource. * @param {Object} args - The url arguments. * @param {string} args.baseUrl - The base url (Default: https://pt-br.bibles.org/v2/). * @param {string} args.resource - The url resource (passages, version). * @param {string} args.query - The query string of resource. * @returns {string} */ }, { key: 'getUrl', value: function getUrl(args) { args.query = args.query || ''; return _util2['default'].format('%s%s.js%s', args.baseUrl, args.resource, args.query !== '' ? '?' + args.query : ''); } /** * Using format JSON to consume the Bible API Resource (Using promises). * @param {string} url - The url. * @returns {Promise} */ }, { key: 'getJson', value: function getJson(url) { var options = { url: url, json: true, auth: { user: this.apiKey, sendImmediately: false } }; return promise(function (resolve, reject) { return (0, _request2['default'])(options, function (error, response, body) { if (error) { return reject(new Error(error)); } return resolve(body); }); }); } }]); return BibleApiResource; })(); exports['default'] = BibleApiResource; module.exports = exports['default'];