<commit_before>var querystring = require('querystring');
var url = require('url');

var buildActions = exports.buildActions = function(env, machine) {
  var actions = null;


  Object.keys(machine.transitions).forEach(function(type) {
    var transition = machine.transitions[type];
    var fields = transition.fields ? [].concat(transition.fields) : [];
    fields.push({ name: 'action', type: 'hidden', value: type });

    var action = {
      name: type,
      method: 'POST',
      href: null,
      fields: fields
    };
    if (!actions) {
      actions = [];
    }

    actions.push(action);
  });

  machine.streams.forEach(function(name) {
    var fields = [];
    fields.push({ name: 'name', type: 'hidden', value: name });

    var action = {
      class: ['event-subscription'],
      name: name.replace('/', '-') + '-subscribe',
      method: 'subscribe',
      href: env.helpers.url.path('/').replace('http', 'ws') + 'events',
      fields: fields
    };

    if (!actions) {
      actions = [];
    }

    actions.push(action);
  });

  return actions;
};

var buildEntity = exports.buildEntity = function buildEntity(loader, env, machine, actions, selfPath) {
  machine.update();
  selfPath = selfPath || env.helpers.url.current();

  var entity = {
    class: [machine.type],
    properties: machine.properties,
    entities: undefined,
    actions: actions,
    links: [{ rel: ['self'], href: selfPath },
            { rel: ['index'], href: env.helpers.url.path(loader.path) }]
  };

  if (machine._devices.length) {
    entity.entities = machine._devices.filter(function(device) {
      var path = env.helpers.url.join(device.name);

      if (loader.exposed[url.parse(path).path]) {
        return device;
      }
    }).map(function(device) {
      var path = env.helpers.url.join(device.name);
      return buildEntity(env, device, null, path)
    });

  }

  if (entity.actions) {
    entity.actions.forEach(function(action) {
      if (!action.href) {
        action.href = env.helpers.url.current();
      }
    });

    entity.actions = entity.actions.filter(function(action) {
      if (action.class && action.class.indexOf('event-subscription') !== -1) {
        return action;
      }

      var allowed = machine.allowed[machine.state];
      if (allowed && allowed.indexOf(action.name) > -1) {
        return action;
      }
    });
  }

  return entity;
};

