I'm trying to clean up something for the hell of it, and looking for some better ways of going about it. The idea is that instead of using regular expressions in my rules for parsing a string, I'd like to use something closer to the routes syntax "something/:searchitem/somethingelse", then given a string like "/something/FOUNDIT/somethingelse" you'd get the result "FOUNDIT".
Here's the example I'm refactoring: Given an input string, say "http://claimid.com/myusername". I want to be able to run this string against a number of possible matches and then return the "myusername" one for the one that matches.
The data to run it against might look like this:
PROVIDERS = [
"http://openid.aol.com/:username",
"http://:username.myopenid.com",
"http://claimid.com/:username",
"http://:username.livejournal.com"]
something_here("http://claimid.com/myusername") # => "myusername"
Any good way of matching up a string like http://claimid.com/myusername
to this list and making sense of the results? Or any techniques to make something like this easier? I was looking through the rails routing code as it does something like this, but that's not the easiest code to follow.
Right now I'm just doing this with regular expressions, but it seems like the above method would be MUCH easier to read
PROVIDERS = [
/http:\/\/openid.aol.com\/(\w+)/,
/http:\/\/(\w+).myopenid.com/,
/http:\/\/(\w+).livejournal.com/,
/http:\/\/flickr.com\/photos\/(\w+)/,
/http:\/\/technorati.com\/people\/technorati\/(\w+)/,
/http:\/\/(\w+).wordpress.com/,
/http:\/\/(\w+).blogspot.com/,
/http:\/\/(\w+).pip.verisignlabs.com/,
/http:\/\/(\w+).myvidoop.com/,
/http:\/\/(\w+).pip.verisignlabs.com/,
/http:\/\/claimid.com\/(\w+)/]
url = "http://claimid.com/myusername"
username = PROVIDERS.collect { |provider|
url[provider, 1]
}.compact.first