Raspberry pi + ruby + sinatra = #winning

There is something about hardware/low level development that amazes me.
I am not really good at it, I don't actually want to be, my relation with hardware is the same I have with sports: I love to learn new sports, practice it a little bit, but when it starts to get too competitive, ie, the guys are getting serious about it, I loose interest.

That being said, I have to say I've played a lot with this things.
The first one was the pic. I friend of mine(electric engineer) helped me to get started and to like it.
The next step was to upgrade(or is it downgrade?) to arduino, a platform much easier than pic, and that comes ready for development.
I got a lot of fun with that, but then when I've moved to london, I brought just the essential, so it was left behind.

But when I read about the new raspberry pi, got all excited again!
I would be finally able to create high level code and run it in a computer with the size of a credit card.
Then you ask: but master, how can I do that???

Simple little grasshopper, you just have to have a pi and a little patience.
step1: install ruby;
step2: install sinatra;
step3: install node.js, and this is the tricky one, I had no idea it was needed...
run your code and GG!

Now, making your rasp visible to the outer world... that is for another post.
ps: It involves changing your router settings, and that.... that is enough to make me sad...


The development process.
This is something that can help or annoy the team, and most of the times those who created don't even realize it.

Last month I just met some team with a very interesting process.
They have only 1 tester, all the tests are automated.
Everyone is responsible for their bugs.

So far, so good, no problems but nothing new.
Now we have the interesting part:

The build process.
VMs are generated with a script downloading the latest code from Team Foundation Server.
This is the same script that creates the test environment L1 and L2, and production.

Why did I put that emphasis to the "and production"?
Simply because everyone is always too scared to mess with the production server, and this is the source of so many problems.

Just to compare, in the same company, I saw another team's server. But this one had around 10 different folders with different versions, and the production published folder was called staging.
There was a folder called prod, but this one wasn't being used.

Now the thing is: why did they let this mess?
Simple, no one wanted to be responsible for taking the server down, so the pile of crap just grows as more people touch it. And I have to confess, I wasn't exception... I just published my files in the weird named folder and left everything as it was.

