Skip to content

Commit

Permalink
new /data/allposts and /weather examples of creating and consuming js…
Browse files Browse the repository at this point in the history
…on data
  • Loading branch information
johnschimmel committed Mar 20, 2012
1 parent 70888c3 commit ff25abe
Show file tree
Hide file tree
Showing 4 changed files with 141 additions and 5 deletions.
3 changes: 0 additions & 3 deletions models.js
Expand Up @@ -21,9 +21,6 @@ module.exports.configureSchema = function(Schema, mongoose) {
}
});




// add schemas to Mongoose
mongoose.model('BlogPost', BlogPost);
mongoose.model('Comment', Comments);
Expand Down
3 changes: 2 additions & 1 deletion package.json
Expand Up @@ -4,6 +4,7 @@
"dependencies": {
"express": "2.5.6",
"ejs" : "latest",
"mongoose" : "2.5.6"
"mongoose" : "2.5.6",
"request" : "2.9.153"
}
}
49 changes: 49 additions & 0 deletions views/weather_from_yahoo.html
@@ -0,0 +1,49 @@
<div class="row">

<!-- LEFT COLUMN -->
<div class="span6">
<h1><%= weather.location.city %>
<img src="<%= weather.condition.image %>" alt="weather condition graphic : <%= weather.condition.text %>">
</h1>

<hr>

<ul>
<li>Temperature:
<%= weather.condition.text %>
<%= weather.condition.temperature %> <%= weather.units.temperature %></li>
<li>Wind speed: <%= weather.wind.speed %><%= weather.units.speed %> <%= weather.wind.direction %></li>
<li>Visibility: <%= weather.atmosphere.visibility %><%= weather.units.distance %>
</ul>

<hr>

<ul>
<li>Sunrise: <%= weather.astronomy.sunrise %></li>
<li>Sunset: <%= weather.astronomy.sunset %></li>
</ul>

<!-- location listing -->
<br><br>
<div class="well">
<h3>See weather in:</h3>
<% for(place in YAHOOLocations) { %>
<a href="/weather/<%= place %>"><%= place.toUpperCase() %></a> &nbsp;
<% } %>
</div>



</div>

<!-- RIGHT COLUMN -->
<div class="span6">
<h3>For example only, this is the JSON data from Yahoo</h3>
<pre><%= jsonFromYahoo %></pre>
<br>
requested url:<br>
<a href="<%= requestedURL %>" target="_new"><%= requestedURL %></a><br>

</div>
</div>

91 changes: 90 additions & 1 deletion web.js
Expand Up @@ -6,6 +6,8 @@ var app = express.createServer(express.logger());
var mongoose = require('mongoose'); // include Mongoose MongoDB library
var schema = mongoose.Schema;

var requestURL = require('request');

/************ DATABASE CONFIGURATION **********/
app.db = mongoose.connect(process.env.MONGOLAB_URI); //connect to the mongolabs database - local server uses .env file

Expand Down Expand Up @@ -193,7 +195,7 @@ app.get('/new-entry',function(request, response){
// receive a form submission
app.post('/new-entry', function(request, response){

console.log('Received new blog post submission')
console.log('Received new blog post submission');
console.log(request.body);

// Prepare the blog post entry form into a data object
Expand Down Expand Up @@ -332,6 +334,7 @@ app.post("/update", function(request, response){
// include data to update with 'updatedData'
// extra options - this time we only want a single doc to update
// after updating run the callback function - return err and numAffected

BlogPost.update( condition, updatedData, options, function(err, numAffected){

if (err) {
Expand All @@ -352,6 +355,92 @@ app.post("/update", function(request, response){
});


/*********** API & JSON EXAMPLES ************/

// return all blog entries in json format
app.get('/data/allposts', function(request, response){

// define the fields you want to include in your json data
includeFields = ['title','content','urlslug','date','comments','author.name']

// query for all blog
queryConditions = {}; //empty conditions - return everything
var query = BlogPost.find( queryConditions, includeFields);

query.sort('date',-1); //sort by most recent
query.exec(function (err, blogPosts) {

// render the card_form template with the data above
jsonData = {
'status' : 'OK',
'posts' : blogPosts
}

response.json(jsonData);
});
});

app.get('/weather', function(request, response){

// default /weather request - redirect to /weather/NYC
response.redirect("/weather/nyc");

});

app.get('/weather/:location', function(request, response){

// Yahoo Where On Earth ID ( WOEID )
// look up more locations here http://woeid.rosselliot.co.nz/lookup/shanghai
YAHOOLocations = {
'nyc' : 2459115,
'berlin' : 638242,
'shanghai' : 2151849
}

requestedLocation = request.params.location.toLowerCase();

if (requestedLocation in YAHOOLocations ) {
woeid = YAHOOLocations[requestedLocation];
} else {
woeid = YAHOOLocations['nyc'] // default to nyc
}

// build the request URL
yahooWeatherURL = "http://weather.yahooapis.com/forecastjson?w=" + woeid;

// make the request
requestURL(yahooWeatherURL, function(err, httpResponse, data) {

if (err || httpResponse.statusCode != 200) {
console.log(err);
response.send("Something went wrong");
}

if (httpResponse.statusCode == 200) {

//convert JSON string into JS Object
weatherData = JSON.parse(data);

console.log("-------- DATA RECEIVED -------");
console.log(data);
console.log("------------------------------");

templateData = {
jsonFromYahoo : data,
weather : weatherData,
requestedURL : yahooWeatherURL,
YAHOOLocations : YAHOOLocations
}

response.render('weather_from_yahoo.html', templateData);
}


})

})


// Make server turn on and listen at defined PORT (or port 3000 if is not defined)
var port = process.env.PORT || 3000;
app.listen(port, function() {
Expand Down

0 comments on commit ff25abe

Please sign in to comment.