Some Ruby Tucows API Client Code for You to Try

.net + Squishy Cows

Here at Tucows, we’re mindful of the astounding leaps and bounds that the Ruby programming language had made in the past few years. When I started here in July 2003, Ruby was an obscure programming language and our programmers’ decision to code Blogware (the blogging platform for this blog) in Ruby was considered dubious by many. Thanks largely to the Ruby on Rails web development framework and also to the strength and friendliness of the Ruby development community, Ruby has been adopted by many programmers — many of whom are Java “defectors” — as their favorite new language (new to them, not new to the world; Ruby debuted in 1993).

Mike Bowler of Gargoyle Software was kind enough to share a simple snippet of code with us. It’s a transliteration of the example code provided in the XML Over HTTPS Post documentation [176K PDF] into Ruby.

Here’s his code being used to send a LOOKUP DOMAIN command for the domain example.com to the Horizon test server. The server response is simply printed onscreen:

require 'net/http'require 'net/https'require 'digest/md5'

srs_host = 'horizon.opensrs.net'srs_port = 55443srs_user_name = 'YOUR USER NAME GOES HERE'srs_private_key =    'YOUR PRIVATE KEY GOES HERE'

requestXml = <<-END<?xml version="1.0" encoding="UTF-8" standalone='yes'?><!DOCTYPE OPS_envelope SYSTEM 'ops.dtd'><OPS_envelope> <header>   <version>0.9</version> </header> <body>   <data_block>     <dt_assoc>       <item key="protocol">XCP</item>       <item key="action">LOOKUP</item>       <item key="object">DOMAIN</item>       <item key="attributes">         <dt_assoc>           <item key="domain">example.com</item>         </dt_assoc>       </item>     </dt_assoc>   </data_block> </body></OPS_envelope>END

digest = Digest::MD5.hexdigest(Digest::MD5.hexdigest(requestXml+srs_private_key)+srs_private_key)

http = Net::HTTP.new srs_host, srs_portdoc = nil

begin  http.use_ssl = true  req = Net::HTTP::Post.new('/')  req['Content-Type'] = ‘text/xml’  req['X-Username'] = srs_user_name  req['X-Signature'] = digest  req['Content-Length'] = requestXml.size.to_s

  response = http.request(req, requestXml)  puts response

ensure  http.finish if http.started?

end

It’s simple enough, but I hope to make this the basis for a Ruby library — perhaps a Rails plugin or helper — for accessing Tucows’ APIs.

Please note that this is not official code – it’s something that a partner wrote and kindly shared with us to share with you. It comes with no guarantees. Support won’t be able to help you, and I’m just figuring it out myself. I will be posting more articles about it, and if you have any comments, questions or suggestions, let me know in the comments for the article or drop me a line!

Our thanks to Mike for sharing his code with us!

1 Comment

  1. Wow nice post! Elbroder

    Comment by Anonymous — May 18, 2007 @ 7:29 pm

RSS feed for comments on this post.

Sorry, the comment form is closed at this time.