var router = require('express').Router();

var db = require("../models"),
    User = db.User
    Tessel = db.Tessel;

// extracts an API key (if present) from a request
var getAPIKey = function(req) {
  var headerRegex = /^Bearer (\w+)/;

  // check in Authorization header
  if (headerRegex.test(req.headers.authorization)) {
    return req.headers.authorization.match(headerRegex)[1];
  }

  // check URL query string (only with GET requests)
  if (req.method === "GET") {
    if (req.query.api_key !== undefined && /\w+/.test(req.query.api_key)) {
      return req.query.api_key;
    }
  }

  // check POST body
  if (req.method === "POST") {
    if (req.body.api_key !== undefined && /\w+/.test(req.body.api_key)) {
      return req.body.api_key;
    }
  }

  return false;
};

// intercept and return error for all requests that don't contain an API key
router.get("/*", function(req, res, next) {
  var json = {
    code: 400,
    error: "invalid_request",
    error_description: "The API key was not found"
  };

  var key = getAPIKey(req);

  if (key) {
    req.api_key = key;
    next();
  } else {
    res.status = 400;
    res.json(json);
  }
});

// # GET /v1/tessels
//
// Lists all tessels belonging to the currently authenticated user
router.get("/tessels", function(req, res) {
  User
    .find({ where: { apiKey: req.api_key }, include: [ Tessel ] })
    .success(function(user) {
      var json = [];

      user.tessels.forEach(function(tessel) {
        json.push({ id: tessel.device_id });
      });

      res.json(json);
    });
});

module.exports = router;

@@ -1158,10 +1158,8 @@
 %0A%0A//
- #
  GET
@@ -1171,16 +1171,16 @@
 tessels%0A
+
 //%0A// Li
@@ -1535,16 +1535,16 @@
 (json);%0A
-
     %7D);%0A
@@ -1548,16 +1548,633 @@
 );%0A%7D);%0A%0A
+// GET /v1/tessels/:device_id%0A//%0A// Returns data on a specific tessel, if the user has access%0Arouter.get(%22/tessels/:device_id%22, function(req, res) %7B%0A  Tessel.find(%7B%0A    include: %5B User %5D,%0A    where: %7B device_id: req.params.device_id, %7D%0A  %7D).success(function(tessel) %7B%0A    var users = tessel.users.filter(function(user) %7B%0A      return user.apiKey === req.api_key;%0A    %7D);%0A%0A    if (users.length === 0) %7B%0A      res.status = 403;%0A      res.json(%7B%0A        error: %22Permission Denied%22,%0A        info: %22I didn't recognize that core name or ID%22%0A      %7D);%0A    %7D else %7B%0A      res.json(%7B id: tessel.device_id %7D);%0A    %7D%0A  %7D);%0A%7D);%0A%0A
 module.e

Add route for getting details on individual tessels