-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
re-factor lib/update.js to use http_request helper
- Loading branch information
Showing
3 changed files
with
10 additions
and
42 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters