Skip to content

Commit

Permalink
Made hypermedia() smartr by not mutating the result when it's an Ar…
Browse files Browse the repository at this point in the history
…ray of URIs, for obvious data modeling issues!
  • Loading branch information
avoidwork committed Sep 18, 2014
1 parent 4b47a28 commit 0182190
Show file tree
Hide file tree
Showing 9 changed files with 23 additions and 50 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# Change Log

## 0.9.3
- Made `hypermedia()` smartr by not mutating the result when it's an Array of URIs, for obvious data modeling issues!

## 0.9.2
- Upgraded deps, gained better CSV decoding

Expand Down
2 changes: 1 addition & 1 deletion doc/global.html
Original file line number Diff line number Diff line change
Expand Up @@ -1826,7 +1826,7 @@ <h5>Returns:</h5>

<span class="jsdoc-message">
Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.2.2</a>
on 2014-09-09T19:17:40-04:00 using the <a
on 2014-09-18T07:45:25-04:00 using the <a
href="https://github.com/terryweiss/docstrap">DocStrap template</a>.
</span>
</footer>
Expand Down
2 changes: 1 addition & 1 deletion doc/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -384,7 +384,7 @@ <h2>License</h2>

<span class="jsdoc-message">
Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.2.2</a>
on 2014-09-09T19:17:40-04:00 using the <a
on 2014-09-18T07:45:25-04:00 using the <a
href="https://github.com/terryweiss/docstrap">DocStrap template</a>.
</span>
</footer>
Expand Down
4 changes: 2 additions & 2 deletions doc/module-tenso.html
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ <h2>
<dt class="tag-version">Version:</dt>
<dd class="tag-version">
<ul class="dummy">
<li>0.9.0</li>
<li>0.9.3</li>
</ul>
</dd>

Expand Down Expand Up @@ -218,7 +218,7 @@ <h2>

<span class="jsdoc-message">
Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.2.2</a>
on 2014-09-09T19:17:40-04:00 using the <a
on 2014-09-18T07:45:25-04:00 using the <a
href="https://github.com/terryweiss/docstrap">DocStrap template</a>.
</span>
</footer>
Expand Down
2 changes: 1 addition & 1 deletion doc/modules.list.html
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ <h2>

