<commit_before>'use strict';

var util = require('util'),
    R = require('ramda'),
    requester = libRequire('requester'),
    formatter = libRequire('formatter'),
    flightCache = libRequire('flightCache'),
    cacheChecker = libRequire('cacheChecker');

function filter(query, flights) {
  var allowedParams = ['airline.code', 'airport.name'],
      passedParams = R.filter(R.rPartial(R.has, query), allowedParams),
      filters = R.map(function (param) {
        return R.pathEq(param, query[param]);
      }, passedParams),
      results = R.filter(R.allPredicates(filters), flights);

  return { flights: results };
}

function requestFlights(direction) {
  var cache = flightCache(direction),
      existing = cache.get();

  return requester[direction](existing).then(function (result) {
    // no changes use cached verson
    if (result.status === 304) {
      console.log('304 using cached version');
      return {
        status: result.status,
        etag: existing.etag,
        lastModified: existing.lastModified,
        body: existing.flights
      };
    } else {
      return formatter.flights(result.body)
        .then(function (flights) {
          console.log('200 using returned version');
          cache.put(result.etag, result.lastModified, flights);

          return {
            status: result.status,
            etag: result.etag,
            lastModified: result.lastModified,
            body: flights
          };
      });
    }
  });
}

function index(req, res) {
  var url = util.format('%s://%s', req.protocol, req.get('host'));

  res.send({
    links: {
      flights: {
        arrivals: {
          type: 'flights',
          href: util.format('%s/v1/flights/arrivals', url)
        },
        departures: {
          type: 'flights',
          href: util.format('%s/v1/flights/departures', url)
        }
      }
    }
  });
}

var flights = R.curry(function flights(direction, req, res) {
  requestFlights(direction).done(function (flights) {
    res.set({
      'ETag': flights.etag,
      'Last-Modified': flights.lastModified
    });

    if (R.isEmpty(req.query) &&
       ((req.get('If-None-Match') === flights.etag) ||
       (req.get('If-Modified-Since') === flights.lastModified))) {

      res.sendStatus(304);
      return;
    }

    res.json(cacheChecker.check(req, filter(req.query, flights.body)));
  });
});

module.exports = {
  index: index,
  flights: flights
};<commit_msg>feat: Added JSONP support<commit_after>'use strict';

var util = require('util'),
    R = require('ramda'),
    requester = libRequire('requester'),
    formatter = libRequire('formatter'),
    flightCache = libRequire('flightCache'),
    cacheChecker = libRequire('cacheChecker');

function filter(query, flights) {
  var allowedParams = ['airline.code', 'airport.name'],
      passedParams = R.filter(R.rPartial(R.has, query), allowedParams),
      filters = R.map(function (param) {
        return R.pathEq(param, query[param]);
      }, passedParams),
      results = R.filter(R.allPredicates(filters), flights);

  return { flights: results };
}

function requestFlights(direction) {
  var cache = flightCache(direction),
      existing = cache.get();

  return requester[direction](existing).then(function (result) {
    // no changes use cached verson
    if (result.status === 304) {
      console.log('304 using cached version');
      return {
        status: result.status,
        etag: existing.etag,
        lastModified: existing.lastModified,
        body: existing.flights
      };
    } else {
      return formatter.flights(result.body)
        .then(function (flights) {
          console.log('200 using returned version');
          cache.put(result.etag, result.lastModified, flights);

          return {
            status: result.status,
            etag: result.etag,
            lastModified: result.lastModified,
            body: flights
          };
      });
    }
  });
}

function index(req, res) {
  var url = util.format('%s://%s', req.protocol, req.get('host'));

  res.jsonp({
    links: {
      flights: {
        arrivals: {
          type: 'flights',
          href: util.format('%s/v1/flights/arrivals', url)
        },
        departures: {
          type: 'flights',
          href: util.format('%s/v1/flights/departures', url)
        }
      }
    }
  });
}

var flights = R.curry(function flights(direction, req, res) {
  requestFlights(direction).done(function (flights) {
    res.set({
      'ETag': flights.etag,
      'Last-Modified': flights.lastModified
    });

    if (R.isEmpty(req.query) &&
       ((req.get('If-None-Match') === flights.etag) ||
       (req.get('If-Modified-Since') === flights.lastModified))) {

      res.sendStatus(304);
      return;
    }

    res.jsonp(cacheChecker.check(req, filter(req.query, flights.body)));
  });
});

module.exports = {
  index: index,
  flights: flights
};
{'fraction_non_alphanumeric': 0.15197878416972666, 'fraction_numerical': 0.005711954304365565, 'mean_word_length': 2.558055152394775, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '2820490', 'n_tokens_mistral': 1521, 'n_tokens_neox': 1495, 'n_words': 289}