I’m doing a little work in Ruby this weekend and happened to write a small class for talking to the Yahoo! Mail Web Service. It only deals with the Yahoo! Mail bits, it doesn’t handle Browser Based Authentication (BBAuth). However, I thought I’d share it with everyone to show you how rediculously easy it can be.
To start, you’ll need the net/http (included in Ruby’s core) and json (oddly absent from the core, but available nontheless) modules. The client itself is a mere 18 lines long, although I’ll admit there’s very little error checking being done in this barebones version.
require "net/http"
require "json"
class YMail
def initialize(appid, cookie, wssid)
@appid = appid
@cookie = cookie
@wssid = wssid
end
def method_missing(method, argument)
body = JSON.generate({"method" => method.id2name, "params" => [argument]})
url = "/ws/mail/v1/jsonrpc?appid=#{@appid}&wssid=#{@wssid}"
http = Net::HTTP.new("mail.yahooapis.com")
res = http.post(url, body, {"Content-Type" => "application/json", "Cookie" => @cookie})
JSON.parse(res.body)["result"]
end
end
Using the YMail class is simple enough.
ymail = YMail.new(applicationID, bbauthCookie, bbauthWssid)
puts "Number of Folders = #{ymail.ListFolders({})['numberOfFolders']}"
You’ll have to pass your application ID as well as the BBAuth cookie and WSSID returned during the authentication process to the YMail constructor. However, once you’ve done so you can easily call any method available in the Yahoo! Mail Web Service and get back a Ruby hash to interrogate.