Skip to content

Instantly share code, notes, and snippets.

@robb1e
Created November 13, 2011 22:01
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save robb1e/1362799 to your computer and use it in GitHub Desktop.
Save robb1e/1362799 to your computer and use it in GitHub Desktop.
Creating API docs for scalatra
class HttpMethod
object HttpPostMethod extends HttpMethod { override def toString = "POST" }
object HttpGetMethod extends HttpMethod { override def toString = "GET" }
object HttpDeleteMethod extends HttpMethod { override def toString = "DELETE" }
object HttpPutMethod extends HttpMethod { override def toString = "PUT" }
abstract class Param(){
val name: String
val description: String
}
abstract class Endpoint {
val slug: String
val path: String
val stringFormat: String
lazy val requiredParams: List[Param] = List()
lazy val optionalParams: List[Param] = List()
val description: String
val method: HttpMethod
}
object ApiResource extends Endpoint {
lazy val slug = "/resource"
...
}
implicit def endpoint2RouteMatcher(endpoint: Endpoint): RouteMatcher = new SinatraRouteMatcher(endpoint.path, requestPath)
get(ApiAction) {
...
}
@rossabaker
Copy link

Cool. A few comments:

  • There's already org.scalatra.HttpMethod, unless you're trying to keep this independent of Scalatra.
  • Param and Endpoint look like good candidates for case classes.
  • If Endpoint already knows its method, it would be nice to not have to repeat it when adding it to Scalatra.

@robb1e
Copy link
Author

robb1e commented Nov 17, 2011

There's no reason why I'm not using org.scalatra.HttpMethod, we're only using this in one project for the moment, but if it proves useful I might pull it out into a common project. If I were to do that, can you point me to a good example of a library that produces HTML from an endpoint that I can copy the best practices from?

I did have a reason for not using case classes, but now I can't remember what that was. I could turn that into case classes quite easily.

I'm hoping to have it automatically register with scalatra based on the HttpMethod type, do you have a suggestion how that might work (based on your third point)?

If you or others think it might be useful, I'll happily pull this out into a common project if I can figure out some of the above.

Cheers

Robbie

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment