Skip to content
This repository has been archived by the owner on May 22, 2018. It is now read-only.

Commit

Permalink
I think JSON is all set.
Browse files Browse the repository at this point in the history
git-svn-id: https://svn.thoughtbot.com/jester/trunk@81 d7758119-aa2c-0410-afcd-b700fbd0d0b3
  • Loading branch information
emill committed May 25, 2007
1 parent 08514a5 commit 76c94a2
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 27 deletions.
35 changes: 20 additions & 15 deletions jester.js
Expand Up @@ -63,7 +63,7 @@ Base.requestAndParse = function(format, callback, url, options, user_callback) {
parse_and_callback = null;
if (format.toLowerCase() == "json") {
parse_and_callback = function(transport) {
eval("var attributes = " + transport.responseText);
eval("var attributes = " + transport.responseText); // hashes need this kind of eval
return callback(attributes);
}
}
Expand Down Expand Up @@ -107,8 +107,8 @@ Object.extend(Base.prototype, {
valid : function() {return ! this.errors.any();},

find : function(id, params, callback) {
findAllWork = function(raw) {
var collection = this._loadCollection(raw);
findAllWork = function(doc) {
var collection = this._loadCollection(doc);

// This is better than requiring the controller to support a "limit" parameter
if (id == "first")
Expand All @@ -117,8 +117,8 @@ Object.extend(Base.prototype, {
return collection;
}.bind(this);

findOneWork = function(raw) {
var base = this._loadSingle(raw);
findOneWork = function(doc) {
var base = this._loadSingle(doc);

// even if the ID didn't come back, we obviously knew the ID to search with, so set it
if (!base._properties.include("id")) base._setAttribute("id", parseInt(id))
Expand Down Expand Up @@ -305,29 +305,29 @@ Object.extend(Base.prototype, {
Internal methods.
*/

_loadSingle : function(raw) {
_loadSingle : function(doc) {
var attributes;
if (this._language == "json")
attributes = this._attributesFromJSON(raw);
attributes = this._attributesFromJSON(doc);
else
attributes = this._attributesFromTree(raw[this._singular]);
attributes = this._attributesFromTree(doc[this._singular]);

return this.build(attributes);
},

_loadCollection : function(raw) {
_loadCollection : function(doc) {
var collection;
if (this._language == "json") {
collection = raw.map(function(item) {
collection = doc.map(function(item) {
return this.build(this._attributesFromJSON(item));
}.bind(this));
}
else {
// if only one result, wrap it in an array
if (!Base.elementHasMany(raw[this._plural]))
raw[this._plural][this._singular] = [raw[this._plural][this._singular]];
if (!Base.elementHasMany(doc[this._plural]))
doc[this._plural][this._singular] = [doc[this._plural][this._singular]];

collection = raw[this._plural][this._singular].map(function(elem) {
collection = doc[this._plural][this._singular].map(function(elem) {
return this.build(this._attributesFromTree(elem));
}.bind(this));
}
Expand All @@ -337,7 +337,7 @@ Object.extend(Base.prototype, {
// Converts a JSON hash returns from ActiveRecord::Base#to_json into a hash of attribute values
// Does not handle associations, as AR's #to_json doesn't either
// Also, JSON doesn't include room to store types, so little auto-transforming is done here (just on 'id')
_attributesFromJSON : function(json) {
_attributesFromJSON : function(json) {
if (!(json && json.attributes)) return false;

var attributes = {}
Expand Down Expand Up @@ -443,7 +443,12 @@ Object.extend(Base.prototype, {

// Pulls errors from JSON
_errorsFromJSON : function(json) {
json = eval(json);
try {
json = eval(json); // okay for arrays
} catch(e) {
return false;
}

if (!(json && json.constructor == Array && json[0] && json[0].constructor == Array)) return false;

return json.map(function(pair) {
Expand Down
31 changes: 19 additions & 12 deletions test/jester_test.html
Expand Up @@ -17,10 +17,10 @@
default_prefix = function() {return "http://" + window.location.hostname + (window.location.port ? ":" + window.location.port : "");}

ericXML = '<?xml version="1.0" encoding="UTF-8"?><user><created-at type="datetime">2007-04-08T15:12:06-04:00</created-at><id type="integer">1</id><first-name>eric</first-name><admin type="boolean">true</admin></user>'
eval('ericJSON = {attributes: {id: "1", first_name: "eric", admin: "1", created_at: "2007-04-08 15:12:06"}}')
eval('johnJSON = {attributes: {id: "2", first_name: "john", admin: "0", created_at: "2007-04-09 15:12:06"}}')
eval('postJSON = {attributes: {id: "1", title: "the title", body: "the body"}}')
eval('usersJSON = [ericJSON, johnJSON]')
ericJSON = '{attributes: {id: "1", first_name: "eric", admin: "1", created_at: "2007-04-08 15:12:06"}}'
johnJSON = '{attributes: {id: "2", first_name: "john", admin: "0", created_at: "2007-04-09 15:12:06"}}'
postJSON = '{attributes: {id: "1", title: "the title", body: "the body"}}'
usersJSON = "[" + ericJSON + "," + johnJSON + "]"

postDetails1 = { 'id' : { '@type' : 'integer', '#text' : '1' }, 'title' : 'the title', 'body' : 'the body' }
postDetails2 = { 'id' : { '@type' : 'integer', '#text' : '2' }, 'title' : 'another title', 'body' : 'another body' }
Expand Down Expand Up @@ -114,14 +114,17 @@

// used to make sure callbacks get called
changed = false;
change = function() {changed = true;}

Base.requestAndParse = function (format, callback, url, options, user_callback) {
call = function(doc) {change(); return callback(doc);}
changed = true;
var result = Internet[url];

if (format == "json")
eval('result = ' + result);
if (callback)
return call(Internet[url]);
else
return Internet[url];
result = callback(result);

return result;
}
}

Expand Down Expand Up @@ -488,10 +491,16 @@
********************************************/

function testAttributesFromJSON() {
var attributes = User._attributesFromJSON(ericJSON);
eval('var json = ' + ericJSON);
var attributes = User._attributesFromJSON(json);
assertEquals("The ID should be auto-transformed from JSON.", 1, attributes.id);
assertEquals("1", attributes.admin.toString()); // toString() since "1" == true
assertEquals("eric", attributes.first_name);

assertEquals(false, User._attributesFromJSON({}));
assertEquals(false, User._attributesFromJSON({email: "emill@thoughtbot.com", id: "1"}));
assertEquals(false, User._attributesFromJSON([]));
assertEquals(false, User._attributesFromJSON(null));
}

function testAttributesFromTree() {
Expand All @@ -512,8 +521,6 @@
assertEquals(false, User._errorsFromJSON(' '));
assertEquals(false, User._errorsFromJSON(''));
assertEquals(false, User._errorsFromJSON(null));
assertEquals(false, User._errorsFromJSON([]));
assertEquals(false, User._errorsFromJSON({}));
}

function testErrorsFromXML() {
Expand Down

0 comments on commit 76c94a2

Please sign in to comment.