While I'm learning Ruby (and a bit of Python) I decided to make things a bit more difficult and I started to learn Vim.
For a while, I thought Vim was just a way for some developers to show off and snob us, the stupid people using "notepad-like" editors. While I still think that really smart "notepad-like" editors like Eclipse, Netbeans and Intellij Idea can be really useful and let developers code and read code faster, I can now understand why Vim is a very valid contender in the race to the best text editor. But damn! The learning curve is steap!
To help me, I found this blog from this canadian dude, Derek Wyatt, and his screencasts teaching how to use Vim. He's a bit crazy (especially in the first video) but his style and passion actually helps a lot.
So if you ever consider learning how to use Vim or get better at using it, take the time to watch Derek Wyatt's screencasts.
By the way, this is my first post written using Vim and I swear I didn't used the arrow keys (almost).
Still on my journey to learning the Ruby language, I've ported a small PHP script I've written a while ago to quickly eject external drives on my MacBook Pro in Ruby.
I converted it to an app with Automator
Here is the code (MIT license):
Comments and critics are welcome.
I'm a developer. But as a developer, I'm fairly faithful. I really seriously started programming with PHP. I learned and used this language thouroughly. I consider myself an expert programmer in this language.
It's more or less my native language. My second language is JavaScript. I started playing with it a long time ago but really discovered its power in 2007 with Mootools.
Yes Mootools! jQuery is for sissies! Just kidding, jQuery is fine but if you want to really learn JavaScript, try Mootools or ExtJS, read this, Crockford's book and watch his lectures.
I've played with other languages, some really close to what I know - ActionScript is just JavaScript- others a little more different like Python. Python is nice but we have our fair share of Python developers at the office. What's the fun of learning a new language if you have the security of being able to ask the guy next door?
So as you probably guessed with this article's title, I've decided to learn Ruby. And I just figured out that I should document this journey. I don't know if it'll help anybody but, heh, why not?
So to be fair, I've tried Ruby before. Not very seriously. And because of this blog actually. It's built on Jekyll which is written in Ruby. When I wanted to change some stuff I realized I should probably learn Ruby first.
This introduction is already way too long so let's get to the point.
new method to transform a string in a pattern. I mean, the php.net documentation is not that good but a least you get examples in their context...require 'json' directive didn't work even though I had installed the gem. Actually, I still don't understand why, I just found that with require 'rubygems' before, it was working.So, for the couple of geeks that'll read this, if you know soemthing about Ruby, have advices, books I should read, please let me know in the comments :)
Sorry but here's a new post about this blog. I've already talked about my choice of Jekyll as the engine for this blog.
Don't worry, I'm still very happy with it!
But we're not in the 20th century anymore and even if I don't need 99% of the features delivered by Wordpress, some are important to me.
I like the external links of my site to open a new tab/window (with the HTML target="_blank" attribute).
Since I didn't want to dig into Jekyll's rendering process, I chose to add the HTML attribute to all external links with some JavaScript:
var domain_root = document.location.protocol+'//'+document.location.host;
var all_links = $('a').each(function(index,element){
if(element.href.substr(0,domain_root.length) !== domain_root)
{
element.target = '_blank';
}
});
Yes I use jQuery and yes some of you will scream but I don't care, most of your CPU's cores are idle right now so I decided to put them to work a bit.
In modern browsers, it shouldn't slow down your browsing experience at all.
I will tweet 10 times more than I'll post here (at least and not counting RTs). So I wanted my Twitter feed included in this blog.
My first reflex was to try out Twitter's official JavaScript widget but let's be honest it's really ugly! And customize it is a pain in the a**.
So after searching a bit, I chose Tweet!. A lot more options, a dedicated CSS and I host it so I can fiddle a bit with it. I used Twitter's official icons and CSS sprites though.
For those, just go to this blog entry. It contains everything you need to build an RSS feed (I used feedburner to track you though :p ), the Categories page and do Search Engine Optimization (sitemap, keywords, etc.). Great stuff for everyone who wants to use/switch to Jekyll.
As you can see, I'm using Disqus. Not much more to add.
I'm using Bootstrap, the CSS toolbox from the Twitter team. Really easy for a style-impaired guy as myself. Works great on iOS too.
Finally, I get to the point where I discuss the second item of this entry's title: Node. The unofficial name of this beast is Node.js and for those who lived underground for the past year, it's server-side JavaScript on top of an event loop or as Ryan Dahl puts it: "evented I/O for V8 JavaScript".
There are a lot of resources available on the interwebs if you're interested in Node. I suggest you take a look at Ryan Dahl's talk and read this tutorial.
So, search. How do you add search to a static website?
First reflex: Google. Tried it, didn't work, too complicated and they even ask for money. No thanks.
Second idea: any plugin available for Jekyll? Yes but (there's always a "but", I know you saw it coming) it uses indextank API which a freemium service. No thanks.
Third idea: I can do it myself! But to stay consistent, let's not use PHP, Ruby or some usual server side language.
Let's populate the backlog:
Let's map this to Node and the existing setup:
How does it work?
There is first an indexing script and then the server part that delivers search results.
The indexing script parses a striped-down list of the posts generated by Jekyll and populate the SQLite DB. It is ran at build time. It seems simple but writing non-blocking code and looping are not that easy to mix the first time. If you checkout the source code, you may notice there is unused code for word indexing. I'll use this for autocompletion suggestions when I have time.
The server queries the SQLite DB for search results and returns JSON to the search AJAX query. Here, it is really simple, just had to clean up search queries to avoid SQL injections. Some duplicated code to clean up though :$
How is it setup?
So the search indexer is ran at built time from a git hook script.
I have a bare repository (/var/www/blog_repo/) acting as a remote for both my local repo and the deployed repo (/var/www/blog/). I also have a Github repo to share this great code. When I push from my local repo, the bare repo post-receive hook is ran:
git push github master # Push updates to the Github repo
cd /var/www/blog/ # Go to the deployed repo
env -i git pull origin master # Update the deployed repo
export PATH="/var/lib/gems/1.8/bin/:$PATH"
jekyll --no-auto # Build the blog
cd /var/www/blog/search_indexer/
node search_indexer.js # Build the search index
The only thing it doesn't do is start the node server because I didn't figured out yet how to run an upstart script as a non-root user. Here's the upstart script:
description "node.js server"
author "Simon Jodet"
start on started mountall
stop on shutdown
respawn
respawn limit 99 5
script
export HOME="/var/www"
exec sudo -u www-data /usr/bin/node /var/www/blog/search/search.js >> /var/log/node.log 2>&1
end script
The good thing is that I only need to reload it when I update the search server script which is not that often.
Finally, the server is behind a Nginx reverse proxy. I'll let you search the web on how to setup Nginx as a reverse proxy. Here's the virtual host configuration:
server
{
listen 80;
server_name blog.jodet.com;
location = /50x.html
{
root /var/www/nginx-default;
}
access_log /var/log/nginx/jodet_access.log;
error_log /var/log/nginx/jodet_error.log debug;
index index.html;
location /search/
{
proxy_pass http://127.0.0.1:1337/;
}
location /
{
root /var/www/blog/_site;
}
}
Simple, right? Nginx rocks! And guess, what? It is built on an event loop too.
If you have questions or see an issue with my setup, drop me a comment please. It will be much appreciated!
Finally, my blog's code is available for all to check on Github. Apart from the blog posts, it's under the MIT license so you're free to do whatever you want with my code (but not my posts!).

Trim
I just discovered that the trim function I've been using for so long to remove whitespaces around strings can also remove any other character. I've been cleaning by hand those path items of possible trailing slashes for so long...
Curl issue with PHP 5.3.8
I've encountered an issue on a project after updating to PHP 5.3.8 (the dotdeb package on Debian).
If you're calling a HTTPS domain with a self-signed certificate, CURLOPT_SSL_VERIFYPEER to 0 is not enough anymore. CURLOPT_SSL_VERIFYHOST should be also set to 0.
HEAD stop
Accordind to this, calling PHP with the HEAD http method will make stop execution on the first output. This is potentially dangerous and a security breach.
British PHP
What would have happened if PHP had been invented by some British chap? The answer is here.
Ok. For the tenth time at least, I'm starting a blog. I'm like a little school girl promising to herself every year to keep a diary. Epic fail each time...
Every time, there is a good reason for me to start a blog. This time around, I really wanted to use Jekyll and get rid of Wordpress.
Wordpress is probably one of the best blog engine out there. However, it doesn't fit my needs and has a number of major flaws:
Hi all,
First of all, if you're not a PHP developer, don't read this.
This post will be about my tries and misses with TDD. To make things harder, I'm adding into the mix PHPUnit (of which I have minimal knowledge), namespaces (because I'm also a JavaScript developer and I feel at home now) and Jenkins (fka Hudson).
First of all and against all odds, TDD is fun! I swear, I really enjoy it. And adding Jenkins multiply the fun because you can gloat, showing off you 100% test coverage, your sky-rocketing test count, etc.
Now, I wouldn't write a post just to say TDD is fun. Anyway TDD becomes fun and worthy of the effort when you have figured out some quirks of the different tools.
PHPUnit is a pain in the a**. But it seems to be the de facto standard so... I'm waiting for a stable version of Atoum though.
I got 3 major issues with PHPUnit:
1- On a previous project, I found out the process isolation support is incomplete. To have a real process isolation, you have to put your tests in separate files! And it is really long to run the complete test suite. So you've got 3 options: multiply the file count of your project, curse PHPUnit's author or rewrite your project and stop using constants for everything...
2- Namespace support is weird, especially when using type hinting of classes. My advice: use full-qualified names. Not a big deal, you SHOULD always use full-qualified names anyway. That way, your fellow developer will not have to learn the whole namespace hierarchy to understand your code.
3- Mocks are cool but they're not meant to fix all your problems. You want to test if an object's method is called: mocks are the solution. You want to make sure that during a future rewrite your fellow developer will not forget to update the client class of another? Use a real instance of the object, not a mock of it. Here's a stupid example:
<?php
class Conf
{
public function get_conf($key)
{
//do something
}
public function set_conf($key,$value)
{
//do something
}
}
class Client
{
public function __construct(Conf $conf)
{
$conf_entry = $conf->get_conf('key');
$conf->set_conf('key2','value2');
$conf_entry_2 = $conf->get_conf('key2');
if($conf_entry_2 === false)
{
throw new Exception();
}
}
}
?>
To make sure the get_conf method is called by Client's constructor, use a mock.
To make sure the exception is thrown if $conf_entry_2 is empty, use an instance of Conf. Why?
Because someday, Conf may return null instead of false after a rewrite. The Conf unit tests will be changed and the developer will be happy, he's got 100% passed tests. But the Client class has not been changed because you've mocked Conf to return false. The tests are passing but the developer didn't rewrite Client to test $conf_entry_2 against null instead of false.
I've also got an issue using autoloading with namespaces. I'm not sure it's related to PHPUnit but anyway... I liked the solution I found because it will prevent some possible code collision which is the main benefit of namespaces in the first place. The solution? Instead of naming your autoload function __autoload(), use a specific and very unique name such as myFantasticAutoloadingFunctionThatKicksAss() and use the spl_autoload_register() function to register it as an autoload function. After that PHPUnit was able to find my classes by himself.
Now TDD is starting to be fun.
PS: I will update this page if I find other quirks.
Update:
I've got 2 new quick tips:
once(), any() or none() in expects(), use at(0) for the first call and at(1) in the second and so on. You can even make the mock return different values each time. I needed this in my previous example because you usually retrieve configuration more than once.
Twitter feed