A new Rails plugin for TextLinkAds (including support for Feedvertising)

Change of Strategy

Instead of updating my constantly breaking Typo sidebar plugin, and to implement Feedvertising from TextLinkAds, I’ve changed gears and chosen to implement just a regular plugin (very close to the new way of doing sidebar plugins in typo 4.1). This approach should work in all versions of typo, as well as any other Ruby on Rails’s application.

The support for feedvertising is slightly different than the one from the WordPress plugin that is the only option offered so far, but should be fairly close.

Installation

Get the plugin from subversion:

script/plugin install http://svn.nanorails.com/plugins/textlinkads/

or to use svn:externals and get future updates via svn update

script/plugin install -x http://svn.nanorails.com/plugins/textlinkads/

The installation will copy a file textlinkads.yml into your config directory.

The file looks like this:

key: TLA_KEY
affiliateid: 0
title: Sponsors
advertisehere: Advertise here!
testing: false
caching: true

Replace TLA_KEY with the one provided by TextLinkAds, set your affiliateid if you’d like to have a link to TextLinkAds with your affiliate id (so you can get credit if someone signs up for a TextLinkAds account). Change the title and advertisehere messages if you don’t like the defaults.

Set testing to false once you’ve verified it works (while testing=true, the plugin will use a special page provided by TextLinkAds that displays to links. However, that page does not contain any RSS links)

Finally, if the caching done by Rails is not enough, the plugin can cache the calls to retrieve the links. See the caching section for explanations on how to setup caching.

Integrating with Typo

Adding the display of regular links

To add the regular TextLinkAds links, you need to add a call to render_TLA anywhere in the rendering code. In typo, the most likely place is in your template’s default.rhtml

Here’s what my template looks like


<div id="sidebar">
  ...
  <div class="sidebar-node"><%= render_TLA %></div>
  <% response.lifetime = 6.hour %>
  <%= render_sidebars %>
</div>

to replace the original:


<div id="sidebar">
  <%= render_sidebars %>
</div>

Add the RSS links

For RSS2.0 for example, edit the file app/views/xml/_rss20_item_article.rxml to add a call to render_TLA_RSS(post_id)

here’s the original file:


  xm.item do
    xm.title post_title(item)
    if this_blog.show_extended_on_rss
      content = item.full_html
    else
      content = item.body_html
    end
    xm.description content
    xm.pubDate pub_date(item.published_at)
    ...

xm.item do
  xm.title post_title(item)
  if this_blog.show_extended_on_rss
    content = item.full_html
  else
    content = item.body_html
  end
  content += render_TLA_RSS(item.id)
  response.lifetime = 6.hour
  xm.description content
  xm.pubDate pub_date(item.published_at)

Depending of which format you need, you may need to edit a different file.

That’s it.

Integrating with other apps

For other apps, just take a similar approach and add calls to render_TLA and render_TLA_RSS where most appropriate. Both calls are accessible from any Controller or Helper class.

Caching

Set “caching: true” in textlinkads.yml. Make sure Rails is configured with some caching. Typically, you need to have


config.action_controller.perform_caching = true
config.action_controller.fragment_cache_store = :file_store, "#{RAILS_ROOT}/tmp/cache"

either set from config/environment.rb or config/environment/[CURRENT ENVIRONMENT].rb

This will cache the pages where the ads appear, and cache the feed pages. But to make sure these pages will update properly if the TextLinkAds ads inventory is updated, you must ensure the cache will expire.

The best way to do that is to use the expiring_cache_action plugin. To install:

script/plugin install http://typosphere.org/trac/browser/trunk/vendor/plugins/expiring_action_cache

Then where you added calls to render_TLA, just add:

response.lifetime = 6.hour

This way, the cached page will expire 6 hours later so that would be the lapse of time to wait to see the updated ads.

No need to expire the cache for RSS, it should get expired automatically every time an article is added.

If you use this plugin in other applications, add a comment or send me a not (psq_0×40_nanorails_0×2e_com) and I’ll add a link to your instructions or code.

TextLinkAds Typo Sidebar Plugin

Based on “Displaying ads from TextLinkAds in a rails application”, here’s the first release of my typo sidebar plugin to display ads from TextLinkAds. The plugin uses typo built-in caching as I explained before.

Download

Download either textlinkadssidebar.zip or textlinkadssidebar.tgz.

Installation

Unzip (unzip textlinkadssidebar.zip) or untar (tar xzvf textlinkadssidebar.tgz) directly into the components/sidebars/ directory of your typo installation.

Configuration

Using the sidebar tab of the admin section of typo, you’ll find an Item named “Text-Link-Ads” on the left hand side.
Simply drag it to the right side where at the desired location (the higher the better!).
Enter a title for that section (here I use nanoRAILS Sponsors).
Fill in your XML KEY from the “Get ad code” section on TextLinkAds.
Enter your affiliate ID (so you can get paid for referrals).
Enter the text for the referral link (I have “Advertise on nanoRails”).
Click on the Publish Changes button.
Once you refresh your blog, you will have a TextLinkAds section.

Text Link Ads

6/15/06 update: to use in the trunk of typo (1055 currently), you will need textlinkadssidebar-1055.zip or textlinkadssidebar-1055.tgz.

The short of it is that sidebar plugins have changed quite a bit! You may be better off recreating from scratch using one of the available ones.

The longer story is that you no longer need a configure.rhtml. Instead, you use the setting helper to describe each setting. You need to subclass Sidebars::ComponentPlugin instead of Sidebars::Plugin. You also need to remove the configure method and the way you specify the display name and and the description is also done with a helper.

Oh yeah, the file content.rhtml is unchanged :D

Release 0.2 of BookmarkIt! Sidebar Plugin for Typo

Well, version 0.1 was shortlived. There was a critical difference between running with WEBrick and running in production environment with fastcgi.
The value of env[“REQUEST_URI”] did not contain “http://host”. So I found a quick solution and we’ll see if there is a better solution tomorrow.

Current solution consists of replacing

@article_url = request.env[&#8220;REQUEST_URI&#8221;]

with

@article_url = &#8216;http://&#8217;+request.env[&#8220;HTTP_HOST&#8221;]+
  request.env[&#8220;REQUEST_URI&#8221;]
  if @article_url.eql?(@article_url.gsub(/http:/,&#8221;).gsub(/HTTP:/,&#8221;))

You live and learn…

bookmarkit-0.2.zip and
bookmarkit-0.2.tgz