<commit_before>var request = require('request')
  , Parser = require('./lib/parser')
  , split = require('split')
  , multiparty = require('multiparty')
  , ss = require('stream-stream')
  , api = 'https://www.googleapis.com'

var Gmail = function (key) {
  if (!key) {
    throw new Error('Access key required')
  }
  this.key = key
}

/*
 * Fetches email that matches the query. Returns a stream of messages with a max of 100 messages
 * since the batch api sets a limit of 100.
 *
 * e.g. to search an inbox: gmail.messages('label:inbox')
 */
Gmail.prototype.messages = function (q, opts) {
  var key = this.key
    , result = new Parser({objectMode: true})
    , combined = ss()
    , opts = opts || {}

  request({
    url: api + '/gmail/v1/users/me/messages',
    json: true,
    qs: {
      q: q
    },
    headers: {
      'Authorization': 'Bearer ' + key
    }
  }, function (err, response, body) {
    if (err) {
      return result.emit('error', err)
    }

    var messages = body.messages.map(function (m) {
      return {
        'Content-Type': 'application/http',
        body: 'GET ' + api + '/gmail/v1/users/me/messages/' + m.id + '\n'
      }
    })

    result.resultSizeEstimate = body.resultSizeEstimate
    messages.length = opts.max || 100

    var r = request({
      method: 'POST',
      url: api + '/batch',
      multipart: messages,
      headers: {
        'Authorization': 'Bearer ' + key,
        'content-type': 'multipart/mixed'
      }
    })

    r.on('error', function (e) {
      result.emit('error', e)
    })

    r.on('response', function (res) {
      var type = res.headers['content-type']
        , form = new multiparty.Form

      res.headers['content-type'] = type.replace('multipart/mixed', 'multipart/related')

      form.on('part', function (part) {
        var parser = new Parser
          , splitter = split('\r\n')

        combined.write(part.pipe(splitter).pipe(parser))
      }).parse(res)
      form.on('close', function () {
        combined.end()
      })

    })
  })

  return combined.pipe(result)
}

module.exports = Gmail
<commit_msg>favor terseness<commit_after>var request = require('request')
  , Parser = require('./lib/parser')
  , split = require('split')
  , multiparty = require('multiparty')
  , ss = require('stream-stream')
  , api = 'https://www.googleapis.com'

var Gmail = function (key) {
  if (!key) {
    throw new Error('Access key required')
  }
  this.key = key
}

/*
 * Fetches email that matches the query. Returns a stream of messages with a max of 100 messages
 * since the batch api sets a limit of 100.
 *
 * e.g. to search an inbox: gmail.messages('label:inbox')
 */
Gmail.prototype.messages = function (q, opts) {
  var key = this.key
    , result = new Parser({objectMode: true})
    , combined = ss()
    , opts = opts || {}

  request({
    url: api + '/gmail/v1/users/me/messages',
    json: true,
    qs: {
      q: q
    },
    headers: {
      'Authorization': 'Bearer ' + key
    }
  }, function (err, response, body) {
    if (err) {
      return result.emit('error', err)
    }

    var messages = body.messages.map(function (m) {
      return {
        'Content-Type': 'application/http',
        body: 'GET ' + api + '/gmail/v1/users/me/messages/' + m.id + '\n'
      }
    })

    result.resultSizeEstimate = body.resultSizeEstimate
    messages.length = opts.max || 100

    var r = request({
      method: 'POST',
      url: api + '/batch',
      multipart: messages,
      headers: {
        'Authorization': 'Bearer ' + key,
        'content-type': 'multipart/mixed'
      }
    })

    r.on('error', function (e) {
      result.emit('error', e)
    })

    r.on('response', function (res) {
      var type = res.headers['content-type']
        , form = new multiparty.Form

      res.headers['content-type'] = type.replace('multipart/mixed', 'multipart/related')

      form.on('part', function (part) {
        combined.write(part.pipe(split('\r\n')).pipe(new Parser))
      }).parse(res)
      form.on('close', function () {
        combined.end()
      })

    })
  })

  return combined.pipe(result)
}

module.exports = Gmail

{'fraction_non_alphanumeric': 0.1614771904417089, 'fraction_numerical': 0.005310161718561429, 'mean_word_length': 2.3995077932731745, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 2, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4677728', 'n_tokens_mistral': 1389, 'n_tokens_neox': 1342, 'n_words': 324}