Archive for the ‘Ruby’ Category

Updating rubygems from 1.2.0 to 1.3.5

You would think it this process is straight forward but it appears there were changes that prevent simple update but here are the steps:

1. Update to 1.3.0:

$> sudo gem install rubygems-update -v 1.3.0
...
 
$> sudo update_rubygems
...

2. Update to the most recent release

$> sudo gem update --system
...

3. Check the version

$> gem --version
1.3.5

That’s it.

AutoGrowling: making autotest and spectest work with Growl

I like test and feature driven development. It keeps me on track when writing ruby code. Since I work on few different projects and some of these projects use TestUnit while others use Rspec I needed the .autospec hook to handle both formats.

So here is another tutorial on how to get these things working together:

  1. Download Growl from: http://growl.info/
  2. Install the downloaded dmg
  3. Install growlnotify from Extras/growlnotify folder in the dmg
  4. Copy the folder to your desktop
    Open the terminal and run following commands.

    $ cd Desktop/growlnotify
    $ sudo sh install.sh
  5. Create
    ~/.autotest_images/

    folder.

  6. Save following images into the folder.

Install following gems: ZenTest, redgreen:

$ [sudo] gem install ZenTest
$ [sudo] gem install redgreen

Download the script below and rename the file to .autotest and place it in your home folder user folder.

##!/usr/bin/ruby
require 'autotest/redgreen'
require 'autotest/timestamp'
 
