"use strict";
var _                = require('underscore');
var co               = require('co');
var Q                = require('q');
var async            = require('async');
var http2raw         = require('../lib/streams/parsers/http2raw');
var parsers          = require('../lib/streams/parsers/doc');
var constants        = require('../lib/constants');
var Peer             = require('../lib/entity/peer');
var AbstractController = require('./abstract');

module.exports = function (server) {
  return new NetworkBinding(server);
};

function NetworkBinding (server) {

  AbstractController.call(this, server);

  // Services
  var MerkleService     = server.MerkleService;
  var PeeringService    = server.PeeringService;

  this.cert = PeeringService.cert;

  this.peer = () => co(function *() {
    var p = yield PeeringService.peer();
    if (!p) {
      throw constants.ERRORS.SELF_PEER_NOT_FOUND;
    }
    return p.json();
  });

  this.peersGet = (req) => co(function *() {
    let merkle = yield server.dal.merkleForPeers();
    return Q.nfcall(MerkleService.processForURL, req, merkle, function (hashes, done) {
      server.dal.findPeersWhoseHashIsIn(hashes)
        .then(function(peers) {
          var map = {};
          peers.forEach(function (peer){
            map[peer.hash] = Peer.statics.peerize(peer).json();
          });
          if (peers.length == 0) {
            done(constants.ERRORS.PEER_NOT_FOUND);
          }
          done(null, map);
        })
        .catch(done);
    });
  });

  this.peersPost = (req) => this.pushEntity(req, http2raw.peer, parsers.parsePeer);

  this.peers = () => co(function *() {
    let peers = yield server.dal.listAllPeers();
    return {
      peers: peers.map((p) => {
        return _.pick(p,
          'version',
          'currency',
          'status',
          'first_down',
          'last_try',
          'pubkey',
          'block',
          'signature',
          'endpoints');
      })
    };
  });
}

@@ -1125,16 +1125,83 @@
 %7B%0A      
+return co(function *() %7B%0A        try %7B%0A          let peers = yield 
 server.d
@@ -1237,40 +1237,9 @@
 hes)
-%0A        .then(function(peers) %7B
+;
 %0A   
@@ -1516,29 +1516,86 @@
    %7D
-)%0A        .catch(done
+ catch (e) %7B%0A          if (done) done(e);%0A          throw e;%0A        %7D%0A      %7D
 );%0A 

Refactor /peering/peers merkle URI code