My Ruby blog client class

2008 January 26
by Joel

Okay, so I’ve been messing with programmatically posting stuff on this blog from Ruby. From casual googling for Ruby client libraries, I found useful hints at Ramana Hridayam. Granted, his code is aimed at Drupal, but since it uses MetaWeblog and Blogger API’s, it works with WordPress as well. After getting the gist out of his code, reading up on the MetaWeblog API, and fiddling around with my own really rudimentary code, I finally came up with the following client class:


require "xmlrpc/client"

class BlogClient

  attr_accessor :blog_id
  attr_accessor :blog_url
  attr_accessor :username
  attr_accessor :password

  def initialize(blog_id, blog_url, username, password)
    @blog_id=blog_id
    @blog_url=blog_url
    @username=username
    @password=password
    @server = XMLRPC::Client.new2(@blog_url)
  end

  def new_post(title, body, categories)

    postmap = Hash.new
    postmap["title"]=title
    postmap["description"]=body
    if categories!=nil then
         postmap["categories"]=categories
    end

    post_proc = Proc.new {@server.call("metaWeblog.newPost",@blog_id, @username, @password, postmap, true )}
    server_call(post_proc)

  end

  def edit_post(post_id, postmap)

    post_proc = Proc.new {@server.call("metaWeblog.editPost",post_id,@username,@password,postmap,true)}
    server_call(post_proc)
  end

  def delete_post(post_id)

    post_proc = Proc.new {@server.call("blogger.deletePost", "none", post_id, @username, @password, true)}
    server_call(post_proc)

  end

  def get_post(post_id)
    post_proc = Proc.new {@server.call("metaWeblog.getPost",post_id,@username,@password)}
    return server_call(post_proc)
  end

  def get_recent_posts(num)
    post_proc = Proc.new {@server.call("metaWeblog.getRecentPosts",@blog_id,@username,@password,num)}
    #return a Hash containing info of posts
    return server_call(post_proc)
  end

private
  def server_call(proc_obj)
    begin
      proc_obj.call()
    rescue XMLRPC::FaultException =>e
      puts "Error: "
      puts e.faultCode
      puts e.faultString
    end
  end
end

To use this class, you pass your blog id, the xml-rpc url of your blog (e.g. ‘http://yourblog.wordpress.com/xmlrpc.php’), username and password to your blog, as parameters to the constructor. From there you can use the class’ instance methods to post entries to your blog, etc. To post an entry with categories, you pass your categories as an array of strings to the last parameter of the new_post() method. Now since all xml-rpc calls can throw FaultExceptions, I decided to just place all server calls in Proc blocks and pass them to the server_call method, where I do the exception catching.This code is what I used to post the previous two entries here. I got more plans for this client — mostly just to address my own blogging needs. I don’t really intend to write yet another gui blogging client, but there have been things I’ve been meaning to do in this blog, particularly for managing my categories and any links that broke when I imported posts from my old blogs.

Anyway, hope other people find my client code useful, even if only as learning tool to developing your own client code. :-)