Re.Mark

My Life As A Blog

Archive for July 2008

Ruby Tuesday #16 Part 2 : IronRuby news

leave a comment »

At OSCON last week, John Lam made a number of announcements about IronRuby (and speaking of OSCON, you should check out the announcements Sam Ramji made, too.)  In summary:

There’s a binary release of IronRuby, which includes the standard Ruby libraries.

An ironruby-contrib project has been established – another way to get involved in the IronRuby community.

A set of changes to RubySpec have been submitted.

The full blog post on the IronRuby announcements is here.  Well worth reading in full.

Written by remark

July 29, 2008 at 5:45 pm

Ruby Tuesday #16 Part 1 : A Little Light Metaprogramming

leave a comment »

One of the reasons I was interested in learning about Ruby is metaprogramming.  For those new to Ruby, consider the following code:

class Test
 (0..5).each do |i|
    define_method "method_#{i}" do
      puts "Hello World #{i}"
    end
  end
end

Ok, it may not be the most useful piece of code in that form, but I think it shows just how metaprogrammable Ruby is.  When you remember that you can modify classes at runtime, alias methods and so on, the possibilities start to become clear.  If this has got you thinking about metaprogramming and what you could use it to achieve, this post goes into more detail on metaprogramming techniques in Ruby.

Written by remark

July 29, 2008 at 5:36 pm

Ruby Tuesday #15 : Ruby Round Up

leave a comment »

There’s a few things I’ve noticed over the last week in the Rubyverse that I thought it was worth highligting.  In no particular order, here we go.

In my Ruby doodlings, I’ve spent quite a bit of time with REXML.  GIven that I’m focussed on learning Ruby rather than deploying code, there’s no issue.  However, this post from RubyInside shows there are alternatives such as libxml-ruby.

Testing, both TDD and BDD style, are prominent in the Ruby community.  This post examines the reasons to unit test, questions the emphasis placed on unit testing and highlights the importance of remembering why you test.  Well worth reading and relevant regardless of your preferred language.

Finally, I came across a couple of resources that those who, like me, are learning Ruby may find valuable.  The first is the news that there is a new chapter in the Book of Ruby.  The second is a couple of posts about learning Ruby for C#ers.  These posts also mention IronRuby, so well worth reading.

Written by remark

July 23, 2008 at 6:33 am

Ruby Tuesday #14 : Creating an Authority

leave a comment »

This week I decided to put my new-found Ruby knowledge to practical use.  Marc has embarked on a micro quest to build a game in WPF.  I’m going to contribute to the project along the way – the first contribution was to write basic save and load routines for maps.  The next step is to enhance that to support SQL Server Data Services – (I’ll blog about the non-Ruby elements to this another time.)  SQL Server Data Services (SSDS) uses an authority as the highest level of storage.  Authorities contain containers, which in turn contain entities.  The first step, then, is to create an authority.  This step is a one-off operation, so it lends itself to an administrative script – I figured Ruby would be a good choice for this task.

As it turns out, someone else must also figure Ruby is a good choice because there’s documentation on MSDN about how to create an authority using Ruby here.  In fact, it looks like there’s Ruby samples for all of the REST interface to SSDS.  Instead of just copying the code and creating the authority, I thought I’d take some of the code I’d written for the Twitter client and re-use it (copy and paste style) for this task.  I wanted the code to be as simple as possible and I ended up with this:

require 'net/https'
require 'rexml/document'

username = 'your user name goes here'
password = 'your password goes here'
authority_id = 'and here is where you put your authority id';

req_xml = "<s:Authority xmlns:s='http://schemas.microsoft.com/sitka/2008/03/'><s:Id>#{authority_id}</s:Id></s:Authority>";

response = ""

http = Net::HTTP.new('data.beta.mssds.com', 443)
http.use_ssl = true
http.start do |http|
  request = Net::HTTP::Post.new("/v1/")
  request.basic_auth(username, password)
  request['Content-Type'] = 'application/xml'
  request['Content-Length'] = req_xml.to_s.size.to_s
  request.basic_auth(username, password)
  response = http.request(request, req_xml)
end

case response
when Net::HTTPSuccess then
  puts authority_id + ' created'
  error = false
when Net::HTTPForbidden then
  puts "SSDS Access denied"
  error = true
when Net::HTTPBadRequest then
  puts "Request is not valid"
  error = true
when Net::HTTPConflict then
  puts "SSDS Authority already exists"
  error = true
else
  puts "Unexpected Error"
  error = true
