I'm getting my latest tweets with HTTParty and Hashie like so.
tweet = Hashie::Mash.new HTTParty.get(http://twitter.com/statuses/user_timeline/ethnt.json).first
puts tweet.text
I want to be able to turn every link (http://*.*
) and usernames (@.
) into links. What would the regex for both of these be, and how would I implement it?
You can try this:
The regex for the url is not perfect, but good enough for the common ones.
This works, so long as URLs are padded by spaces or are at the beginning and/or end of the tweet.
This project has a method for it: https://github.com/mzsanford/twitter-text-rb
From their docs:
Expanding on the Tin Man's answer, there's a simple one liner for making URLs clickable.
You'll then need to use
body.html_safe
if in Rails. For the Twitter users, you should really be relying on the Twitter API to tell you what is and isn't a valid username, because they can correctly filter out "@looksvalid" when there is no user by that name.For finding URLs in text, why not reuse an existing wheel instead of invent a new one?
Add that to the answer given by @stef and you're done.