From 76c94a2cd6de141af60f02cfde632a19f45a1b5e Mon Sep 17 00:00:00 2001 From: emill Date: Fri, 25 May 2007 16:25:10 +0000 Subject: [PATCH] I think JSON is all set. git-svn-id: https://svn.thoughtbot.com/jester/trunk@81 d7758119-aa2c-0410-afcd-b700fbd0d0b3 --- jester.js | 35 ++++++++++++++++++++--------------- test/jester_test.html | 31 +++++++++++++++++++------------ 2 files changed, 39 insertions(+), 27 deletions(-) diff --git a/jester.js b/jester.js index 5c0dfb4..39cbf6f 100644 --- a/jester.js +++ b/jester.js @@ -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); } } @@ -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") @@ -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)) @@ -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)); } @@ -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 = {} @@ -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) { diff --git a/test/jester_test.html b/test/jester_test.html index e7d5e8f..aabf0a0 100644 --- a/test/jester_test.html +++ b/test/jester_test.html @@ -17,10 +17,10 @@ default_prefix = function() {return "http://" + window.location.hostname + (window.location.port ? ":" + window.location.port : "");} ericXML = '2007-04-08T15:12:06-04:001erictrue' - 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' } @@ -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; } } @@ -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() { @@ -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() {