0

Possible Duplicate:
Path parsing in rails

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!

Community
  • 1
  • 1
  • 1
    Can you give a better explanation of how you would like this interface to translate to your log? What do you want to be able to type, what do you want it to then do? You might also be interested to see how Sinatra converts routes to regex + names for capture groups http://github.com/JoshCheek/JoshsRubyKickstart/blob/master/cheatsheets/sinatra.rb#L27 I Assume Rails' routes are very similar. – Joshua Cheek Jul 04 '10 at 11:18
  • Joshua, Sinatra seems to be doing the same thing that Rails does. –  Jul 07 '10 at 03:16

1 Answers1

2

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
  • 20,200
  • 2
  • 32
  • 28