all files / cheerio-httpcli/lib/ core.js

100% Statements 17/17
100% Branches 4/4
100% Functions 3/3
100% Lines 17/17
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                                                                            12× 11× 11×                         92×            
/*eslint key-spacing:0*/
'use strict';
 
var cheerioExtend = require('./cheerio-extend');
var encoding      = require('./encoding');
var client        = require('./client');
var pkg           = require('../package.json');
var browsers      = require('./browsers.json');
 
/**
 * cheerio-httpcliモジュール本体
 */
var cheerioHttpCli = {
  /**
   * プロパティ
   */
 
  version : pkg.version, // バージョン情報
  headers : {},          // リクエストヘッダ
  timeout : 30000,       // タイムアウトまでの時間(効いているかどうか不明)
  gzip    : true,        // gzip転送する/しない
  referer : true,        // Refererを自動設定する/しない
  debug   : false,       // デバッグオプション
 
  /**
   * メソッド
   */
 
  /**
   * 使用するiconvモジュールを指定
   *
   * @param icmod iconvモジュール名(iconv|iconv-jp|iconv-lite)
   */
  setIconvEngine: function (icmod) {
    if (! encoding.iconvLoad(icmod)) {
      throw new Error('Cannot find module "' + icmod + '"');
    }
  },
 
  /**
   * ブラウザごとのUser-Agentをワンタッチ設定
   *
   * @param browser ブラウザ種類(see browsers.json)
   * @return 設定できた/できなかった
   */
  setBrowser: function (type) {
    if (type in browsers) {
      this.headers['User-Agent'] = browsers[type];
      return true;
    }
    return false;
  },
 
  /**
   * GETによるhttpリクエストを実行
   *
   * @param url      リクエスト先のURL
   * @param param    リクエストパラメータ
   * @param encode   取得先のHTMLのエンコーディング(default: 自動判定)
   * @param callback リクエスト完了時のコールバック関数(err, cheerio, response, body)
   */
  fetch: function (url, param, encode, callback) {
    return client.run('GET', url, param, encode, callback);
  }
};
 
// clientオブジェクト内で使用する外部オブジェクトを登録
client.core = cheerioHttpCli;
client.encoding = encoding;
client.cheerio = cheerioExtend(encoding, client);
 
module.exports = cheerioHttpCli;