All files / src/tumblr index.js

100% Statements 16/16
100% Branches 8/8
100% Functions 4/4
100% Lines 16/16
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106                                                                  11x 11x 11x 10x     10x 10x 10x                           12x 1x 1x   12x                                     4x 1x 1x   4x                                   1x          
import isFunction from 'lodash.isfunction';
import Core from '../core/core';
import TumblrStream from './stream';
 
/**
 * @class Tumblr
 * @description Tumblr class
 * @example
 * const tumblr = new Tumblr({
 *  consumerKey: 'your-consumer-key',
 * });
 *
 * tumblr.get('some-tumblr-route').then((data) => {
 *  console.log(data);
 * });
 *
 * const stream = tumblr.stream('blog/museumdemain.tumblr.com/posts');
 *
 * stream.on('message', (message) => {
 *  console.log(message);
 * });
 */
class Tumblr extends Core {
  /**
   * @constructs Tumblr
   * @description Constructs an instance of Tumblr.
   *
   * @param {object} config - Config of class.
   * @param {string} config.consumerKey - consumerKey of Tumblr app.
   * @param {object} [options] - Options of class.
   *
   */
  constructor(config = {}, options = {}) {
    super(options);
    this.name = 'tumblr';
    this.checkValidConfig(['consumerKey'], config);
    this.options = {
      api_key: config.consumerKey,
    };
    this.url = 'https://api.tumblr.com';
    this.version = options.version || 'v2';
    this.baseApiUrl = `${this.url}/${this.version}`;
  }
 
  /**
   * @memberof Tumblr
   * @function get
   * @description Make a get request to tumblr api.
   *
   * @param {string} url - Url of route.
   * @param {object} [options] - Options to pass in request.
   * @param {function} [callback] - Callback to call when the request finish.
   * @return {promise}
   */
  get(url, options, callback) {
    if (isFunction(options)) {
      callback = options;
      options = {};
    }
    return this.request({
      method: 'GET',
      json: true,
      uri: `${this.baseApiUrl}/${url}`,
      qs: Object.assign({}, this.options, options),
    }, callback);
  }
 
  /**
   * @memberof Tumblr
   * @function post
   * @description Make a post request to tumblr api.
   *
   * @param {string} url - Url of route.
   * @param {object} [options] - Options to pass in request.
   * @param {function} [callback] - Callback to call when the request finish.
   * @return {promise}
   */
  post(url, options, callback) {
    if (isFunction(options)) {
      callback = options;
      options = {};
    }
    return this.request({
      method: 'POST',
      json: true,
      uri: `${this.baseApiUrl}/${url}`,
      form: Object.assign({}, this.options, options),
    }, callback);
  }
 
  /**
   * @memberof Tumblr
   * @function stream
   * @description Create a new tumblr stream.<br />
   *
   * @param  {string} url
   * @param  {Object} [options]
   * @return {TumblrStream} A new tumblr stream.
   */
  stream(url, options) {
    return new TumblrStream(this, url, options);
  }
}
 
export default Tumblr;