Code coverage report for gingerbread/index.js

Statements: 100% (30 / 30)      Branches: 81.25% (13 / 16)      Functions: 100% (2 / 2)      Lines: 100% (30 / 30)      Ignored: none     

All files » gingerbread/ » index.js
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 751       1             1 4 2 2     4   4 4   4 4 1     3 3   1     2               2 4   4 4 4     4 4   4 4   4                 4     2 2     2      
var request = require('request'),
    url = require('url'),
    _ = require('underscore');
 
var defaults = {
  apiEndpoint: 'http://services.gingersoftware.com/Ginger/correct/json/GingerTheText',
  apiKey: '6ae0c3a0-afdc-4532-a810-82ded0054236',
  apiVersion: '2.0',
  lang: 'US',
};
 
module.exports = function (text, options, callback) {
  if (callback === undefined && typeof options === 'function') {
    callback = options;
    options = {};
  }
 
  options = _.defaults(options, defaults);
 
  var uri = url.parse(options.apiEndpoint);
  uri.query = {text: text, lang: options.lang, apiKey: options.apiKey, clientVersion: options.apiVersion};
 
  request({uri: url.format(uri)}, function (error, response, body) {
    if (error) {
      return callback(new Error('Couldn\'t connect to API endpoint (' + options.apiEndpoint + ')'));
    }
 
    try {
      var data = JSON.parse(body);
    } catch (error) {
      return callback(new Error('Received an invalid JSON format'));
    }
 
    var suggestion,
        currentSuggestion,
        definition,
        index,
        result = '',
        corrections = [],
        i = 0;
 
    for (index in data.LightGingerTheTextResult) {
      suggestion = data.LightGingerTheTextResult[index];
 
      Eif (i <= suggestion.From) {
        Eif (suggestion.From !== 0) {
          result += text.substr(i, suggestion.From - i);
        }
 
        currentSuggestion = suggestion.Suggestions[0] || { Text: '', Definition: '' };
        result += currentSuggestion.Text;
 
        definition = currentSuggestion.Definition;
        definition = definition === undefined ? null : definition;
 
        corrections.push({
          text: text.substr(suggestion.From, suggestion.To - suggestion.From + 1),
          correct: currentSuggestion.Text,
          definition: definition,
          start: suggestion.From,
          length: suggestion.To - suggestion.From + 1,
        });
      }
 
      i = suggestion.To + 1;
    }
 
    Eif (i < text.length) {
      result += text.substring(i);
    }
 
    callback(null, text, result, corrections);
  });
};