Skip to content

Commit

Permalink
re-factor lib/update.js to use http_request helper
Browse files Browse the repository at this point in the history
  • Loading branch information
nelsonic committed Dec 28, 2014
1 parent 77d54b3 commit 14dc0bc
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 42 deletions.
44 changes: 6 additions & 38 deletions lib/update.js
Original file line number Diff line number Diff line change
@@ -1,41 +1,9 @@
var http = require('http');
var config = require('../config');
var OPTIONS = require('./options');
var REQUEST = require('./http_request');

function update(record, callback) {

// Build the post string from an object
var json = JSON.stringify(record);

// An object of options to indicate where to post to
var post_options = {
host: config.host,
port: config.port,
path: '/' + record.index + '/' + record.type + '/' + record.id,
method: 'PUT',
headers: {
'Content-Type': 'application/json'
}
};

var resStr = '';
// Set up the request
var req = http.request(post_options, function(res) {
res.setEncoding('utf8');
var resStr = '';
res.on('data', function (chunk) {
resStr += chunk;
}).on('end', function () {
// console.log(JSON.parse(resStr));
callback(null, JSON.parse(resStr));
});
});

// post the data
req.write(json);
module.exports = function update(record, callback) {
var options = OPTIONS(record, 'PUT');
var req = REQUEST(options, callback);
req.write(JSON.stringify(record));
req.end();

}

module.exports = {
update : update
}
4 changes: 2 additions & 2 deletions test/update.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ var faker = require('faker');

var CREATE = require('../lib/create.js');
var READ = require('../lib/read.js');
var U = require('../lib/update.js');
var UPDATE = require('../lib/update.js');

test(chalk.cyan('UPDATE a record'), function (t) {
var record = {
Expand All @@ -26,7 +26,7 @@ test(chalk.cyan('UPDATE a record'), function (t) {
READ(rec, function (err2, res2) {
rec.message = "my new message"; // change message
// update record in ES
U.update(rec, function(err3, res3) {
UPDATE(rec, function(err3, res3) {
t.equal(res3._version, 2, chalk.green("✓ Record updated (version: "+res3._version +")"));
// read back the record to confirm it was updated:
READ(rec, function(err4, res4){
Expand Down
4 changes: 2 additions & 2 deletions test/upsert.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ var faker = require('faker');

var CREATE = require('../lib/create.js');
var READ = require('../lib/read.js');
var U = require('../lib/update.js');
var UPDATE = require('../lib/update.js');

test(chalk.cyan('UPDATE a record'), function (t) {
var record = {
Expand All @@ -26,7 +26,7 @@ test(chalk.cyan('UPDATE a record'), function (t) {
READ(rec, function (err2, res2) {
rec.message = "my new message"; // change message
// update record in ES
U.update(rec, function(err3, res3) {
UPDATE(rec, function(err3, res3) {
t.equal(res3._version, 2, chalk.green("✓ Record updated (version: "+res3._version +")"));
// read back the record to confirm it was updated:
READ(rec, function(err4, res4){
Expand Down

0 comments on commit 14dc0bc

Please sign in to comment.