exports.create = function(loader) {
  var DeviceResource = function() {
    this.path = loader.path;
  };


  DeviceResource.prototype.init = function(config) {
    config.path(this.path)
      .produces('application/vnd.siren+json')
      .consumes('application/x-www-form-urlencoded')
      .consumes('multipart/form-data')
      .get('/', this.home)
      .get('/{splat: (.*)}', this.show)
      .post('/{splat: (.*)}', this.action)
  };

  DeviceResource.prototype.home = function(env, next) {
    var entity = {
      class: ['server'],
      entities: [],
      links: [ { rel: ['self'], href: env.helpers.url.path(this.path) } ]
    };

    Object.keys(loader.exposed).forEach(function(path) {
      var machine = loader.exposed[path];
      entity.entities.push({
        class: ['machine'],
        rel: ['http://rels.zettajs.io/machine'],
        properties: machine.properties,
        links: [ { rel: ['self'], href: env.helpers.url.path(path) } ]
      })
    });

    entity.entities = entity.entities.sort(function(a,b){
      if(a.properties.name < b.properties.name)
  return -1;
      else if(a.properties.name > b.properties.name)
  return 1;
      else
  return 0;
    });



    env.response.body = entity;
    next(env);
  };

  DeviceResource.prototype.show = function(env, next) {
    // match path
    // load machine
    // build representation
    // don't forget subdevices

    var machine = loader.exposed[this.path + '/' + env.route.params.splat];
    if (!machine) {
      // return 404<commit_msg>Cleaned up formating for sorting device code<commit_after>var querystring = require('querystring');
var url = require('url');

var buildActions = exports.buildActions = function(env, machine) {
  var actions = null;


  Object.keys(machine.transitions).forEach(function(type) {
    var transition = machine.transitions[type];
    var fields = transition.fields ? [].concat(transition.fields) : [];
    fields.push({ name: 'action', type: 'hidden', value: type });

    var action = {
      name: type,
      method: 'POST',
      href: null,
      fields: fields
    };
    if (!actions) {
      actions = [];
    }

    actions.push(action);
  });

  machine.streams.forEach(function(name) {
    var fields = [];
    fields.push({ name: 'name', type: 'hidden', value: name });

    var action = {
      class: ['event-subscription'],
      name: name.replace('/', '-') + '-subscribe',
      method: 'subscribe',
      href: env.helpers.url.path('/').replace('http', 'ws') + 'events',
      fields: fields
    };

    if (!actions) {
      actions = [];
    }

    actions.push(action);
  });

  return actions;
};

var buildEntity = exports.buildEntity = function buildEntity(loader, env, machine, actions, selfPath) {
  machine.update();
  selfPath = selfPath || env.helpers.url.current();

  var entity = {
    class: [machine.type],
    properties: machine.properties,
    entities: undefined,
    actions: actions,
    links: [{ rel: ['self'], href: selfPath },
            { rel: ['index'], href: env.helpers.url.path(loader.path) }]
  };

  if (machine._devices.length) {
    entity.entities = machine._devices.filter(function(device) {
      var path = env.helpers.url.join(device.name);

      if (loader.exposed[url.parse(path).path]) {
        return device;
      }
    }).map(function(device) {
      var path = env.helpers.url.join(device.name);
      return buildEntity(env, device, null, path)
    });

  }

  if (entity.actions) {
    entity.actions.forEach(function(action) {
      if (!action.href) {
        action.href = env.helpers.url.current();
      }
    });

    entity.actions = entity.actions.filter(function(action) {
      if (action.class && action.class.indexOf('event-subscription') !== -1) {
        return action;
      }

      var allowed = machine.allowed[machine.state];
      if (allowed && allowed.indexOf(action.name) > -1) {
        return action;
      }
    });
  }

  return entity;
};

exports.create = function(loader) {
  var DeviceResource = function() {
    this.path = loader.path;
  };


  DeviceResource.prototype.init = function(config) {
    config.path(this.path)
      .produces('application/vnd.siren+json')
      .consumes('application/x-www-form-urlencoded')
      .consumes('multipart/form-data')
      .get('/', this.home)
      .get('/{splat: (.*)}', this.show)
      .post('/{splat: (.*)}', this.action)
  };

  DeviceResource.prototype.home = function(env, next) {
    var entity = {
      class: ['server'],
      entities: [],
      links: [ { rel: ['self'], href: env.helpers.url.path(this.path) } ]
    };

    Object.keys(loader.exposed).forEach(function(path) {
      var machine = loader.exposed[path];
      entity.entities.push({
        class: ['machine'],
        rel: ['http://rels.zettajs.io/machine'],
        properties: machine.properties,
        links: [ { rel: ['self'], href: env.helpers.url.path(path) } ]
      })
    });

    entity.entities = entity.entities.sort(function(a,b){
      if(a.properties.name < b.properties.name) {
	return -1;
      } else if(a.properties.name > b.properties.name) {
	return 1;
      } else {
	return 0;
      }
    });

    env.response.body = entity;
    next(env);
  };

  DeviceResource.prototype.show = function(env, next) {
    // match path
    // load machine
    // build representation
    // don't forget subdevices

    var machine = loader.exposed[this.path + '/' + env.route.params.splat];
    if (!machine) {
      // return 404
{'fraction_non_alphanumeric': 0.16814608166370784, 'fraction_numerical': 0.002028911995942176, 'mean_word_length': 2.6312154696132595, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 7, 'https://': 0, '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': '5249321', 'n_tokens_mistral': 2490, 'n_tokens_neox': 2438, 'n_words': 479}