class AutoTestNotifier
  IMAGEDIR = File.expand_path("~/.autotest_images")
  FAIL_IMG = IMAGEDIR + "/rails_fail.png"
  PASS_IMG = IMAGEDIR + "/rails_pass.png"
  PEND_IMG = IMAGEDIR + "/rails_pending.png"
 
  def self.growl(data)
    AutoTestNotifier.new(data).notify
  end
 
  def initialize(data)
    @priority = nil
    @result = {}
    if data.last =~ /examples/i
      parse_spec_message(data)
    else
      parse_unit_message(data)
    end
  end
 
  def parse_spec_message(data)
    @result[:type] = :spec
    output = data.last.strip.gsub(/\e\[\d+m/, '').split(',')
    output.map do |i| 
      val, lbl = i.strip.split(" ")
      @result[lbl.to_sym] = val.to_i
    end
  end
 
  def parse_unit_message(data)
    @result[:type] = :unit
    output = data.last.chop.split(',')
    output.map do |i| 
      val, lbl = i.strip.split(" ")
      @result[lbl.to_sym] = val.to_i
    end
  end
 
  def img
    return FAIL_IMG if @result[:errors].to_i > 0 || @result[:failures].to_i > 0 
    return PEND_IMG if @result[:pending].to_i > 0 
    return PASS_IMG
  end
 
  def title
    pre = @result[:type] == :spec ? "Spec Tests" : "Unit Tests"
    return "#{pre} Failed:" if @result[:errors].to_i > 0 || @result[:failures].to_i > 0 
    return "#{pre} Pending:" if @result[:pending].to_i > 0 
    return "#{pre} Passed:"
  end
 
  def to_s
    msg = ""
    msg << "Examples: #{@result[:examples]}     " if @result[:examples]
    msg << "Tests: #{@result[:tests]}     " if @result[:tests]
    msg << "Assertions: #{@result[:assertions]}     " if @result[:assertions]
    msg << "Pending: #{@result[:pending]}      " if @result[:pending]
    msg << "Failures: #{@result[:failures]}     "
    msg << "Errors: #{@result[:errors]}" if @result[:errors]
    msg
  end
 
  def priority
    return @priority || 0
  end
 
  def stick
    return ""
  end
 
  def notify
    cmd = "growlnotify -n autotest --image #{img} -p #{priority} -m '#{to_s}' #{title} #{stick}" 
    `#{cmd}`
  end
 
end
 
module Autotest::Growl
  Autotest.add_hook :ran_command do |at|
    AutoTestNotifier.growl(at.results)
  end
end

This should take care for both the UnitTest and RSpec messages.

Fixing libxml-ruby 0.9.9 on Leopard OS X

Update: The problem has been fixed in version 1.0.0.

Recently I have tried to update my gems and I have noticed that things just stopped to a grinding halt when it got to libxml-ruby. It appears that the 0.9.9 spectacularly barfed all over itself and it stopped other gems from being updated. Not good!

I started Googling for some answers but since the release has only 2 days I guess not that many people have run into it just yet.

I have found a repo with a patch:

http://github.com/tadman/libxml-ruby/tree/master

If you not paranoid you may do:

$ git clone git://github.com/tadman/libxml-ruby.git 
$ cd libxml-ruby
$ ruby setup.rb
$ rake gem
$ sudo gem install admin/pkg/libxml-ruby-0.9.9.gem

If you are paranoid then you would go to the install directory where the original gem broke:

$ cd /Library/Ruby/Gems/1.8/gems/libxml-ruby-0.9.9
$ cd ext/libxml

Edit:

ruby_xml_html_parser_context.c
ruby_xml_parser_context.c
by inserting “extern” before “ID IO_ATTR;” statement.

After these files are updated

$ cd ../..
$ ruby setup.rb
$ rake gem
$ sudo gem install admin/pkg/libxml-ruby-0.9.9.gem

iPhone screenshot processing – part 2

Today I was working on automating of preparation of iPhone screen shots for usage on the website. The new eideticsoftware.com website design requires for the images to have drop shadows so instead processing everything in Adobe® Photoshop® I have decided it would be much nicer to just automate the process with rmagick.

If you are impatient and just want the script jump to the bottom of the post and download image_processor.rb but if you want to know how it works then read on.

This script scales the images for the post.

example_scaled
First we take the original image and scale it by 50% –

source = original.scale(0.5)











example_scaled_stripped
Next we strip out the status bar with crop method:

source = source.crop(0, 10, 160, 240)











example_scaled_stripped_canvas_with_shadow
We create a transparent canvas that is 10px wider and 10px longer and another identical canvas that we are going to use for the shadow. On the image_shadow canvas we draw a rectangle with the same dimensions as the original image. In the source file the rectangle is created by the “drop_shadow_rectangle” function.

We use the image_shadow canvas to create two shadows, first is the actual shadow, the second we use as a mask to soften the shadow itself.

We apply the first shadow to the original canvas with “OverCompositeOp” option and then we apply the mask on top of that with “CopyOpacityCompositeOp” option.


example_scaled_stripped_canvas_with_image
We apply the scaled and cropped image on top of the shadow and draw a white border. The white border is only necessary when the screen shot is dark to distinguish the shadow from the image.

Finally we write the image to a file with “canvas.write” method.







Source Code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
require 'rubygems'
require 'rmagick'
 
include Magick
 
img_filename = "example.png"
 
 
# Helper function to create new name
def new_name(img_filename, modifier = "_shadow")
  chunks = img_filename.split(".")
  chunks[0..-2].join(".") + modifier + ".#{chunks[-1]}"  
end
 
# Creates drop show rectangle
def drop_shadow_rectangle(x,y,width,height)
  gc = Draw.new
  gc.fill_color("black")
  gc.rectangle(x,y,width,height)
end
 
# creates frame draw object
def white_frame(x,y,width, height)
  frame = Draw.new
  frame.fill_opacity(0)
  frame.stroke_color("white")
  frame.rectangle(x,y,width,height)
end
 
 
def add_shadow(img_filename)  
  original = ImageList.new(img_filename)
 
  source = original.scale(0.5)
  source.write(new_name(source.filename, "_scaled"))
  source = source.crop(0, 10, 160, 240)
 
  canvas = Image.new(source.columns + 10, source.rows + 10) do
    self.background_color = "transparent"
  end
 
  # Generates intermediate images
  #source.write(new_name(source.filename, "_stripped"))
  #canvas.write(new_name(source.filename, "_raw_canvas"))
 
  image_shadow = Image.new(canvas.columns, canvas.rows) do
    self.background_color = "transparent"
  end
 
  drop_shadow_rectangle(0, 0, source.columns, source.rows).draw(image_shadow)
 
  blur = 1
  x, y = 1, 1
 
  image_shadow = image_shadow.shadow(x, y, blur, 1)
  shadow_mask = image_shadow.shadow(x, y, blur, 1)
 
  # Place the shadow
  canvas.composite!(image_shadow, 0, 0, OverCompositeOp)
 
  # Place the mask to soften the shadow
  canvas.composite!(shadow_mask, 0, 0, CopyOpacityCompositeOp)
 
  # Intermediate image  
  # canvas.write(new_name(source.filename, "_canvas_with_shadow"))
 
  # Now place the image on the canvas
  canvas.composite!(source, 1, 1, OverCompositeOp)
 
  # And draw the white border on top of the image
  white_frame(0, 0, source.columns, source.rows).draw canvas
 
  # save the final image
  canvas.write(new_name(source.filename, "_canvas_with_image"))
end
 
add_shadow(img_filename)

Processor: image_processor.rb

Automating iPhone screenshots processing with rmagick

In programming repetition is evil. No one should do the same task more than once so I was not to excited about doing screenshots for FitTimer.

Taking the screenshots is not a big deal.

  1. On your device Hold the sleep/wake button and click the “home” button.
  2. Connect the device to your mac and import your images to iPhoto
  3. Select the imported images in iPhoto and drag them to a desired folder.

Unfortunately you can’t use these raw images. Apple requires you convert them to JPEG format and recommends you crop out the iPhone status bar. This task for many people mean opening each screenshot in Photoshop, cropping the image to “320 x 460″ and saving it as a JPG.

One may say: “It’s only a few images” but if you often update to your applications UI updating screenshots becomes one more thing you have to do. Additionally, if your website requires screenshots of different size or thumbnails, your screenshot updating task will take more and more time.

There is a better way of dealing with screenshots. You can automate the whole process with a few simple lines of ruby code using rmagick gem.

To use rmagick you will need to install the gem. You can find a link to installation instructions at the end of the post in the references section.

After you have rmagick successfully installed you can start processing your images.

I keep all my marketing material in <project_root>/marketing folder to keep it together in a single source code repository (git), however you are welcome to keep it anywhere you like.

To use my script you will need to prepare a few things:

  1. Create your working folder (in my project – “marketing”)
  2. In the working folder create three folders “raw_images”, “itunesconnect” and “web_images”
  3. Copy the script to the working folder
  4. Copy your raw ‘png’ screenshots to raw_images
  5. If you are copying the files from iPhoto you may want to rename them to something human readable like “main_view.png”
  6. Open your favorite terminal app and run the script

The script does 4 things.

  1. Crops the status bar and saves screenshots for itunesconnect in the itunesconnect folder
  2. Saves the cropped screenshots in the web_images folder
  3. Reduces the image by 50% and saves it in web_images folder with new name <filename>_50.jpg
  4. Creates a thumbnail by reducing the original image to 25% of it’s original size and saves it in web_images folder with new name following schema <filename>_thumb.jpg
require 'rubygems'
require 'rmagick'
 
Dir.glob('raw_images/*').each do |original|
  path = original.split('/')
  name = path[1].split('.').first
 
  itunes_img_path = "itunesconnect/#{name}.jpg"
 
  web_img_path = "web_images/#{name}.jpg"
  web_img_scaled_path = "web_images/#{name}_50.jpg"
  web_img_thumb_path = "web_images/#{name}_thumb.jpg"
 
  original = Magick::ImageList.new(original)
  cropped = original.crop(0,20, 320, 460)
  cropped.write(itunes_img_path)
  cropped.write(web_img_path)
 
  scaled = cropped.scale(0.5)
  scaled.write(web_img_scaled_path)
 
  thumb = cropped.scale(0.25)
  scaled.write(web_img_thumb_path)
end

References

Rmagick installation instructions
iTunes Connect Developer Guide can be found on the itunesconnect website in Manage Your Applications Section.

Rails, tests and code coverage

“If it’s not tested assume it’s broken.” This is one of the mantras in my company. Sometimes it’s easier said then done. If I always had to test everything I would be doing nothing but testing. So I employ different techniques to keep me programming and the computer to test the code I write.

One of the nice things about Rails is that you have a whole array of tools to automate testing and at least one very nice tool to let you know what’s tested and what’s not. Yes I am talking about Rcov.

Rcov has been “the bee’s knees” for code coverage awareness. Recently I have noticed Rcov has been doing more work then expected. When I ran the rcov command in a rails project it tested everything. I mean everything, including gems installed on the system.

Since everything is just a bit too much for my project I started looking for a way to limit the output. It appears I had to use -x option to stop the code coverage tool from traversing the gems. Here is the whole task:

desc 'Measures test coverage using rcov'
Rcov::RcovTask.new(:rcov) do |rcov|
  rcov.pattern    = "test/**/*_test.rb"
  rcov.rcov_opts  = [
    "-x 'gems/.*,rubygems/.*,rcov/.*'",
    "-T",
    "--rails" ]
  rcov.output_dir = "rcov/unified"
end

I hope this helps you.

Gaussian Rounding in Ruby

While working on a research tool for my work I have noticed that my ruby solution rounds differently then the original c# .net code. It was not in every but about 10 percent of my tests failed due to the different rounding schemes.

Ruby uses the Symmetric Arithmetic Rounding while C# .net uses the Gaussian rounding otherwise known as the Banker’s routing.

I have looked for a quick solution for Ruby but Google did not return a result I could immediately use. I have found a javascript solution which I translated to Ruby.

Additionally I have added a “decimal” parameter to specify the precision of rounding.

Here is the code:

class Numeric
  def _round(val)
    sign = val < 0 ? -1 : 1
    return (val.abs.round * sign) if (val.abs - val.abs.floor) != 0.5
    return (val.abs.ceil  * sign) if val.abs.floor % 2 == 1
    return (val.abs.floor * sign)
  end

  def gaussian_round(decimals = 0)
    return (_round(self * (10**decimals))/((10**decimals).to_f))
  end
end

Ref: Javascript Gaussian Rounding Example
Ref: Wikipedia article on rounding

Using MacRuby to set Xcode project version from git

I have been looking into automating some of my development tasks in Cocoa/Xcode environment.  For the longest time I was using Subversion which would integrate quite well with Xcode. Recently, however, I have been exploring other version control systems.  Especially git.  Since it’s a much newer (D)VCS it does not have as much integration into systems as subversion.

I have managed to find some interesting solutions to include Git version number in Xcode project however the only actual solutions I have found are written in languages I don’t care for anymore.  Yes I can program in them but why would I want to.

So here is a quick solution written in MacRuby.

#!/usr/local/bin/macruby

git_output = `git show --abbrev-commit`
commit_version = git_output.split("\n").grep(/^commit/).first
version = commit_version.gsub(/^commit\s+(.*)\.{3}/, "\\1")
if version
  list = NSMutableDictionary.dictionaryWithContentsOfFile("Info.plist")
  list["CFBundleVersion"] = version
  list.writeToFile('Info.plist', :atomically => true)
end

In my final solution I intend to have an output in following format:

major.minor.revision (build)

where the “build” is going to correspond with the git commit version
and the other parts will reflect the marketing version numbers.


Ref: Shiny Frog Article Python solution
Ref: Cocoa is my Girlfriend Article Perl solution

Contributing to ActiveMerchant

I have always been a fan of open source and wanted to contribute some code to a project or two. Somehow, I never found a project I would really enjoy and could directly benefit from as well. I was simply looking for an project that would scratch my itch.
Finally while working on an e-commerce system for my company I have decided to use active_merchant to connect to PayPal. The original implementation used by active_merchant was targeting and older xml based gateway and I have decided to re-implement the gateway using more recent NVP (name-value pair) interface. Hopefully this will allow for more modern features of the PayPal service.
A few days ago Cody has committed my patch and it is currently part of the active_merchant main code.

I have also created git repository where I can add more functionality as needed.
Here is the link for the interested:

git clone http://git.furmanek.net/active_merchant.git

Enjoy.

Ruby acrobatics aka. metaprogramming

Dave Thomas shows how to make ruby into a beautiful meta-programming language. So if you want to know how things like has_many in Ruby on Rails works, here is your chance.

http://www.infoq.com/presentations/metaprogramming-ruby