views:

51

answers:

1

I'm trying to parse out some log files with ruby. I have plenty of experience with regexes but the other day I was using rails routes and thought that might be a really neat way to parse out these log files because it would be incredibly easy to understand the parser. Regex, on the other hand, takes several minutes for most people to digest into understandable logic.

The "black magic" of Rails seems to do things like this:

#some sort of route (the format string):
map.awesome_route 'awesome/:id/:slug.:format'
#
#presumably behind the scenes:
url = 'stackoverflow.com/awesome/3/all_my_questions.xml
hash = url.parse_by_route map.awesome_route, url

#now, again presumably:
#
#hash[:id] => '3'
#hash[:slug] => 'all_my_questions'
#hash[:format] => 'xml'

Maybe this isn't the way Rails actually does things. But it should. Is there a way to parse out my log file like that? It would really really really make my day.

Thanks!

A: 

maybe ActionController::Routing::Routes.recognize_path is what you're looking for?

ActionController::Routing::Routes.recognize_path '/users/6'
# => { :controller => 'users', :action => 'show', :id => 6 }

If you want to parse logfiles in a script, it shouldn't be a problem to parse your routes.rb before starting, so that the routes mapping will be set up.

flitzwald