<span class="jsdoc-message">
Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.2.2</a>
on 2014-09-09T19:17:40-04:00 using the <a
on 2014-09-18T07:45:25-04:00 using the <a
href="https://github.com/terryweiss/docstrap">DocStrap template</a>.
</span>
</footer>
Expand Down
27 changes: 6 additions & 21 deletions lib/tenso.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@
* @license BSD-3 <https://raw.github.com/avoidwork/tenso/master/LICENSE>
* @link http://avoidwork.github.io/tenso
* @module tenso
* @version 0.9.2
* @version 0.9.3
*/
( function () {
"use strict";

var turtleio = require( "turtle.io" ),
SERVER = "tenso/0.9.2",
SERVER = "tenso/0.9.3",
CONFIG = require( __dirname + "/../config.json" ),
keigai = require( "keigai" ),
util = keigai.util,
Expand Down Expand Up @@ -63,7 +63,7 @@ function Tenso () {
this.rates = {};
this.server = turtleio();
this.server.tenso = this;
this.version = "0.9.2";
this.version = "0.9.3";
}

/**
Expand Down Expand Up @@ -765,7 +765,7 @@ function factory ( arg ) {
*/
function hypermedia ( server, req, rep, headers ) {
var seen = {},
query, page, page_size, nth, root, remove, rewrite, parent;
query, page, page_size, nth, root, parent;

// Parsing the object for hypermedia properties
function parse ( obj, rel, item_collection ) {
Expand Down Expand Up @@ -819,8 +819,6 @@ function hypermedia ( server, req, rep, headers ) {
page = 1;
}

rewrite = false;
remove = [];
nth = Math.ceil( rep.data.result.length / page_size );

if ( nth > 1 ) {
Expand Down Expand Up @@ -854,34 +852,21 @@ function hypermedia ( server, req, rep, headers ) {
} ).join( "&" );
}

array.each( rep.data.result, function ( i, idx ) {
array.each( rep.data.result, function ( i ) {
var uri;

if ( typeof i == "string" && REGEX_SCHEME.test( i ) ) {
rewrite = true;
uri = i.indexOf( "//" ) > -1 ? i : req.parsed.protocol + "//" + req.parsed.host + i;
uri = i.indexOf( "//" ) > -1 ? i : req.parsed.protocol + "//" + req.parsed.host + i;

if ( uri !== root ) {
rep.data.link.push( {uri: uri, rel: "item"} );
remove.push( idx );
}
}

if ( i instanceof Object ) {
parse( i, "item", req.parsed.pathname.replace( REGEX_TRAILING, "" ).replace( REGEX_LEADING, "" ).replace( REGEX_TRAILING_S, "" ) + "s" );
}
} );

// This is for collections of URIs
if ( remove.length > 0 ) {
array.each( remove.reverse(), function ( i ) {
array.remove( rep.data.result, i );
} );
}

if ( rewrite && rep.data.result.length === 0 ) {
rep.data.result = null;
}
}
else if ( rep.data.result instanceof Object ) {
parent = req.parsed.pathname.split( "/" ).filter( function( i ){ return i !== ""; } );
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "tenso",
"description": "Tensō is a REST API facade for node.js, designed to simplify the implementation of APIs.",
"version": "0.9.2",
"version": "0.9.3",
"homepage": "http://avoidwork.github.io/tenso",
"author": {
"name": "Jason Mulligan",
Expand Down
21 changes: 3 additions & 18 deletions src/hypermedia.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
*/
function hypermedia ( server, req, rep, headers ) {
var seen = {},
query, page, page_size, nth, root, remove, rewrite, parent;
query, page, page_size, nth, root, parent;

// Parsing the object for hypermedia properties
function parse ( obj, rel, item_collection ) {
Expand Down Expand Up @@ -69,8 +69,6 @@ function hypermedia ( server, req, rep, headers ) {
page = 1;
}

rewrite = false;
remove = [];
nth = Math.ceil( rep.data.result.length / page_size );

if ( nth > 1 ) {
Expand Down Expand Up @@ -104,34 +102,21 @@ function hypermedia ( server, req, rep, headers ) {
} ).join( "&" );
}

array.each( rep.data.result, function ( i, idx ) {
array.each( rep.data.result, function ( i ) {
var uri;

if ( typeof i == "string" && REGEX_SCHEME.test( i ) ) {
rewrite = true;
uri = i.indexOf( "//" ) > -1 ? i : req.parsed.protocol + "//" + req.parsed.host + i;
uri = i.indexOf( "//" ) > -1 ? i : req.parsed.protocol + "//" + req.parsed.host + i;

if ( uri !== root ) {
rep.data.link.push( {uri: uri, rel: "item"} );
remove.push( idx );
}
}

if ( i instanceof Object ) {
parse( i, "item", req.parsed.pathname.replace( REGEX_TRAILING, "" ).replace( REGEX_LEADING, "" ).replace( REGEX_TRAILING_S, "" ) + "s" );
}
} );

// This is for collections of URIs
if ( remove.length > 0 ) {
array.each( remove.reverse(), function ( i ) {
array.remove( rep.data.result, i );
} );
}

if ( rewrite && rep.data.result.length === 0 ) {
rep.data.result = null;
}
}
else if ( rep.data.result instanceof Object ) {
parent = req.parsed.pathname.split( "/" ).filter( function( i ){ return i !== ""; } );
Expand Down
10 changes: 5 additions & 5 deletions test/tenso_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ describe("Permissions", function () {
.expectStatus( 200 )
.expectHeader( "allow", "GET, HEAD, OPTIONS" )
.expectValue( "data.link", [{uri:"http://localhost:" + port + "/items", rel:"item"}, {uri:"http://localhost:" + port + "/things", rel:"item"}] )
.expectValue( "data.result", null )
.expectValue( "data.result", ["/items", "/things"] )
.expectValue( "error", null )
.expectValue( "status", 200 )
.end(function(err) {
Expand Down Expand Up @@ -281,7 +281,7 @@ describe("Basic Auth", function () {
.get( "/" )
.expectStatus( 200 )
.expectValue( "data.link", [{uri:"http://localhost:" + port + "/items", rel:"item"}, {uri:"http://localhost:" + port + "/things", rel:"item"}] )
.expectValue( "data.result", null )
.expectValue( "data.result", ["/items", "/things"] )
.expectValue( "error", null )
.expectValue( "status", 200 )
.end( function ( err ) {
Expand Down Expand Up @@ -332,7 +332,7 @@ describe("OAuth2 Token Bearer", function () {
.get( "/" )
.expectStatus( 200 )
.expectValue( "data.link", [{uri:"http://localhost:" + port + "/items", rel:"item"}, {uri:"http://localhost:" + port + "/things", rel:"item"}] )
.expectValue( "data.result", null )
.expectValue( "data.result", ["/items", "/things"] )
.expectValue( "error", null )
.expectValue( "status", 200 )
.end( function ( err ) {
Expand Down Expand Up @@ -474,7 +474,7 @@ describe("Rate Limiting", function () {
.expectHeader( "x-ratelimit-limit", "2" )
.expectHeader( "x-ratelimit-remaining", "1" )
.expectValue( "data.link", [{uri:"http://localhost:" + port + "/items", rel:"item"}, {uri:"http://localhost:" + port + "/things", rel:"item"}] )
.expectValue( "data.result", null )
.expectValue( "data.result", ["/items", "/things"] )
.expectValue( "error", null )
.expectValue( "status", 200 )
.end( function ( err ) {
Expand All @@ -492,7 +492,7 @@ describe("Rate Limiting", function () {
.expectHeader( "x-ratelimit-limit", "2" )
.expectHeader( "x-ratelimit-remaining", "0" )
.expectValue( "data.link", [{uri:"http://localhost:" + port + "/items", rel:"item"}, {uri:"http://localhost:" + port + "/things", rel:"item"}] )
.expectValue( "data.result", null )
.expectValue( "data.result", ["/items", "/things"] )
.expectValue( "error", null )
.expectValue( "status", 200 )
.end( function ( err ) {
Expand Down

0 comments on commit 0182190

Please sign in to comment.