Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion CHANGELOG
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
0.1.0 - 07.06.2018
- Start publishing fork from https://github.com/iugu/iugu-node/ as iugu-sdk

0.0.4 May 13, 2014
- Change on npm description

0.0.3 May 13, 2014
- Refactored to new Architecture

Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

## Instalação

`npm install iugu`
`npm install iugu-sdk`

## Exemplo de Uso
```js
Expand Down
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
0.0.5
0.1.0
92 changes: 35 additions & 57 deletions lib/IuguResource.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ var http = require('http');
var https = require('https');
var path = require('path');
var when = require('when');
var request = require('request');

var utils = require('./utils');
var Error = require('./Error');
Expand Down Expand Up @@ -49,7 +50,7 @@ IuguResource.prototype = {
this.basePath(urlData),
this.path(urlData),
typeof commandPath == 'function' ?
commandPath(urlData) : commandPath
commandPath(urlData) : commandPath
).replace(/\\/g, '/'); // ugly workaround for Windows
},

Expand All @@ -65,19 +66,23 @@ IuguResource.prototype = {
},

createDeferred: function(callback) {
var deferred = when.defer();

if (callback) {
// Callback, if provided, is a simply translated to Promise'esque:
// (Ensure callback is called outside of promise stack)
deferred.promise.then(function(res) {
setTimeout(function(){ callback(null, res) }, 0);
}, function(err) {
setTimeout(function(){ callback(err, null); }, 0);
});
}
var deferred = when.defer();

if (callback) {
// Callback, if provided, is a simply translated to Promise'esque:
// (Ensure callback is called outside of promise stack)
deferred.promise.then(function(res) {
setTimeout(function() {
callback(null, res)
}, 0);
}, function(err) {
setTimeout(function() {
callback(err, null);
}, 0);
});
}

return deferred;
return deferred;
},

_timeoutHandler: function(timeout, req, callback) {
Expand Down Expand Up @@ -153,61 +158,34 @@ IuguResource.prototype = {
},

_request: function(method, path, data, auth, callback) {

var requestData = utils.stringifyRequestData(data || {});
var requestData = data || {};
var self = this;
console.log(requestData);

var apiVersion = this._iugu.getApiField('version');
var headers = {
// Use specified auth token or use default from this stripe instance:
'Authorization': auth ?
'Basic ' + new Buffer(auth + ':').toString('base64') :
this._iugu.getApiField('auth'),
'Basic ' + new Buffer(auth + ':').toString('base64') : this._iugu.getApiField('auth'),
'Accept': 'application/json',
'Content-Type': 'application/x-www-form-urlencoded',
'Content-Length': requestData.length,
'User-Agent': 'Iugu/v1 NodeBindings/' + this._iugu.getConstant('PACKAGE_VERSION')
'Content-Type': 'application/json'
};

if (apiVersion) {
headers['Iugu-Version'] = apiVersion;
}
var options = {
method: method,
url: 'https://api.iugu.com/' + path,
body: requestData,
headers: headers,
json: true,
};

// Grab client-user-agent before making the request:
this._iugu.getClientUserAgent(function(cua) {
headers['X-Iugu-Client-User-Agent'] = cua;
makeRequest();
request(options, function(err, response, body) {
if (err) {
callback.call(self, err, null)
} else {
callback.call(self, null, body);
}
});

function makeRequest() {

var timeout = self._iugu.getApiField('timeout');

var req = (
self._iugu.getApiField('protocol') == 'http' ? http : https
).request({
host: self._iugu.getApiField('host'),
port: self._iugu.getApiField('port'),
path: path,
method: method,
headers: headers
});

req.setTimeout(timeout, self._timeoutHandler(timeout, req, callback));
req.on('response', self._responseHandler(req, callback));
req.on('error', self._errorHandler(req, callback));

req.on('socket', function(socket) {
socket.on('secureConnect', function() {
req.write(requestData);
req.end();
});
});

}

}

};

module.exports = IuguResource;
4 changes: 1 addition & 3 deletions lib/iugu.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ var resources = {

// The following rely on pre-filled customer IDs:
CustomerPaymentMethods: require('./resources/CustomerPaymentMethods'),

};

Iugu.IuguResource = require('./IuguResource');
Expand Down Expand Up @@ -131,9 +131,7 @@ Iugu.prototype = {
name[0].toLowerCase() + name.substring(1)
] = new resources[name](this);
}

}

};

module.exports = Iugu;
Loading