Skip to content

Instantly share code, notes, and snippets.

@gpincheiraa
Last active August 22, 2016 12:34
Show Gist options
  • Save gpincheiraa/48fecdcc56daed3d7476808d45ba5415 to your computer and use it in GitHub Desktop.
Save gpincheiraa/48fecdcc56daed3d7476808d45ba5415 to your computer and use it in GitHub Desktop.
Loopback Es5
module.exports = function(Sbif) {
var request = require('request-promise'),
token = process.env.SBIF_TOKEN_URL,
api_url = process.env.SBIF_API_URL;
// UTM ---------------------------------------------
Sbif.utm = function(params, cb) {
makeRequest('utm',{}).then(function(res) {
cb(null, JSON.parse(res).UTMs[0]);
});
};
Sbif.remoteMethod('utm',{
accepts: {arg: 'params', type: 'string'},
returns: {arg: 'utm', type: 'string'},
http: {path: '/utm', verb: 'get' }
});
// IPC ---------------------------------------------
Sbif.ipc = function(params, cb) {
makeRequest('ipc', {}).then(function(res) {
cb(null, JSON.parse(res).IPCs[0]);
});
};
Sbif.remoteMethod('ipc', {
accepts: {arg: 'params', type: 'string'},
returns: {arg: 'ipc', type: 'string'},
http: {path: '/ipc'}
});
// EURO --------------------------------------------
Sbif.euro = function(params, cb) {
makeRequest('euro', {}).then(function(res) {
cb(null, JSON.parse(res).Euros[0]);
});
};
Sbif.remoteMethod('euro', {
accepts: {arg: 'params', type: 'string'},
returns: {arg: 'euro', type: 'string'},
http: {path: '/euro'}
});
// Dolár -------------------------------------------
Sbif.dolar = function(params, cb) {
makeRequest('dolar',{}).then(function(res) {
cb(null,JSON.parse(res).Dolares[0]);
});
};
Sbif.remoteMethod('dolar', {
accepts: {arg: 'params', type: 'string'},
returns: {arg: 'dolar', type: 'string'},
http: {path: '/dolar'}
});
// UF ----------------------------------------------
Sbif.uf = function(params, cb) {
makeRequest('uf',{}).then(function(res) {
cb(null, JSON.parse(res).UFs[0]);
});
};
Sbif.remoteMethod( 'uf',{
accepts: {arg: 'params', type: 'string'},
returns: {arg: 'uf', type: 'string'},
http: {path: '/uf', verb: 'get' }
});
// SBIF Token and URL
function makeRequest(indicador, parametros){
return request(buildUrl(indicador, parametros));
}
function buildUrl(indicador, parametros){
var url = api_url + indicador;
if(parametros.length > 0){
parametros.forEach((parametros) {
url += '/' + parametros;
});
}
return url + "?apikey=" + token + "&formato=json";
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment