Skip to content

Commit

Permalink
Forecast API calls and beach data
Browse files Browse the repository at this point in the history
Refactored getForecast function to rely on fibers rather than wrapAsync as the forecast.io api returns 3 values and I need the 3rd one (data)
Included a method to return specidic beach data
  • Loading branch information
timsvoice committed Apr 12, 2016
1 parent a457bd9 commit 1388fbb
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 17 deletions.
39 changes: 22 additions & 17 deletions imports/api/forecast/methods.js
Original file line number Diff line number Diff line change
@@ -1,36 +1,41 @@
import { Meteor } from 'meteor/meteor';
import { HTTP } from 'meteor/http';
import { ValidatedMethod } from 'meteor/mdg:validated-method';
import { _ } from 'underscore';
import Future from 'fibers/future';

import Forecast from 'forecast.io';

const options = { APIKey: Meteor.settings.FORECAST_KEY };
const forecast = new Forecast(options);
const future = new Future();

export const getForecast = new ValidatedMethod({
name: 'forecast.get',
validate: null,
run({ lat, lng }) {
let forecastCall = Meteor.wrapAsync(forecast.get, forecast);
let forecastOptions = { exclude:'minutely,hourly,flags,alerts' };
let forecastResult = forecastCall(lat, lng, forecastOptions).body;
return forecastResult;
run({ lat, lng }) {
const forecastOptions = { exclude: 'minutely,flags,alerts' };
forecast.get(lat, lng, forecastOptions, (err, res, data) => {
if (err) future.return(err);
future.return(data);
});
return future.wait();
},
});

export const getBeachForecast = new ValidatedMethod({
name: 'forecast.beach',
validate: null,
run({ lat, lng }) {
let forecastResult = getForecast.call({ lat, lng });
let beachForecast = {}

beachForecast.summary = forecastResult;
}
})
const data = getForecast.call({ lat, lng });
const currentData = data.currently;
const hourlyData = data.hourly;

// getBeachForecast.call({
// lat: 10.345,
// lng: -10.56
// })
const beachForecast = {
summary: hourlyData.summary,
current: currentData.summary,
temperature: currentData.apparentTemperature,
cloudCover: currentData.cloudCover,
precipProbability: currentData.precipProbability,
};
return beachForecast;
},
});
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
"test": "meteor test --driver-package=practicalmeteor:mocha --port 8100 --settings imports/startup/server/development/settings.json"
},
"dependencies": {
"fibers": "^1.0.10",
"forecast.io": "0.0.9",
"foundation-sites": "^6.2.1",
"meteor-node-stubs": "^0.2.3",
Expand Down

0 comments on commit 1388fbb

Please sign in to comment.