-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Igor Drobiazko
committed
Sep 16, 2014
1 parent
b605041
commit 9ee057c
Showing
3 changed files
with
58 additions
and
39 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
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 |
---|---|---|
@@ -0,0 +1,52 @@ | ||
var request = require("request"); | ||
var Q = require("q"); | ||
var messages = require('./messages.js'); | ||
|
||
exports.HttpComponent = HttpComponent; | ||
|
||
function HttpComponent(component) { | ||
this.component = component; | ||
this.responseHandler = null; | ||
} | ||
|
||
HttpComponent.prototype.onResponse = function onResponse(responseHandler) { | ||
this.responseHandler = responseHandler; | ||
|
||
return this; | ||
}; | ||
|
||
HttpComponent.prototype.exec = function exec(method, requestOptions) { | ||
if (!this.responseHandler) { | ||
throw new Error("Response handler is required. Please set it through HttpComponent.onResponse"); | ||
} | ||
|
||
var self = this; | ||
var emitter = this.component; | ||
|
||
Q.nfcall(request[method], requestOptions) | ||
.then(transformArrayToObject) | ||
.then(self.handleResponse) | ||
.then(emitMessage) | ||
.fail(handleError) | ||
.done(done); | ||
|
||
function transformArrayToObject(output) { | ||
return Q({ | ||
response : output[0], | ||
body : output[1] | ||
}); | ||
} | ||
|
||
function emitMessage(msg) { | ||
emitter.emit('data', msg); | ||
} | ||
|
||
function handleError(err) { | ||
console.log(err); | ||
emitter.emit('error', err); | ||
} | ||
|
||
function done() { | ||
emitter.emit('end'); | ||
} | ||
}; |
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