Gem-ified WBXML4R

Posted by tom May 21st, 2008

I’ve had a lot of requests for the sourcecode for WXBML4R – a Ruby extension which allows you to convert from WBXML to XML and vice-versa. So far I’ve sent the code to people who asked, but now I’ve gem-ified the code and with the use of a gem, it’ll be a lot easier! Hopefully I’ll soon find the time to create a Rubyforge page for it and put it on there.

UPDATE: I’ve just asked for the RubyForge project, once that’s approved I’ll publish the gem there.

Ruby Qualification Test

Posted by tom May 21st, 2008

Recently, as part of an interview, I’ve been asked to make the following test (in Ruby):

1) Write a program, which asks the user for a URI/URL. The program should then go off, and fetch the webpage which is on that URI/URL. You’re not allowed to use any existing HTTP library (ie use a socket connection yourself). 2) Extend the program so that it loops, until the user enter’s a q (for the URI). 3) Extend the program so that it displays the headers, separated from the body. The headers should be separated in key – value pairs.

I’ve perceived this as a very simple test, which hardly requires OOP. I would use OOP if it would simplify the solution or when it would contribute to the re-use of components. As this was a test, it’s inherently a one-off, therefor no OOP in my opinion. The testers felt otherwise, so I rewrote my entire solution as follows.


# 
#  test.rb
#  degrunt.net
#  
#  Created by Tom de Grunt on 2008-05-21.
#  Copyright 2008 degrunt.net. All rights reserved.
# 

require 'uri'
require 'socket'

class Request

  include Socket::Constants

  def initialize( uri )
    begin
      @uri = URI.parse(uri)
    rescue URI::InvalidURIError => e
      raise "Please enter a valid URI" 
    end
  end

  def get
    socket = Socket.new( AF_INET, SOCK_STREAM, 0 )
    sockaddr = Socket.pack_sockaddr_in( 80, @uri.host )
    socket.connect( sockaddr )
    puts "Path: #{@uri.path || '/'}" 
    socket.write( "GET #{@uri.path || '/'} HTTP/1.0\r\n\r\n" )
    socket.read
  end

end

class Response

  def initialize( text )
    @text = text

    headers,@body = text.split(/\r\n\r\n/)
    headers = headers.split(/\r\n/)
    @status_line = headers.shift

    @headers = {}
    headers.each do |h|
      kv = h.split(/: /)
      @headers[kv[0]] = kv[1]
    end
  end

  def to_s
    result = "" 
    @headers.each do |k,v|
      result << "#{k} - #{v}\n" 
    end
    result << @body
    result
  end

end

# ========
# = Main =
# ========
begin
  puts "Please enter a URI (press Q to quit): " 
  uri_str = gets.strip.downcase
  unless uri_str == 'q'
    request = Request.new(uri_str)
    puts Response.new(request.get)
  end
rescue Exception => e
  puts "An error occurred: #{e}" 
end until uri_str == 'q'

Please share your thought on whether using OOP or not is a good idea and whether this test successfully tests one’s qualifications as a Ruby programmer.

On the other hand, I can tell you that I’ve written Ruby Extensions (three by now) using Ruby and C-code, I wrote a Radiant CMS plugin and wrote a couple of Ruby on Rails websites in my spare time. Professionally I work on an extensive Ruby on Rails project, which tests the boundaries of Rails (if I may say so).

Ruby Units

Posted by tom January 6th, 2008

While we’re at it here’s also a little info about the brilliant gem “ruby-units”, which takes away all the nasty conversion from you.

It can be easily installed using the known:

gem install ruby-units

Here’s some sample code:

require 'rubygems'
gem 'ruby-units'
require 'ruby-units'

c = Unit.new("100cm") >> "ft" 
puts c.scalar
k = Unit.new("100","kg") >> "lbs" 
puts k.scalar

Ruby Currency

Posted by tom January 6th, 2008

Today, while I was looking how to do Currency conversion in Ruby in a simple manner, using up-to-date rates, I found the currency ruby gem, which can easily be installed using the regular:

gem install currency

Here’s example code (Using RubyGems 1.0.1):

require 'rubygems'
gem 'currency'

require 'currency'
require 'currency/exchange/rate/deriver'
require 'currency/exchange/rate/source/xe'
require 'currency/exchange/rate/source/timed_cache'

# Rate source initialization
provider = Currency::Exchange::Rate::Source::Xe.new
deriver  = Currency::Exchange::Rate::Deriver.new(:source => provider)
cache = Currency::Exchange::Rate::Source::TimedCache.new(:source => deriver)
Currency::Exchange::Rate::Source.default = cache

