URL generation

26 views
Skip to first unread message

Gabriel Boyer

unread,
Jun 29, 2009, 12:16:51 PM6/29/09
to sinatrarb
Hello,

Since I've started working with Sinatra, I've wondered: Sinatra
obviously handles URL routing nicely, but how do people generate URLs?
Do you roll your own setup for each app? Does your app have a simple
enough URI space that string interpolation everywhere is fine? Or is
there a Sinatra feature I've overlooked that makes this a non-issue?
I'd love to hear about how people have approached this.

After noticing a recent changeset that allowed for the use of URI
Templates from the Addressable for routing, I wrote a little module
for an app I'm working on that I can mix into my Sinatra::Application
subclasses to get unified routing and URI generation based on URI
Templates. I've pasted it at http://gist.github.com/137356.

I did one slightly dirty thing to get the usage I wanted, by
overriding the private compile method of Sinatra::Base. If anyone
could suggest a better way to go about this, I'd appreciate it.

Cheers,
Gabriel

Bob Aman

unread,
Jun 29, 2009, 2:34:20 PM6/29/09
to sina...@googlegroups.com
Gabe,

Interesting take on that. Glad I'm not the only one using that commit. :-)

To be honest I hadn't really considered reusing the templates like
that. Any time I've needed to generate an href, I've either ended up
creating a new template instance or just used string concatenation,
depending on complexity and/or the need for escaping any variable bits
in the URI.

TBH, I'm torn between the feeling that your implementation is too
heavyweight and admitting that my way of doing things involves
needless duplication.

Bob Aman

Gabriel Boyer

unread,
Jun 29, 2009, 3:17:25 PM6/29/09
to sinatrarb
I get what you mean about it being a little heavyweight. I guess I
decided that removing the duplication was more important.

Originally, I just needed a URI generation helper method, because I
wanted to mount multiple Sinatra apps at different paths in one
Rack::Builder, and I didn't want the app to have to care about the
path at which it was mounted. So, I needed a centralized way to
generate URIs, so I could modify the paths of the generated URIs using
request.script_name (which is where Rack::URLMap stores the part of
the URI path that it has matched) if necessary. The rest of the design
came from that, and wanting to keep duplication to a minimum.

Thanks for the feedback, and thanks for Addressable, too. I'd wanted a
Ruby URI Template implementation that did both expansion and matching
for a while, and managed to put off building it myself for long enough
that you did it for me. :-)

Gabriel Boyer

Alex Chaffee

unread,
Jul 19, 2009, 1:20:25 PM7/19/09
to sina...@googlegroups.com, Gabriel Boyer
Looks very nice, though like Bob, I think I won't use it :-)  Regular string concatenation is working fine for me too.

My only comment is terminological (and pedantic). It's not a URI! What you're dealing with are paths, or maybe routes. While you can *use* a path as shorthand for a URI (aka a "URI reference"), not all URIs are paths, and a URI can comprise several pieces not accounted for (like protocols and hostnames and query strings). Although "informal usage" (in the words of Wikipedia) "does not often maintain the distinction between a URI and a URI reference," it's clearer if you call it what it is.


 - Don Quixote

Guido De Rosa

unread,
Jul 19, 2009, 4:19:25 PM7/19/09
to sinatrarb
I would take a look at Sinatra-REST:

http://github.com/blindgaenger/sinatra-rest/

G.
Reply all
Reply to author
Forward
0 new messages