Once my application with TextLinkAds was approved, I looked for a sample code to make it work in typo, or more generally in Ruby on Rails. TextLinkAds did not provide any, and a quick search in google did not return anything.
So I took their php example and came up with these snippets.
In your controller:
require ‘net/http’ require ‘cgi’ def content url = “http://www.text-link-ads.com/xml.php?inventory_key=”+TEXTLINKADS KEY+”&referer=”+CGI::escape(@request.env[‘REQUEST_URI’])+”&user_agent=”+CGI::escape(@request.env[‘HTTP_USER_AGENT’]) @links = request(url) rescue nil end def request(url) XmlSimple.xml_in(http_get(url)) end def http_get(url) Net::HTTP.get_response(URI.parse(url)).body.to_s end
And in your view (rhtml), you just need to add:
<% if @links != nil %> <% if (@links != nil)&&(@links["Link"] != nil) %> <% for link in @links["Link"] -%> <li><%= link['BeforeText'] -%> < a href="<%= link['URL'] -%>”><%= link['Text'] -%></a> <%= link['AfterText'] -%> </li> <% end -%> <% end -%> <% else %> Advertise here! <% end -%>
To make the above look right with markdown, I had to cut the < a > element in between. When pasting this, remove the carriage return. If someone has a better idea on how to prevent the < a > element from being interpreted as a link even in a code block, please let me know!
Based on the example, it is fine to cache the result, so I’ll be adding that later tonight. As soon as I get caching up and running, I’ll also post the typo sidebar plugin
5/4/06 update: I tweaked the code to account for the no links at all case. TextLinkAds starts with a test link that they later remove, and my code broke. I replaced:
<% if @links != nil %>
<% if (@links != nil)&&(@links["Link"] != nil) %>