-
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
Oct 10, 2014
1 parent
50317d4
commit 3534664
Showing
2 changed files
with
121 additions
and
22 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 |
---|---|---|
|
@@ -8,11 +8,11 @@ exports.HttpComponent = HttpComponent; | |
function HttpComponent(component) { | ||
this.component = component; | ||
this.statusCodesToRebound = []; | ||
this.responseHandler = null; | ||
this.handleResponse = null; | ||
This comment has been minimized.
Sorry, something went wrong. |
||
} | ||
|
||
HttpComponent.prototype.success = function success(responseHandler) { | ||
this.responseHandler = responseHandler; | ||
HttpComponent.prototype.success = function success(handleResponse) { | ||
this.handleResponse = handleResponse; | ||
|
||
return this; | ||
}; | ||
|
@@ -32,30 +32,38 @@ HttpComponent.prototype.reboundOnStatusCode = function reboundOnStatusCode(statu | |
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"); | ||
HttpComponent.prototype.get = function get(requestOptions) { | ||
doRequest.apply(this, ['get', requestOptions]); | ||
This comment has been minimized.
Sorry, something went wrong.
iznenad
|
||
}; | ||
|
||
HttpComponent.prototype.put = function get(requestOptions) { | ||
doRequest.apply(this, ['put', requestOptions]); | ||
}; | ||
|
||
HttpComponent.prototype.post = function get(requestOptions) { | ||
This comment has been minimized.
Sorry, something went wrong. |
||
doRequest.apply(this, ['post', requestOptions]); | ||
}; | ||
|
||
function doRequest(method, requestOptions) { | ||
if (!this.handleResponse) { | ||
throw new Error("Response handler is required. Please set it through HttpComponent.success"); | ||
} | ||
|
||
var self = this; | ||
var emitter = this.component; | ||
|
||
Q.nfcall(request[method], requestOptions) | ||
.then(transformArrayToObject) | ||
.then(self.handleResponse) | ||
.spread(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); | ||
if (msg) { | ||
emitter.emit('data', msg); | ||
} else { | ||
console.log('Component produced no data'); | ||
} | ||
} | ||
|
||
function handleError(err) { | ||
|
@@ -74,4 +82,4 @@ HttpComponent.prototype.exec = function exec(method, requestOptions) { | |
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
Why set to null?