Search code examples
ruby-on-railsruby-on-rails-3twittergsubtwitpic

Ruby on Rails pull an id from url, insert into image_tag (gsub?)


I have a twitter feed displaying my tweets on a page. I want to include the thumbnails of my twitpic images as well. I've been using gsub to replace text, but I'm having a hard time figuring out the syntax.

A typical 'twitpic tweet' output with a raw tag will look like this:

This is a tweet with an image. http://twitpic.com/12345

Then the link for the image is

http://twitpic.com/show/thumb/12345.jpg

Here's the simple code to display the text:

<% @tweets.each do |tweet| %>
  <li>
    <%= raw tweet.text %>
  </li>
<% end %>

So I've been experimenting with gsub but am pretty stuck.

I can search for http://twitpic.com/* by /http:\/\/twitpic.com?[^]+ and then take out everything with the url, replacing it with raw html, but this doesn't help if I want to link an image.

If I could extract the twitpic id (http://twitpic.com/12345), I could put that 12345 into a helper method that inputs a string and inserts it into a linked image.

If anyone could give me some insight on how to extract this id, it would be greatly appreciated.


Solution

  • Maybe you can use #split instead of a regex?

    "This is a tweet with an image. http://twitpic.com/12345".split("http://twitpic.com/")[1]