Back to the story, the development / testing database is also reset every night (copied from production). Because of that, the developers have to make a script for loading the data when the tests takes more than one day.
Once the bug is fixed it in their environment, a routine runs to destroy the test environment, and recreate it with the updated code.
Once they receive the green light from the tester (and this is something that made me impressed, THE tester. That's right, just one for a big team of developers), the code is marked as good to go.

Then, around once a week, this code is published to the production server. the production server has its vm destroyed, and recreated using the latest approved code from the version control. This rolls so smooth that I was there during a new publish, and most of the team didn't even know that it was happening. they just do a quick test when it's done to make sure everything is ok.

This makes them more responsible for their code, but the problem about giving someone too much responsibility and not enough time is that things are not going to be as good as they could be.

Yes, the time, this is the downside, and it isn't a small amount of time. Their development cycle is quite slow, and any unforeseen task can take weeks or months to be developed. They have a fast lane of small tasks, but this only works for some features.
They have no problem with that, for this team it is more valuable to have less bugs than more features, but this is not always the reality, in fact what happens in most companies is exactly the opposite.

The golden rule for development is: you can have Time, Cost or Quality. pick TWO, and only two.
And by cost I mean getting more developers to give smaller proportion of work for each.

That being said, if you can afford to spend more time in order to get more quality, congratulations, you are already half way to a good product. Now it is time to pick a development methodology, and I really think this guys are using a pretty good one.

I feel this would have made my life so much easier in a few projects in the past.


Today it is all about tableless. Or is it?
A few years ago this new trend started growing among webdevelopers.

For some time, I haven't really seen a real advantage in that. And then I started to see some value, but still, I thought for most of the cases tables was faster to code, and also results in a clearer code.
But this time I had to make a hebrew website. This means to make an RTL version of the website. RTL means not only to write in the opposite direction, but also to mirror the whole thing.
And this time I was amazed by how easy the conversion was.
I've had to make some adjustments, but most of it worked like a charm.

What do you think you have to do when people tell you you have to have several languages for the same site? and what if some are LTR and others are RTL?

Well, one excellent recommendation from my friend Avi was to create variables in the css and replace them like this:

[ALIGN] = left
[OALIGN] = right
[ALIGN] = right
[OALIGN] = left

this alone can take care of almost all the problems when you change the direction.
It is quite impressive how this simple thing can make a whole site mirrored.

So if you are considering the possibility of having different direction for you site, please make it tabeless to begin with, or you will hate yourself later.


So, you want to create a expandable box, where if the user hovers the top part you show the whole thing, right?
Easy peasy, just set the hidden text display to none, outer_div:hover > div to block and you are all set, right?


I see why you could think that it works. Just because it makes sense, doesn't mean it works.
Don't forget a whole lot of people uses Internet Explorer, and IE will never allow you to have it that easy...

As usual for IE, we must do some css hacks.
Specially 2 things:
1 - to fix the hover around the title text, not only the characters, set the background to something other than transparent.
2 - to be able to hover over the expanded part and not get it hidden, set the height to 100%, but not only to the hover, or it will hide even the title when it is not being hovered

it should look like this:

this css is considering:

your text here

show this message when hovered
there you go, and I just couldn't not make this comment: I HATE INTERNET EXPLORER!


Just talk to them

Today I was having a cors(cross-origin resource sharing) problem.
You see, having a cross domain ajax call can be very important sometimes, but this is blocked for security reasons.
So finally I found out about this guys: http://enable-cors.org/
and their enable cors project.

Ok, this should be perfect, but... how will I change the headers of the response I am getting?
simple: Just talk to them.

Yes, as simple as that, I just sent them an email explaining what was happening and why I wanted that changed. In less than 1 hour, it was done.

Sometimes we overthink about solutions that won't involve the other side, when it should be a matter of a few minutes.

Anyway, thanks profundcom, your support was very helpful.

Ah, this is what is supposed to be in the header:
Access-Control-Allow-Origin: *

in asp.net:
Response.AppendHeader("Access-Control-Allow-Origin", "*");

That's it for now, hope it helps to solve your problems as well.


Dynamic CSS

Today I was in need of changing the css file for some pages in a MVC3 project.
Still I want to keep the layout page with a default css (overwritten in case there is another one for that page)
I've read several complex solutions, but then I thought: "that can't be right"
I mean, it is something too simple to use a whole nuget for that.

Did some tests, and it worked.
There are only 2 simple steps.

1: change your _layout page to this:

<link href="@Url.Content(ViewBag.Css ?? "~/Content/Site.css")" rel="stylesheet" type="text/css" />

2: in the pages you want to change the css files, simply do

ViewBag.Css = "~/Content/OtherSite.css";

Easy, right?


Writing JavaScript is awesome.
It is so simple, yet powerful. And if you add jQuery, it just makes me happy!

But there is a problem in here. There is no compiler for JavaScript, once I spent over 4 hours over a bug to find a 'd' character lost in one of the files.
Not today. Today I got this:


It will be in every single  site I do from now on.
And I strongly recommend you do the same.

This is not the only option, you can look for others, but most of them are online analyzers, and I think this one is easier to run, you just have to link it (and jQuery, silly)

<script src="jquery.js"></script>
<script src="jquery.lint.js"></script>
easy as Π 
Got it?


Web services(soap). This takes me back to 2005, in a project where we thought it was the solution for all the problems.
How wrong were we?

But to be fair, it is not all that bad. I remember back in 2004 when I did a college coursework consuming a .net webservice with a java application. The idea was awesome, but it was so painful I thought it would never be any good.
Now I had to do another coursework. Not for college degree, but anyway I had to build a website, and then convert it to a web service.
But this time I thought: I don't want to build another project to consume it, it will be just a waste of time.

Solution: JQuery. Yes, the solution for so many web questions is the answer once again...

And it was quite easy.
You just have to send it as xml, and build a xml envelope to send as data. Sounds hard?
Well, you will see how easy it is.
To build the envelope all you have to do is to access the webservice you generated, in my case Java.
As you can see, the SOAP request is already done for you, it's only a matter of copy and replace the values.
var soapMessage = '<?xml version="1.0" encoding="UTF-8"?>'
                    + '<S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope/"><S:Header/><S:Body>'
                    + '    <ns2:listCustomers xmlns:ns2="http://webservice/">'
                    + '    </ns2:listCustomers>'
                    + '</S:Body></S:Envelope>';
                    type: "POST",
                    url: "./WSManager",
                    data: soapMessage,
                    contentType: "text/xml; charset=\"utf-8\"",
                    success: function(msg) { $("#customerList").html(msg.childNodes[0].textContent); }

But the thing is, why to use soap?
Seriously, I can't see any reason in that. REST is so much better, is clean, nice, you can have it with json instead of the verbose xml, you don't have to send a whole xml as request.

I'll write some post about rest later, because now it is late and I've got to sleep.

see ya


To log or not to log?

To log or not to log?

You see, to log is mandatory if your software does more than a hello world. But the thing is, I've always thought it wasn't cool to stuff chunks of code all over the code, it makes the code ugly, and less readable.
I was using Log4Net, and it is not all bad, but as I said before, I don't like my code "dirty".

So today a friend of mine told me about the ELMAH, and I thought it is amazing. You might say it isn't that clean, if you want to log something you have to write code to log.
Of course you have, how else it is supposed to guess what you want to log???
But that's not even the most important part, the log caught errors and log them in a very nice way.

As I was trying to set aside a few hours to install and configure it, I've remembered one old Scott Hanselman's video about the .net gems nugets, and how easy it was to set up the addons using nugets.

This works like a charm, you can have it all in 3 simple steps:
  1. Install the ELMAH nuget.
  2. Create a database for it and run the sql file App_Readme/Elmah.SqlServer.sql
  3. Update the ELMAH connection string  in your web.config, just look for the already created key: <add name="elmah-sqlserver" connectionString="Data Source=****;User ID=****;Password=****;Initial Catalog=****;" providerName="System.Data.SqlClient" /> and then insert values about the database you just created.
And that's it, done, Easy Peasy...

Ah, and No, you don't have to look database records, or a shitty unformated text file...
Just access yourapp/elmah.axd and you will see something like this:

And clicking details, will be something like this:

Go ahead and start feeling guilty for logging all your mistakes. Forever. Because it doesn't forgive, and it never forgets...


My Rails Enviroment

If you have developed in rails, you probably love the language, but hate to set up the environment.
I know I do.

I've tried to use it in windows (it is hell) and linux (also not so good).
I thought the problem was just with me. My system is always with problems to run rails, and every time I had to work on something, I had a lot of work making it work again.

Finally a friend found a solution for that.
What he did is:
  • he created a virtualbox vm with ubuntu.
  • Installed ruby, gems, rails, etc
  • configured it to be accessible through ssh, and redirected to the port 3000
This sounds just perfect.
So all I had to do is to download this vm, install the editor I like(scite) and use it.
But it was made in a mac, with support to the x system, and windows doesn't have that.
Looking for that in a few blogs I found out about Xming, which is a software that allows you to run linux x applications on windows.

It was quite easy to set up and use.
  • Open putty
  • fill host name and port
  • click connection \ ssh \ X11
  • check Enable X11 forwarding
  • type :0.0 in x display location
like this:

after that you just have to open the file like this: scite index.html.haml
As you can see, the application looks different than a windows regular application, and that is because it is. This is running right from the vm, and the file being edited here is inside the vm.
I don't know about you guys, but to me, this is really impressive.

By the way, he wrote a post about it, and it is very nice to read if you want to know how to do it.
Here is the link for his page: Giorgenes' post about virtual ruby environment



PetaPoco. Said whaaaat?


So, maybe some of you guys checked out my dead database project, the dNet.Db. It is free and open source @ http://dnetdb.codeplex.com/
I started developing that framework because I always thought  it is important to automatize the simple and boring part of database. And by this I mean that it load an object values should be as easy as object.load(), or object.LoadList() if you want an array.
 And it was working fine. But we all know how a project like that requires a lot of maintenance, adding new databases support, fixing bugs and things like that.

A few weeks ago I was talking about this to a friend and he told me about the project massive.
Massive is nice, but it doesn't support everything I needed (mainly because of the stored procedures), so trying to go further into that, I found the PetaPoco(http://www.toptensoftware.com/petapoco/).

This is the kind of framework I consider that fit all my needs:
- really easy to install
- easy to fetch objects from the db
- supports sqlite
- allows you to use plain old SQL for all the other situations.

So I've been using it for a bit now, and it hasn't let me down. I am not thinking about using my framework anymore. So I decided to write a little tutorial of how it works.

Install: There is a nuget for that. If you don't know what a nuget is, here is a link, probably I'll talk about this someday, but this is really cool, check it out.


public class Project
      public int Id { get; set; }
      public string Name { get; set; }

Voil'a... Easy as pie...
then you could say:-Hum... the loading part might be tricky....
not really... this is the code:

var db = new PetaPoco.Database("Base");
ViewBag.Projects = db.Fetch<Models.Project>("");

and that's it.
Ok, that's not it. Well it is if you have a really simple object, but you don't.
So... now comes the tricky part?
Yes. Compared to the other part it is. but not so much

The loading bit is exactly the same, but now I'll add some more code to the model:
    public class Feature
        public int Id { get; set; }
        public int ParentId { get; set; }
        public int ProjectId { get; set; }
        public string Title { get; set; }
        public string Description { get; set; }
        public Project project { get; set; }
    public class Project
        public int Id { get; set; }
        public string Name { get; set; }
        public Dictionary<long, Feature> Features { get; set; }
    class ProjectFeatureRelator
        Dictionary<long, Feature> features = new Dictionary<long, Feature>();

        public Project MapIt(Project p, Feature f)
            Feature fExisting;
            if (features.TryGetValue(f.Id, out fExisting))
                f = fExisting;
                features.Add(f.Id, f);

            f.project = p;
            p.Features = features;
            return p;

Now, this looks a lot, but let's understand what happened there:
1 - I've added a new class (feature). This class have a primary key Id, and I want it to load the project associated with it.
2 - I've added a new property to the project class:Features. this is because I want the list of features to be loaded as well. Note that I've annotated the [PetaPoco.ResultColumn] to make the connection.
3 - So far so good, but now comes the ugly part: the join. I admit it is not an elegant code as the rest, but it is not so bad, and it is very easy, you just have to copy it, and use it in your code.

I don't know if it is a good idea to use this in a big project, but for the small ones, I will definitely use it.


What about mvc?

So seems like asp.net mvc is finally popular?
That's perfect, I mean, this is how it should have been since the beginning.

Let's start with what is mvc:
Mvc is to build the application as model, view and controller.
What does that means?
It means that the view should be only responsible for showing the interface, the data access should be in the model layer, and everything else should be done by the controller.

Sounds nice, right? Old but gold... But the thing is... and how about the web?
 Usually we had a asp / aspx / php / jsp / etc file. This file is requested, and then, from this point on you can call your controller logic.
But this isn't the proper way to do it. This way your view is mixed with the controller.

And then, there was the light! true MVC to the web applications.
Is this hard to implement? off course not, my little grasshopper....
You just have to think your application in a different way. and what is that way?

And your routes file should look like this:
public static void RegisterRoutes(RouteCollection routes)

            "Default",                                              // Route name
            "{controller}/{action}/{id}",                           // URL with parameters
            new { controller = "Home", action = "Index", id = "" }  // Parameter defaults

And how do you use it?
 Easy: you set it as your controller name, and action, so every time the user types that address the method defined as action is called.
so when the user types website/home/index, the method index inside the controller home is called.

Ok, we've got the code, but how to display it to the user?
In the controller method, there should be a return View(). You can return View("name-of-the-view")

Now you have the request processed in the right place, and you have your view. Sweet! :D
Buuuuuuuuut, something is still missing... and that's because you have to connect those 2.
And this is called the mighty ViewBag!

View bag - speaking like mr jobs would say - is a magical, fantastic... the better ever created object to carry values between controller and view.
So how do you use this thing?
ViewBag.YourVariable = "The title";

and in the view you can use it like this:

By the way, this brings up another topic:RAZOR

Once again: this is magical, fantastic, amazingly simple. you can put C# code in the view just by adding a @ in front of it.
And, if you want to put a chunk of code, just use
  //several lines of code

and how about the model?
Well... the model is the same as before, I won't talk about it here.
But that's it for now.
I'll write more about razor.

Hello world!

Hello world!

To start with, I'll explain why I am creating this blog. Maybe you know my old blog. It is dead now.
It was in my old server and I decided not to keep it, but what makes me mad is the domain. it expired and now I don't even have email.
Anyway, new blog, new posts, new life.
I don't hate java anymore, yay!
Just kidding... I still do.
But not as much.
You see, now the servlets seems to work. The IDEs are decent. And java is so far behind .Net, they don't even try to compete anymore... Maybe that's the best part.
So this is my first...ish post... at least the first in this blog.