Skip to content

Commit

Permalink
Primer commit
Browse files Browse the repository at this point in the history
  • Loading branch information
UBEIMAR ALVAREZ HERNANDEZ authored and UBEIMAR ALVAREZ HERNANDEZ committed Jul 11, 2019
0 parents commit c72450f
Show file tree
Hide file tree
Showing 7 changed files with 503 additions and 0 deletions.
Binary file added .DS_Store
Binary file not shown.
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
## Aplicación del Clima - Curso Node


Recuerden ejecutar ```npm install``` para las librerías


### Ejemplo:
```
node app -d "San Jose Costa Rica"
```
41 changes: 41 additions & 0 deletions app.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@


const lugar = require('./lugar/lugar');
const clima = require('./clima/clima');


const argv = require('yargs').options({
direccion: {
alias: 'd',
desc: 'Dirección de la ciudad para obtener el clima',
demand: true
}
}).argv;


// lugar.getLugarLatLng( argv.direccion )
// .then( console.log );

// clima.getClima( 40.750000, -74.000000 )
// .then( console.log )
// .catch( console.log );


const getInfo = async( direccion ) => {


try {
const coords = await lugar.getLugarLatLng( direccion );
const temp = await clima.getClima( coords.lat, coords.lng );
return `El clima de ${ coords.direccion } es de ${ temp }.`;
} catch (e) {
return `No se pudo determinar el clima de ${ direccion }`;
}

}



getInfo( argv.direccion )
.then( console.log )
.catch( console.log );
16 changes: 16 additions & 0 deletions clima/clima.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
const axios = require('axios');



const getClima = async ( lat, lng ) => {

const resp = await axios.get(`https://api.openweathermap.org/data/2.5/weather?lat=${ lat }&lon=${ lng }&appid=32f843d833c38373032f825c4a92418a&units=metric`)

return resp.data.main.temp;

}


module.exports = {
getClima
}
36 changes: 36 additions & 0 deletions lugar/lugar.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
const axios = require('axios');


const getLugarLatLng = async( dir ) => {

const encodedUlr = encodeURI( dir );

const instance = axios.create({
baseURL: `https://devru-latitude-longitude-find-v1.p.rapidapi.com/latlon.php?location=${ encodedUlr }`,
headers: {'X-RapidAPI-Key': 'YrIv9XHJxmmshCBitpg1YTAnahQSp1KbdHhjsnSBU1hvMDMlzK'}
});

const resp = await instance.get();

if ( resp.data.Results.length === 0 ) {
throw new Error(`No hay resultados para ${ dir }`);
}

const data = resp.data.Results[0];
const direccion = data.name;
const lat = data.lat;
const lng = data.lon;


return {
direccion,
lat,
lng
}
}


module.exports = {
getLugarLatLng
}

Loading

0 comments on commit c72450f

Please sign in to comment.