Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

add RSS 2.0 feed

commit b111c01d5a3f739b3f6a0839ca5828d5bd00e8b4 1 parent 5bedafb
James Ward authored
59 app/controllers/RegionController.java
View
@@ -1,9 +1,9 @@
package controllers;
-import models.Region;
-import models.RegionSubscription;
-import models.Route;
-import models.User;
+import com.sun.syndication.feed.synd.*;
+import com.sun.syndication.io.FeedException;
+import com.sun.syndication.io.SyndFeedOutput;
+import models.*;
import play.data.Form;
import play.data.validation.ValidationError;
import play.mvc.Controller;
@@ -11,6 +11,8 @@
import play.mvc.Security;
import play.mvc.With;
+import java.io.IOException;
+import java.io.StringWriter;
import java.util.ArrayList;
import java.util.List;
@@ -18,9 +20,54 @@
public class RegionController extends Controller {
// todo
- public static Result getRegionFeed(String urlFriendlyRegionName) {
+ public static Result getRegionFeed(String urlFriendlyRegionName) throws FeedException, IOException {
Region region = Region.findByUrlFriendlyName(urlFriendlyRegionName);
- return ok(region.getName());
+
+ SyndFeed feed = new SyndFeedImpl();
+ feed.setFeedType("rss_2.0");
+
+ feed.setTitle("Uber Tracks - " + region.getName());
+ feed.setLink("http://hike.ubertracks.com");
+ feed.setDescription("Updates for Hike Uber Tracks - " + region.getName());
+
+ List entries = new ArrayList();
+
+ for (Route route : region.routes) {
+
+ SyndEntry entry = new SyndEntryImpl();
+ entry.setTitle("Route - " + route.getName());
+ entry.setLink("http://hike.ubertracks.com" + routes.RouteController.getRouteHtml(region.getUrlFriendlyName(), route.getUrlFriendlyName()).url());
+ entry.setPublishedDate(route.creationDate);
+
+ SyndContent description = new SyndContentImpl();
+ description.setType("text/plain");
+
+ // todo: templatize
+ String text = "Route - " + route.getName() + "\n\n" +
+ route.description + "\n\n" +
+ "Location: " + route.location + "\n\n" +
+ "Created: " + route.creationDate + "\n\n" +
+ "Distance (Miles): " + route.distanceInMiles + "\n\n" +
+ "Average Rating: " + route.getAverageRating() + "\n\n";
+
+ for (Direction direction : route.directions) {
+ text += direction.stepNumber + " - " + direction.instruction + "\n";
+ }
+
+ description.setValue(text);
+
+ entry.setDescription(description);
+
+ entries.add(entry);
+ }
+ feed.setEntries(entries);
+
+ StringWriter writer = new StringWriter();
+ SyndFeedOutput output = new SyndFeedOutput();
+ output.output(feed, writer);
+ writer.close();
+
+ return ok(writer.toString()).as("application/rss+xml");
}
@Security.Authenticated(Secured.class)
39 app/views/main.scala.html
View
@@ -1,23 +1,24 @@
-@(title: String)(content: Html)
+@(title: String, headContent: Html = Html(""))(bodyContent: Html)
<!DOCTYPE html>
<html>
- <head>
- <title>@title</title>
- <link rel="shortcut icon" type="image/png" href="@routes.Assets.at("images/favicon.png")">
- </head>
- <body>
-
- @if(CurrentUser.get() != null) {
- Hello, @CurrentUser.get().fullName
- } else {
- <form action="@routes.ApplicationController.login()" method="post">
- Email: <input name="emailAddress"/>
- Password: <input name="password" type="password"/>
- <input type="submit" value="Login"/>
- </form>
- }
-
- @content
- </body>
+<head>
+ <title>@title</title>
+ <link rel="shortcut icon" type="image/png" href="@routes.Assets.at("images/favicon.png")">
+ @headContent
+</head>
+<body>
+
+ @if(CurrentUser.get() != null) {
+ Hello, @CurrentUser.get().fullName
+ } else {
+ <form action="@routes.ApplicationController.login()" method="post">
+ Email: <input name="emailAddress"/>
+ Password: <input name="password" type="password"/>
+ <input type="submit" value="Login"/>
+ </form>
+ }
+
+ @bodyContent
+</body>
</html>
6 app/views/region.scala.html
View
@@ -1,6 +1,10 @@
@(region: Region)
-@main("Happy Trails - Regions - " + region.getName) {
+@headContent = {
+ <link rel="alternate" type="application/rss+xml" title="@region.getName RSS Feed" href="@routes.RegionController.getRegionFeed(region.getUrlFriendlyName)" />
+}
+
+@main("Happy Trails - Regions - " + region.getName, headContent) {
<ul>
@for(route <- region.routes) {
2  conf/routes
View
@@ -12,7 +12,7 @@ POST /login controllers.ApplicationController.login()
GET /logout controllers.ApplicationController.logout()
-GET /feed/:region controllers.RegionController.getRegionFeed(region)
+GET /:region/feed controllers.RegionController.getRegionFeed(region)
POST /:region/subscribe controllers.RegionController.subscribe(region)
3  project/Build.scala
View
@@ -8,7 +8,8 @@ object ApplicationBuild extends Build {
val appVersion = "1.0-SNAPSHOT"
val appDependencies = Seq(
- "com.github.twitter" % "bootstrap" % "2.0.3"
+ "com.github.twitter" % "bootstrap" % "2.0.3",
+ "rome" % "rome" % "1.0"
)
val main = PlayProject(appName, appVersion, appDependencies, mainLang = JAVA).settings(
Please sign in to comment.
Something went wrong with that request. Please try again.