Skip to content

Commit

Permalink
Minor improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
Igor Drobiazko committed Oct 10, 2014
1 parent 50317d4 commit 3534664
Show file tree
Hide file tree
Showing 2 changed files with 121 additions and 22 deletions.
42 changes: 25 additions & 17 deletions lib/httpComponent.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Copy link
@iznenad

iznenad Oct 21, 2014

Why set to null?

}

HttpComponent.prototype.success = function success(responseHandler) {
this.responseHandler = responseHandler;
HttpComponent.prototype.success = function success(handleResponse) {
this.handleResponse = handleResponse;

return this;
};
Expand All @@ -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.

Copy link
@iznenad

iznenad Oct 21, 2014

Why not binding the doRequest to this once in the constructor function, then simply executing this.boundDoRequest('get', requestOptions)?

};

HttpComponent.prototype.put = function get(requestOptions) {
doRequest.apply(this, ['put', requestOptions]);
};

HttpComponent.prototype.post = function get(requestOptions) {

This comment has been minimized.

Copy link
@iznenad

iznenad Oct 21, 2014

Three different get functions

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) {
Expand All @@ -74,4 +82,4 @@ HttpComponent.prototype.exec = function exec(method, requestOptions) {
function done() {
emitter.emit('end');
}
};
}
101 changes: 96 additions & 5 deletions spec/httpComponent.spec.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
describe('Http Component', function () {

var Q = require('q');
var nock = require('nock');
var HttpComponent = require('../lib/httpComponent.js').HttpComponent;
var messages = require('../lib/messages.js');

it('should work', function () {
it('GET', function () {

nock('http://foobarbazbarney.com')
.get('/api')
Expand All @@ -22,16 +21,108 @@ describe('Http Component', function () {
json: true
};

function handleResponse(response) {
return Q(messages.newMessageWithBody(response.body));
function handleResponse(response, body) {
return messages.newMessageWithBody(body);
}

var component = new HttpComponent(emitter)
.success(handleResponse);

runAndExpect(
function () {
component.exec('get', options);
component.get(options);
},
function () {
return emitter.emit.callCount === 2;
},
function () {
var emitCalls = emitter.emit.calls;

var emitDataArgs = emitCalls[0].args;

expect(emitDataArgs[0]).toEqual('data');
expect(emitDataArgs[1].body).toEqual({
foo: 'bar',
baz: 'barney'
});

expect(emitCalls[1].args).toEqual(['end']);
});
});

it('POST', function () {

nock('http://foobarbazbarney.com')
.post('/api')
.reply(200, JSON.stringify({
foo: 'bar',
baz: 'barney'
}));


var emitter = jasmine.createSpyObj('emitter', ['emit']);

var options = {
url: 'http://foobarbazbarney.com/api',
json: true
};

function handleResponse(response, body) {
return messages.newMessageWithBody(body);
}

var component = new HttpComponent(emitter)
.success(handleResponse);

runAndExpect(
function () {
component.post(options);
},
function () {
return emitter.emit.callCount === 2;
},
function () {
var emitCalls = emitter.emit.calls;

var emitDataArgs = emitCalls[0].args;

expect(emitDataArgs[0]).toEqual('data');
expect(emitDataArgs[1].body).toEqual({
foo: 'bar',
baz: 'barney'
});

expect(emitCalls[1].args).toEqual(['end']);
});
});

it('PUT', function () {

nock('http://foobarbazbarney.com')
.put('/api')
.reply(200, JSON.stringify({
foo: 'bar',
baz: 'barney'
}));


var emitter = jasmine.createSpyObj('emitter', ['emit']);

var options = {
url: 'http://foobarbazbarney.com/api',
json: true
};

function handleResponse(response, body) {
return messages.newMessageWithBody(body);
}

var component = new HttpComponent(emitter)
.success(handleResponse);

runAndExpect(
function () {
component.put(options);
},
function () {
return emitter.emit.callCount === 2;
Expand Down

0 comments on commit 3534664

Please sign in to comment.