Mephisto Trackback Library

I really enjoy using Mephisto but it is missing some of the features that other blogging applications have. One is the ability to send a trackback ping to another blog post. So I built my own library to handle it.

Download trackback.rb

require 'net/http'
require 'uri'

# Simple trackback class to send a trackback to another weblog
# This is used via the console
#
# Example:
# >> t = Trackback.new("theadmin.org", 227)
# >> t.send('http://trackback.example/url.php?1234')
#
class Trackback

  @data = { }

  def initialize(host, article_id)
    site = Site.find_by_host(host)
    article = site.articles.find(article_id)

    if article.nil?
      raise "Could not find article"
    end

    if article.published_at.nil?
      raise "Article not published"
    end

    @data =  {
      :title => article.title,
      :excerpt => article.body_html,
      :url => "http://" + get_full_url(site, article),
      :blog_name => site.title
    }
  end

  def send(trackback_url)
    u = URI.parse trackback_url
    res = Net::HTTP.start(u.host, u.port) do |http|
      http.post(u.request_uri, url_encode(@data), { 'Content-Type' => 'application/x-www-form-urlencoded; charset=utf-8' })
    end
    RAILS_DEFAULT_LOGGER.info "TRACKBACK: #{trackback_url} returned a response of #{res.code} (#{res.body})"
    return res
  end

  private

  def url_encode(data)
    return data.map {|k,v| "#{k}=#{v}"}.join('&')
  end

  def get_full_url(site, article)
    article_peramlink = article.hash_for_permalink
    url = site.host + '/' + site.permalink_style
    ['permalink', 'year', 'month', 'day'].each do |value|
      url.gsub!(/:#{value}/,article_peramlink[value.to_sym].to_s)
    end
    return url
  end

end

To install this just save the above code to a file in your Mephisto’s lib/ directory. Now you can send a trackback to a website using the Rails console:

$ script/console production
>> t = Trackback.new("theadmin.org", 227)
>> t.send("http://acheron/blog1/wp-trackback.php?p=1")
=> #<Net::HTTPOK 200 OK readbody=true>

I haven’t had a lot of time to work on it, so I hope I can get some help improving it. Some ideas I have to improve it:

  • Turn into a Mephisto plugin
  • Unit tests
  • Scan all urls in the Article, looking for trackback urls.
  • Support other types of trackbacks

Download trackback.rb

Eric

9 thoughts on “Mephisto Trackback Library”

  1. Hi Eric,

    thanks for sharing, I was looking for that! Please share the Mephisto plugin if you happen to write it.

  2. Panagiotis Atmatzidis: Yes, you would send the trackback using the console. I’d like to have it send a trackback whenever an article is posted but haven’t had time to implement that yet.

  3. Okay! I wish I was a ruby guru, because it’s a feature that already missed twice. If you do implementation sometime in the future, plz drop a mail.

    Regards,

    and thank you in advance.

Comments are closed.