CrudVision - Lisa Seelye

February 17, 2008

First Reve Trac Ticket

Filed under: api, bugs, eve online, open source, rails, reve, ruby, subversion, testing, trac — Lisa Seelye @ 11:58 am

Had my first bug report today about the to_i method I made on the String class breaking Rails migrations because leading zeroes weren’t being treated nicely in my method: "001".to_i # => 1 and fails the test 1.to_s == "001" and so a String was being returned fromto_i. Crap.

I don’t think that I need the method anyways (I’m going to spend part of today to make sure with more tests!) so I’ve removed it and tagged Release 80 and uploaded the gem to RubyForge. It should be available to gem update or gem install shortly.

This release also has a larger test coverage and some other unnecessary methods were pruned. Check it out!

February 9, 2008

An evil snippet for hash traversal

Filed under: json, rails, ruby, snippet, testing, work — Lisa Seelye @ 12:28 pm

First the problem:

I'm writing a load test framework at work and I need to consume JSON webservice and pass on the output to another request. But I don't always know what the data is that I need to pass on but I do know the basic "path" ("hpath" -- "hash path") to get to the data as the JSON data is uniform.

Now the method:

RUBY:
  1. # Picks out data from a (JSON) decoded hash based on the @passon hash,
  2. # which looks like this:
  3. # { "id" => "packet.products[0].attributes.id",
  4. #   "quantity" => "packet.products[0].attributes.quantity"
  5. # }
  6. # The "id" and "quantity" are the new keys for the return data;
  7. # packet.products[0].attributes.id will look at the value of the id key
  8. # in the attributes hash in the 0th element of the products array in the
  9. # packet hash.
  10. def pick_out_passon(hash_data)
  11.   return {} unless @passon
  12.   returnhash = {}
  13.   nh = hash_data.dup
  14.   @passon.each do |newkey,part_str|
  15.     parts = part_str.split(".").reverse
  16.     while (part = parts.pop) do
  17.       m = part.match(/\[(\d+)\]/)
  18.       index = nil
  19.       if m
  20.         index = m[1].to_i
  21.         part.gsub!(/\[#{m[0]}\]/,'')
  22.       end
  23.       nh = nh.values_at(part).first
  24.       nh = nh.at(index) if index
  25.     end
  26.     returnhash.merge!({ newkey=> nh})
  27.     nh = hash_data.dup
  28.   end
  29.   returnhash
  30. end

What's it do? It does magic!

I'll step through it...

Looks for @passon instance variable and doesn't do anything useful unless it exists.

Duplicate the input hash because the process done is destructive to it and we may need to reuse it.

For each new hash key and "hpath" pair from @passon split up the hpath into its parts and reverse it so Array#pop will work in the while loop to get the next first part to try.

Since each part of the hpath can examine an Array by index it has to be checked for and the index removed from the part (and saved).

Next, investigate the copy of the hash_data, nh by the computed key; if the value in the hash was an Array use the index to get the desired value. Then compute the next part!

Once we're out of parts stuff the new key and data into the returnhash and keep going til there's no more @passon pairs.

And thus some fun code was written.

February 2, 2008

A New Era in Reve: Gems

Filed under: deployment, reve, ruby — Lisa Seelye @ 6:35 pm

Okay it's a lame title but whatever.

There's a new release of Reve today (Release 76) and it can be installed as a gem!


gem install reve

And that's it.

Reve also has a project page at Rubyforge (http://reve.rubyforge.org) but I'll still host everything, sans gems, here on crudvision.com.

An autogenerated changelog is available from Reve's trac but a quick overview is:

  • Support to convert between Character ID and Character Name
  • Fixed a ParseDate problem reported by Paul
  • Now available in .gem format
  • Move reve.rb to lib to fall in line with more standard practice. This should be seamless upgrade unless one expicitly required reve.rb
    Reorganised other files into a subdirectory.

Enjoy!

(Edit at 2008-02-02 19:20 GMT: Fixed the Rakefile to include lib/ruby/*.rb as that's where the meat of Reve now lives. I imagine the .gem should be mirrored soon.

I appologise for the screwup here but this is my first gem! :) I'm getting used to the process still. It beats .tgz and .zip files though.)

Powered by WordPress