Ruby String parsing like how rails parses routes (with :symbols) [duplicate]
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 lo开发者_运维问答g 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!
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.
精彩评论