Skip to content

Commit

Permalink
first steps towards new api
Browse files Browse the repository at this point in the history
  • Loading branch information
kevinohara80 committed May 4, 2012
1 parent dd92783 commit 7444bbc
Show file tree
Hide file tree
Showing 4 changed files with 155 additions and 5 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Expand Up @@ -12,4 +12,5 @@ logs
results results


node_modules node_modules
npm-debug.log npm-debug.log
examples/json
29 changes: 25 additions & 4 deletions index.js
Expand Up @@ -3,6 +3,7 @@
var request = require('request'); var request = require('request');
var qs = require('querystring'); var qs = require('querystring');
var url = require('url'); var url = require('url');
var Record = require('./lib/record');


// constants // constants


Expand Down Expand Up @@ -223,7 +224,7 @@ Connection.prototype.getDescribe = function(data, oauth, callback) {
} }


Connection.prototype.insert = function(data, oauth, callback) { Connection.prototype.insert = function(data, oauth, callback) {
if(typeof data.type !== 'string') { if(typeof data.attributes.type !== 'string') {
return callback(new Error('Type must be in the form of a string'), null); return callback(new Error('Type must be in the form of a string'), null);
} }
if(typeof data.fieldValues !== 'object') { if(typeof data.fieldValues !== 'object') {
Expand All @@ -232,8 +233,8 @@ Connection.prototype.insert = function(data, oauth, callback) {
if(!oauth || !oauth.instance_url || !oauth.access_token) { if(!oauth || !oauth.instance_url || !oauth.access_token) {
return callback(new Error('Invalid oauth object argument'), null); return callback(new Error('Invalid oauth object argument'), null);
} }
var uri = oauth.instance_url + '/services/data/' + this.apiVersion + '/sobjects/' + data.type; var uri = oauth.instance_url + '/services/data/' + this.apiVersion + '/sobjects/' + data.attributes.type;
var opts = { uri: uri, method: 'POST', body: JSON.stringify(data.fieldValues) } var opts = { uri: uri, method: 'POST', body: JSON.stringify(data.getFieldValues()) }
apiRequest(opts, oauth, callback); apiRequest(opts, oauth, callback);
} }


Expand Down Expand Up @@ -325,7 +326,18 @@ Connection.prototype.query = function(query, oauth, callback) {
} }
var uri = oauth.instance_url + '/services/data/' + this.apiVersion + '/query'; var uri = oauth.instance_url + '/services/data/' + this.apiVersion + '/query';
var opts = { uri: uri, method: 'GET', qs: { q: query } } var opts = { uri: uri, method: 'GET', qs: { q: query } }
apiRequest(opts, oauth, callback); apiRequest(opts, oauth, function(err, resp){
if(!err) {
if(resp.records && resp.records.length > 0) {
var recs = [];
for(var i=0; i<resp.records.length; i++) {
recs.push(new Record(resp.records[i]));
}
resp.records = recs;
}
}
callback(err, resp);
});
} }


Connection.prototype.search = function(search, oauth, callback) { Connection.prototype.search = function(search, oauth, callback) {
Expand Down Expand Up @@ -422,4 +434,13 @@ module.exports.createConnection = function(opts) {
return new Connection(opts); return new Connection(opts);
} }


module.exports.createSObject = function(type, id) {
var data = {
attributes: {}
}
data.attributes.type = type;
if(id) data.Id = id;
return new Record(data);
}

module.exports.version = '0.0.2'; module.exports.version = '0.0.2';
41 changes: 41 additions & 0 deletions lib/record.js
@@ -0,0 +1,41 @@
var Record = function(data) {

if(data.attributes) {
this.attributes = data.attributes;
delete data.attributes;
}

this.fieldValues = {}
this._data = data;

var key;

for(key in data) {
(function (record, key) {
record.__defineGetter__(key, function() {
return record._data[key];
});
record.__defineSetter__(key, function(val) {
record._data[key] = val;
record.fieldValues[key] = val;
});
}(this, key));
}
}

Record.prototype.getFieldValues = function() {
var fvs = this.fieldValues;
for (var fieldKey in this) {
if(fieldKey !== 'url' && fieldKey !== 'type' && fieldKey !== 'id'
&& fieldKey.substring(0,1) !== '_' && fieldKey !== 'fieldValues'
&& !this.__lookupGetter__(fieldKey) && typeof this[fieldKey] !== 'function') {
if(!fvs[fieldKey]) {
fvs[fieldKey] = this[fieldKey];
}
}
}
return fvs;
}


module.exports = Record;
87 changes: 87 additions & 0 deletions test/record.js
@@ -0,0 +1,87 @@
var Record = require('../lib/record');

var accountRec = {
"attributes": {
"type": "Account",
"url": "/services/data/v24.0/sobjects/Account/001Q000000RpQagIAF"
},
"Id": "001Q000000RpQagIAF",
"IsDeleted": false,
"MasterRecordId": null,
"Name": "Portal House Account",
"Type": null,
"ParentId": null,
"BillingStreet": null,
"BillingCity": null,
"BillingState": null,
"BillingPostalCode": null,
"BillingCountry": null,
"ShippingStreet": null,
"ShippingCity": null,
"ShippingState": null,
"ShippingPostalCode": null,
"ShippingCountry": null,
"Phone": null,
"Fax": null,
"Website": null,
"Industry": null,
"AnnualRevenue": null,
"NumberOfEmployees": null,
"Description": null,
"OwnerId": "00530000004sFb6AAE",
"CreatedDate": "2012-04-02T23:06:57.000+0000",
"CreatedById": "00530000004sFb6AAE",
"LastModifiedDate": "2012-04-02T23:06:57.000+0000",
"LastModifiedById": "00530000004sFb6AAE",
"SystemModstamp": "2012-05-03T19:24:28.000+0000",
"LastActivityDate": "2012-05-03",
"IsCustomerPortal": true,
"Jigsaw": null,
"JigsawCompanyId": null,
"AccountSource": null,
"SicDesc": null
}

describe('#constructor', function() {

it('should allow me to set properties', function() {

var myRecord = new Record(accountRec);

myRecord.Fax = '248-443-3456';

myRecord.Fax.should.equal('248-443-3456');
myRecord.fieldValues.Fax.should.equal('248-443-3456');

});

it('should allow me to set properties multiple times', function() {

var myRecord = new Record(accountRec);

myRecord.Fax = '248-443-3456';
myRecord.Fax = '248-443-3457';
myRecord.Fax = '248-443-3458';

myRecord.Fax.should.equal('248-443-3458');
myRecord.fieldValues.Fax.should.equal('248-443-3458');

});

});

describe('#getFieldValues', function() {

it('should return existing and custom set properties', function() {

var myRecord = new Record(accountRec);

// set a new Fax
myRecord.Fax = '248-443-3456';
myRecord.Custom_Field__c = 'This is something';

myRecord.getFieldValues().should.have.keys('Fax', 'Custom_Field__c');

});

});

0 comments on commit 7444bbc

Please sign in to comment.