jeudi 26 avril 2012

Setup for new rails project with RSpec

I like to use RSpec instead of Test::Unit for my rails projects. Here are the steps I use to setup my projects.
Create new rails project without Test::Unit
rails new MYAPP -T
Add rspec-rails to your Gemfile:
gem 'rspec-rails'
Choose a javascript runtime in the Gemfile:
gem 'therubyracer', :platform => :ruby
Run bundle install:
bundle install
Install rspec in rails:
rails g rspec:install
Let's write the first spec!

mardi 7 février 2012

Debian gem install RMagick

I tried to install RMagick gem on my Debian box and got the following error:
checking for Magick-config... no
Can't install RMagick 2.3.0. Can't find Magick-config in [...]
I installed libmagickcore-dev:
sudo apt-get install libmagickcore-dev
But I still had an error with:
Can't find MagickWand.h
The problem was solved by installing libmagickwand-dev:
sudo apt-get install libmagickwand-dev
Hope that helps!

jeudi 2 février 2012

Review of "JavaScript & jQuery: The Missing Manual, Second Edition" by David Sawyer McFarland; O’Reilly Media

Disclaimer: I'm a member of the O'Reilly Blogger Review Program and as such, this book was freely provided to me by the editor.

David Sawyer McFarland has been building websites since 1995. In "JavaScript & jQuery: The Missing Manual", he tries to teach javascript and jQuery to web designers or beginner programmers so that they can build highly interactive web pages. This book is clearly aimed to people who know a little bit of HTML and CSS but want to add some dynamics to their pages.

The first two parts of the book teach the reader basics of javascript and jQuery. It begins with very basic things like "What's a Computer Program" and finishes with a tutorial to write an animated dashboard in jQuery. I have to say that I skipped most of these 200 pages as I already know most of what is written here.

The third part of the book gives lots of tips for improving images and navigation in your website. There are lots of tutorial which cover the most frequent cases that you find on modern websites. I liked the chapter on web forms for it introduce some very useful techniques for having smarter forms and interfaces.

The fourth part was clearly the most interesting to me: Ajax. David goes back to the basics of request/response and explains trough simple tutorials how Ajax works with jQuery. All you need to know to be able to make Ajax with jQuery is here! Moreover, the next chapter show how to use these new skills with popular web services from Flickr or Google Maps.

Finally, the last part "Tips, Tricks, and Troubleshooting" explain how to work with jQuery and javascript efficiently in a daily basis with things like traversing the DOM, regex and debugging.

I was a bit disappointed at the beginning as the first 200 pages are really very basic but then, I learned lots of practical tips that I will be able to use in all my web projects.

jeudi 29 septembre 2011

Ayende tax calculation challenge

I had some fun doing this little challenge from ayende, it's a good exercise to practice TDD. Here is my solution in C#.
The tests
[TestFixture]
public class TaxCalculatorTests
{
	[Test]
	public void Tax_for_0()
	{
		var calculator = new TaxCalculator();
		
		var tax = calculator.TaxFor(0);
		
		Assert.That(tax, Is.EqualTo(0));
	}
			
	[Test]
	public void Tax_for_5000()
	{
		var calculator = new TaxCalculator();
		
		var tax = calculator.TaxFor(5000);
		
		Assert.That(tax, Is.EqualTo(500));
	}
	
	[Test]
	public void Tax_for_5800()
	{
		var calculator = new TaxCalculator();
		
		var tax = calculator.TaxFor(5800);
		
		Assert.That(tax, Is.EqualTo(609.2).Within(0.001));
	}

	[Test]
	public void Tax_for_9000()
	{
		var calculator = new TaxCalculator();
		
		var tax = calculator.TaxFor(9000);
		
		Assert.That(tax, Is.EqualTo(1087.8).Within(0.001));
	}

	[Test]
	public void Tax_for_15000()
	{
		var calculator = new TaxCalculator();
		
		var tax = calculator.TaxFor(15000);
		
		Assert.That(tax, Is.EqualTo(2532.9).Within(0.001));
	}

	[Test]
	public void Tax_for_50000()
	{
		var calculator = new TaxCalculator();
		
		var tax = calculator.TaxFor(50000);
		
		Assert.That(tax, Is.EqualTo(15068.1).Within(0.001));
	}
}
And the implementation
public class TaxCalculator
{
	private readonly Tuple<decimal, decimal>[] slices = 
	{
		new Tuple<decimal, decimal>(40230, 0.45M),
		new Tuple<decimal, decimal>(21240, 0.33M),
		new Tuple<decimal, decimal>(14070, 0.30M),
		new Tuple<decimal, decimal>(8660, 0.23M),
		new Tuple<decimal, decimal>(5070, 0.14M),
		new Tuple<decimal, decimal>(0, 0.10M),
	};
	
	public decimal TaxFor(decimal sum)
	{
		if (sum == 0)
		{
			return 0;
		}
		
		var tax = 0M;
		foreach (var slice in slices)
                {
			if (sum > slice.Item1)
                        {
				var amount = sum - slice.Item1;
				tax += amount * slice.Item2;
				sum -= amount;
			}
		}
		
		return tax;
	}
}

