Adding Social Bookmarks in Rails

Thinking of adding some social bookmarklets to your Rails project? Well, you should! It's a great way to drive traffic to your site and get that extra exposure. Now, while refactoring some of my code from Graphics World (you can see the code below at work in the right sidebar) I figured I should share this snippet since it's fairly tedious to setup. Here it is, in all its glory:

> social_helper.rhtml

    <%  current_uri = u(request.protocol << request.host_with_port << request.request_uri)
        title = u(@title)
 
        bookmarklets = {
            :delicious =>   ['del.icio.us', 'delicious.gif' ,'http://del.icio.us/post?url={{url}}&amp;title={{url_encoded_title}}'],
            :digg =>        ['digg it', 'digg.gif', 'http://digg.com/submit?phase=2&amp;url={{url}}&amp;title={{url_encoded_title}}'],
            :spurl =>       ['spurl', 'spurl.gif', 'http://www.spurl.net/spurl.php?title={{url_encoded_title}}&amp;url={{url}}'],
         #  :wists =>       ['wists', 'wists.gif', 'http://wists.com/r.php?c=&amp;r={{url}}&amp;title={{url_encoded_title}}'],
            :simpy =>       ['simpy', 'simpy.gif', 'http://www.simpy.com/simpy/LinkAdd.do?href={{url}}&amp;title={{url_encoded_title}}'],
         #  :newsvine =>    ['newsvine', 'newsvine.gif', 'http://www.newsvine.com/_tools/seed&amp;save?u={{url}}&amp;h={{url_encoded_title}}'],
            :blinklist =>   ['blinklist', 'blinklist.gif', 'http://www.blinklist.com/index.php?Action=Blink/addblink.php&amp;Description=&amp;Url={{url}}&amp;Title={{url_encoded_title}}'],
            :furl =>        ['furl', 'furl.gif', 'http://www.furl.net/storeIt.jsp?u={{url}}&amp;t={{url_encoded_title}}'],
            :reddit =>      ['reddit', 'reddit.gif', 'http://reddit.com/submit?url={{url}}&amp;title={{url_encoded_title}}'],
         #  :fark =>        ['fark','fark.gif', 'http://cgi.fark.com/cgi/fark/edit.pl?new_url={{url}}&amp;new_comment={{url_encoded_title}}&amp;linktype={{category}}'],
            :blogmarks =>   ['blogmarks', 'blogmarks.gif', 'http://blogmarks.net/my/new.php?mini=1&amp;simple=1&amp;url={{url}}&amp;title={{url_encoded_title}}'],
            :yahoo =>       ['yahoo', 'yahoo.gif', 'http://myweb2.search.yahoo.com/myresults/bookmarklet?u={{url}}&amp;t={{url_encoded_title}}'],
         #  :smarking =>    ['smarking', 'smarking.gif', 'http://smarking.com'],
         #  :segnalo =>     ['segnalo', 'segnalo.gif', 'http://segnalo.com/post.html.php?url={{url}}&amp;title={{url_encoded_title}}'],
            :magnolia =>    ['magnolia', 'magnolia.gif', 'http://ma.gnolia.com/bookmarklet/add?url={{url}}&amp;title={{url_encoded_title}}']
        }
 
     %>
 
   <div>
      <% bookmarklets.each_value { |bookmarklet|
            bookmarklet[2].gsub!(/\\{\\{url\\}\\}/, current_uri)
            bookmarklet[2].gsub!(/(\\{\\{url_encoded_title\\}\\})|(\\{\\{title\\}\\})/, title) -%>
 
        <p><a href="<%= bookmarklet[-1] %>" title="<%= bookmarklet[0] %>:<%= title %>">
            <img src="/images/social/<%= bookmarklet[1] %>" alt="<%= bookmarklet[0] %>" /> <%= bookmarklet[0] %>
        </a></p>
      <% } %>
   </div>
 

socialYou can see a quick preview of the results on the left. If you don't like the layout or need to adjust the formatting just change the xhtml at the bottom of the template. You can also add/remove bookmarklets you wish to include by simply commenting out the hash values. Each value points to an array where first field is the service name, second is the image name (just in case), and third is the URL on which we will run a regex. If you see a missing service, or need to get the icons head here. If you add any new services to the hash, let me know, I'll add them to my sample above to make it easier for everyone.

Note that I'm using code above as a shared template, but you could easily turn this into a helper function. Also, I assume that you have a @title variable which corresponds to your current page title. If you use static titles, or a different scheme altogether, you'll have to modify it accordingly. And last, but not least, make sure you keep the u(...) function around the current URI and page title - it's there to URL encode both variables ('My Title' turns into 'My%20Title').


About this entry