usd = Currency::Money('6.78', :USD)
puts "usd = #{usd.format}" 
cad = usd.convert(:CAD)
puts "cad = #{cad.format}" 

Trying to write a Rails ConnectionAdapter

Posted by tom January 6th, 2008

Today I’ve started with trying to write a Rails Connection Adapter for IBM U2 (UniData/UniVerse). There’s very little documentation available, only a couple of examples.

PostgreSQL on Mac OSX 10.5

Posted by tom November 10th, 2007

I’ve downloaded PosgreSQL from PostgreSQL for Mac and installed it, immediately after the server runs. From the diskimage I got the folders where the terminal binaries are installed (the GUI tools which come with the package don’t work for me) and it all worked. That saves a lot of compilation time!

Using Ruby on Rails with PostgreSQL is fairly simple, just install the ruby-postgres gem (gem install ruby-postgres) and have your database.yml represent something like:

development:
  adapter: postgresql
  database: my_development
  username: tdegrunt
  password:
  encoding: utf8

Ruby Xerces, Xalan and XQilla

Posted by tom September 27th, 2007

Ruby is badly in need of proper XML extensions for Xerces, Xalan and XQilla. That way it is easy do XML parsing and processing and transformation, one step closer to acts_as_enterprisey.

I’ve seen a couple of people suggesting the use of SWIG for creating these kind of extensions. I managed to compile the lot (Xerces, Xalan and XQilla) on Mac OSX (10.5 I might add), but using SWIG is beyond me (for now).

Ruby and Barcode Readers

Posted by tom June 5th, 2007

Today I finally “cracked” the Peninsula BlackBird II Portable Memory Barcode Reader communication protocol (using RS232 via USB) and wrote a Ruby class to read the scans from it. I got some help, from a PDF describing the protocol, but the PDF was very, very meager (to keep things polite). Nonetheless, I’m happy the guys over at Peninsula were kind enough to provide me with it.

Earlier I already wrote a Ruby class to read the scans from the IntelliScanner Mini. For that scanner however (which is actually a Symbol CS1504) is more than enough English documentation available.

Next on the todo list is to retrieve timestamps on each scan (I still need to investigate whether that is actually possible) from the Blackbird. And later, finish the code on the Mini.

Generating EAN13 Barcodes with Rails

Posted by tom June 3rd, 2007

Today I wrote code which generates EAN-13 Barcodes from Ruby/Rails, with the help of RMagick and ImageMagick. There already was a barcode-gem but that just generated Code39 Barcodes. Based on the code in that gem, better said: following the conventions in that code I created a class which now is capable of generating EAN-13 PNG images:

EAN 13

Why do I do such things? Well, it’s part of my new pet project: Barcodes on Demand

Ruby Gems got me confused

Posted by tom February 23rd, 2007

Recently (well okay maybe not so recently?) there were quite a bit of changes to RubyGems, one of which is that require_gem is deprecated. Previously I could say:

require 'rubygems'
require_gem 'mygem'

This would automagically require ‘mygem’ because of spec.autorequire. There’s also:

require 'rubygems'
gem 'mygem', '>=0.0.8'

This doesn’t do a require ‘mygem’ but I’m able to indicate a version which should be used. Then, which by now should be the standard (if I read everything correct):

require 'rubygems'
require 'mygem'

But where’s the version in that? Maybe this would be an even better solution:

require 'rubygems'
require 'mygem', '>=0.0.8'

But this doesn’t work: ArgumentError: wrong number of arguments (2 for 1). The question is: What is the solution? What should I use and what shouldn’t I use?

This week a colleague and myself have been working hard to create a Ruby extension which allows Ruby to make RPC calls to UniVerse and UniData (the IBM U2 databases). The module has been dubbed U2, the class UniRPC. It implements the intercall interface, and from that the functions: open, close, opensession, quit, readlist, execute, read, write & delete. Within the c-code it translates the @FM separated records (and readlists) into Ruby Arrays (for easy access). It currently does not translate @VM, @SM & @TM to sub-array structures, but maybe one day…

We’ve also created a ActiveRecord-like class to work with U2 files (tables & records) all in all pretty interesting!!

Twitter

Posted by tom January 30th, 2007

I’ve recently discovered Twitter and made a small Ruby class for it, to be further extended into the full API. I’ve included the sourcecode in this post, but …

Read the rest of this entry