end
if(error)
  # process the http response body
  xml = REXML::Document.new(response.body)
  puts "Error: #{xml.root().elements[1].name} => #{xml.root().elements[1].text}"
  puts "Error: #{xml.root().elements[2].name} => #{xml.root().elements[2].text}"
end

The big difference between this and the Twitter code is the use of SSL.  That’s led to a few differences in the use of the Net::HTTP library.  The code in the MSDN example can also handle proxies, which my code can’t, so it’s worth a look at how that’s accomplished.  The code works and I now have a freshly minted authority – I fell foul of the naming rules a couple of times (e.g. authorities can only contain lowercase characters, numbers and hyphens – more info here), but the error reporting is great at pointing out exactly what you’ve done wrong.  And with the code for creating an authority complete, creating a container should be straightforward.

Written by remark

July 15, 2008 at 6:58 pm

Ruby Tuesday #13 : Testing

with one comment

Having completed a rudimentary Twitter client, I thought it was high time I figured out how unit testing works in Ruby.  The good news is that there’s a framework (called Test::Unit) distributed with Ruby.  To create a class of tests, simply create a new class and inherit from Test::Unit::TestCase.  Any method in the class that begin with test will be executed as tests.  Here’s a simple test for the translator in my Twitter client:

require 'test/unit'
require 'Twitter'
require 'rexml/document'

include REXML

class Test_Translator < Test::Unit::TestCase

    def test_element_to_user
        xml = '<user>
        <id>99999999</id>
        <name>Username</name>
        <screen_name>Screen Name</screen_name>
        <location>Location</location>
        <description>Description</description>
        <profile_image_url>image_url</profile_image_url>
        <url>url</url>
        <protected>false</protected>
        <followers_count>404</followers_count>
        </user>'
        translator = Translator.new
        element = get_user_element(xml)
        user = translator.element_to_user(element)
        assert_equal('99999999', user.id)
        assert_equal('Username', user.name)
        assert_equal('Screen Name', user.screen_name)
        assert_equal('Location', user.location)
        assert_equal('Description', user.description)
        assert_equal('image_url', user.image_url)
        assert_equal('url', user.url)
        assert_equal('false', user.protected)
        assert_equal('404', user.followers_count)
    end

    private

    def get_user_element(xml)
        xml_document = Document.new(xml)
        xml_document.root
    end
end

Run that test and it tells me there are 8 assertions in 1 test.  And there’s an error.  The error is a NoMethodError – tells me that there is no method followers_count on the User instance.  And looking at the class it’s clear why:

class User
    attr_reader :id, :name, :screen_name, :location, :description, :image_url, :url, :protected

    def initialize(id, name, screen_name, location, description, image_url, url, protected, followers_count)
        @id = id
        @name = name
        @screen_name = screen_name
        @location = location
        @description = description
        @image_url = image_url
        @url = url
        @protected = protected
        @followers_count = followers_count
    end
end

There’s an instance variable that gets set when an instance is created, but there’s no property defined.  All that’s needed is to update the first line of the class like so:

attr_reader :id, :name, :screen_name, :location, :description, :image_url, :url, :protected, :followers_count

And the test passes.  Lots more to learn in Test::Unit, but that feels like a good start.

When I started this series of Ruby Tuesdays, Matt recommended ZenTest, so I thought I’d look at that, too.  You can install it as a gem.  Once it’s installed, I called it with the following command:

zentest Twitter.rb >Test_Twitter.rb

That creates a file called Test_Twitter.rb that contains tests for the methods in Twitter.rb.  Here’s a brief excerpt:

class TestClient < Test::Unit::TestCase
    def test_download_friends_timeline
        raise NotImplementedError, 'Need to write test_download_friends_timeline'
    end

As you can see, for each method an error is raised to prompt you to write the test code.  There’s some other goodness in there like the autotest daemon that automatically runs your tests as you make changes.  And if you like to have your tests running automatically and you use Growlthis might be for you.

Written by remark

July 8, 2008 at 7:05 pm

Made Up Technical Terms #12

leave a comment »

Interfarce – an unreliable means of transferring information between systems.

Written by remark

July 8, 2008 at 7:38 am

Are you experienced?

leave a comment »

Everywhere I go these days there’s a new experience awaiting me.  My local supermarket, for instance,  proudly boasts that it offers the best shopping experience.  Every time I see the word experience used in this way I am reminded of the following quote:

"Experience is what you get when you don’t get what you want."

Thinking of it that way makes me wonder if the supermarket should be so keen to boast…

Written by remark

July 5, 2008 at 9:06 pm

Posted in General