mardi 20 septembre 2011

ReSharper test runner with MSTest

The current project I'm working on at my job uses MSTest for testing (and I don't like it!). Anyway, I had an error while running the test suite in Resharper test runner while there were none in Visual Studio test runner.

Actually, the ReSharper test runner create a specific directory where it copies all assemblies under test and runs the test from this location. One of my tests had to load all assemblies in the executing assembly path and some of them were missing. The problem comes from the fact that by default ReSharper is configured to shallow-copy assemblies being tested.

What you need to do is go to Resharper -> Options.

From left side pane select tools -> Unit Test

Uncheck Shallow-copy assemblies being tested

And now, all my tests pass as expected!

dimanche 18 septembre 2011

Bad URI error with heroku on windows

I wanted to push my little rails app to Heroku but after asking for my credentials, the heroku command failed with the following message:
C:/RailsInstaller/Ruby1.9.2/lib/ruby/1.9.1/uri/common.rb:156:in `split': bad URI(is not URI?): 10.35.10.249:8080 (URI::InvalidURIError)
        from C:/RailsInstaller/Ruby1.9.2/lib/ruby/1.9.1/uri/common.rb:174:in `parse'
        from C:/RailsInstaller/Ruby1.9.2/lib/ruby/1.9.1/uri/common.rb:628:in `parse'
        from C:/RailsInstaller/Ruby1.9.2/lib/ruby/gems/1.9.1/gems/rest-client-1.6.7/lib/restclient/request.rb:99:in `net_http_class'
        from C:/RailsInstaller/Ruby1.9.2/lib/ruby/gems/1.9.1/gems/rest-client-1.6.7/lib/restclient/request.rb:142:in `transmit'
        from C:/RailsInstaller/Ruby1.9.2/lib/ruby/gems/1.9.1/gems/rest-client-1.6.7/lib/restclient/request.rb:64:in `execute'
        from C:/RailsInstaller/Ruby1.9.2/lib/ruby/gems/1.9.1/gems/rest-client-1.6.7/lib/restclient/request.rb:33:in `execute'
        from C:/RailsInstaller/Ruby1.9.2/lib/ruby/gems/1.9.1/gems/rest-client-1.6.7/lib/restclient/resource.rb:67:in `post'
        from C:/RailsInstaller/Ruby1.9.2/lib/ruby/gems/1.9.1/gems/heroku-2.7.0/lib/heroku/client.rb:554:in `process'
        from C:/RailsInstaller/Ruby1.9.2/lib/ruby/gems/1.9.1/gems/heroku-2.7.0/lib/heroku/client.rb:536:in `post'
        from C:/RailsInstaller/Ruby1.9.2/lib/ruby/gems/1.9.1/gems/heroku-2.7.0/lib/heroku/client.rb:35:in `auth'
        from C:/RailsInstaller/Ruby1.9.2/lib/ruby/gems/1.9.1/gems/heroku-2.7.0/lib/heroku/auth.rb:96:in `ask_for_credentials'
        from C:/RailsInstaller/Ruby1.9.2/lib/ruby/gems/1.9.1/gems/heroku-2.7.0/lib/heroku/auth.rb:129:in `ask_for_and_save_credentials'
        from C:/RailsInstaller/Ruby1.9.2/lib/ruby/gems/1.9.1/gems/heroku-2.7.0/lib/heroku/auth.rb:71:in `get_credentials'
        from C:/RailsInstaller/Ruby1.9.2/lib/ruby/gems/1.9.1/gems/heroku-2.7.0/lib/heroku/auth.rb:19:in `login'
        from C:/RailsInstaller/Ruby1.9.2/lib/ruby/gems/1.9.1/gems/heroku-2.7.0/lib/heroku/command/auth.rb:12:in `login'
        from C:/RailsInstaller/Ruby1.9.2/lib/ruby/gems/1.9.1/gems/heroku-2.7.0/lib/heroku/command.rb:114:in `run'
        from C:/RailsInstaller/Ruby1.9.2/lib/ruby/gems/1.9.1/gems/heroku-2.7.0/bin/heroku:14:in `'
        from C:/RailsInstaller/Ruby1.9.2/bin/heroku:19:in `load'
        from C:/RailsInstaller/Ruby1.9.2/bin/heroku:19:in `
'
Actually, the problem comes once again from my proxy. My environment variable for the proxy was set to HTTP_PROXY=host:port while the Heroku gem expects HTTP_PROXY=http://host:port
I took me one hour to find this, I'd rather read be more carefully the error messages...

vendredi 16 septembre 2011

How to setup github access through corporate firewall

I had troubles cloning and pushing to my github account. My company has a firewall and all internet accesses are through the corporate firewall. Hopefully, I found an easy way to get access to my account with the following steps. I'm using msysgit 1.7.6 on a Windows 7 machine.
Configure the github global property http.proxy:
git config --global http.proxy http://host:port
Access your git repository through the http protocol. For public repositories it's
git clone http://github.com/username/project.git
And for private repositories, use the following command (you'll be prompted for your password):
git clone https://username@github.com/username/project.git
If you want to push your changes to your github account, you have to change the URL for your remotes and use the same format as specified above.