Permalink
Browse files

first steps towards new api

  • Loading branch information...
1 parent dd92783 commit 7444bbca0f78b48d47c8107fb325142e3f336869 @kevinohara80 committed May 4, 2012
Showing with 155 additions and 5 deletions.
  1. +2 −1 .gitignore
  2. +25 −4 index.js
  3. +41 −0 lib/record.js
  4. +87 −0 test/record.js
View
@@ -12,4 +12,5 @@ logs
results
node_modules
-npm-debug.log
+npm-debug.log
+examples/json
View
@@ -3,6 +3,7 @@
var request = require('request');
var qs = require('querystring');
var url = require('url');
+var Record = require('./lib/record');
// constants
@@ -223,7 +224,7 @@ Connection.prototype.getDescribe = 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);
}
if(typeof data.fieldValues !== 'object') {
@@ -232,8 +233,8 @@ Connection.prototype.insert = function(data, oauth, callback) {
if(!oauth || !oauth.instance_url || !oauth.access_token) {
return callback(new Error('Invalid oauth object argument'), null);
}
- var uri = oauth.instance_url + '/services/data/' + this.apiVersion + '/sobjects/' + data.type;
- var opts = { uri: uri, method: 'POST', body: JSON.stringify(data.fieldValues) }
+ var uri = oauth.instance_url + '/services/data/' + this.apiVersion + '/sobjects/' + data.attributes.type;
+ var opts = { uri: uri, method: 'POST', body: JSON.stringify(data.getFieldValues()) }
apiRequest(opts, oauth, callback);
}
@@ -325,7 +326,18 @@ Connection.prototype.query = function(query, oauth, callback) {
}
var uri = oauth.instance_url + '/services/data/' + this.apiVersion + '/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) {
@@ -422,4 +434,13 @@ module.exports.createConnection = function(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';
View
@@ -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;
View
@@ -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.