Getting started with Raspberry Pi

I got my hands into a Raspberry Pi, so I thought it was time to start playing with it. My goal is to show the first steps to get your Pi running, so basically installing the OS in the SD card.

I am going to install Raspbian, which you can get from Raspberry Pi’s download page. I used the torrent option, which downloaded a zip file. After getting the zip file, extract it’s contents and keep it handy for later.

To verify which devices are mounted run:

1
df -h

Now insert your SD card in your computer and run the command again. The device that wasn’t there is your SD card. The output looks something like this:

1
/dev/sdb1  3.7G 4.0K  3.7G  1% /media/adrian/Pi

Now that we know the device, we need to unmount it:

1
umount /dev/sdb1
Read More

Writing JavaScript unit tests with venus.js

JavaScript used to be a language mainly for handling minor interactions or animations in the browser, but not anymore. Now you can see full applications built using JavaScript. Frameworks like backbone have brought architecture to the browser and you can even see JavaScript being used in the server with frameworks like node. As JavaScript becomes a language for building real applications, it becomes important to also adopt professional practices like having a way write and run unit tests.

Venus

Venus is a test runner that makes it very easy to run your tests and plug them into a CI system. Venus is written in JavaScript and runs on Node. It lets you choose which tools you want to use to organize your tests, write assertions or create mocks.

You can get more information about venus at venus’ website and you can get the code at github.

Read More

Getting started with Jenkins Continuous Integration

For those of you who don’t know, Jenkins is an application that allows you to create a Continuous Integration environment for your software projects. Most of the time it is used to periodically run test and build your project in an environment that should be similar to your production environment.

Installation

To install Jenkins on a Debian based system you only need to use:

1
sudo apt-get install jenkins

This command will install it and will automatically start it as a service. This service will be started automatically every time your system is restarted.

If you are using another OS you can download a war file from Jenkins main site and run it using:

1
java -jar jenkins.war

To see your instance of Jenkins running you can visit http://localhost:8080

Read More

Why and how is dust.js asynchronous

Today we had a discussion at work about which templating library we should use for our backbone applications. Since Dust is the library most teams in the company use, we thought it may be the direction we want to take. I have never used Dust, but some people in the room mentioned that the fact that it was asynchronous made it a little painful to do some tasks. It was very weird for me to hear that the rendering of templates happened asynchronously, mostly because I don’t know a way to make JS execute asynchronously other than using setTimeout. So I decided to dive into the code and figure out how they are doing it.

I started my journey by getting dust from github. The only file I really needed was dist/dust-full-0.3.0.js, so I got the file and built a simple example in an HTML file:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
<html>
<head>
  <script src='dust-full-0.3.0.js'></script>
  <script>
    var compiled = dust.compile("Hello {name}!", "intro");
    dust.loadSource(compiled);
    dust.render("intro", {name: "Fred"}, function(err, out) {
      console.log(out);
    });
  </script>
</head>
<body>
</body>
</html>
Read More

JS Dependency Management

It is common for JavaScript applications to depend on libraries, or JavasScript files to depend on other files. The way we usually deal with this problem is by manually including the files we depend on on our document:

1
2
3
4
5
6
<script src="jquery.js"></script>
<script>
$(function() {
  // Do something
});
</script>

To deal with this problem some people came out with the Asynchronous Module Definition (AMD) API, which allows us to specify modules or files that our code depends on and have them automatically loaded for us. The API defines one global function require that allows you to define the dependencies of the module and it’s functionality. Something like this:

1
2
3
4
5
6
7
<script>
require(['jquery'], function($) {
  $(function() {
    // Do something
  });
});
</script>
Read More

Create MySQL users

I always forget the syntax to create a new mysql user, so I decided to write it in here for later reference. I am doing this from a mysql terminal, so first we need to login:

1
2
mysql -uroot -p
mysql>

And then we can:

1
2
CREATE USER 'username'@'domain' IDENTIFIED BY 'password';
GRANT ALL PRIVILEGES ON database.table TO 'username'@'domain'

When issuing the GRANT command you can use wildcards like this:

1
GRANT ALL PRIVILEGES ON *.* TO 'username'@'%'

And that is pretty much all there is to it.

Read More

Writing a simple Node.js server

I recently wrote a post about node.js and this time I’m going to talk about one of it’s most common uses; a web server.

Thanks to the libraries that come packed by default with node, creating a very simple server is very easy. Create a file named server.js and add this content to it:

1
2
3
4
5
6
var http = require('http');

http.createServer(function(req, res) {
  res.writeHead(200, {'Content-Type': 'text/plain'});
  res.end('I\'m a server');
}).listen('9000', '127.0.0.1');

Save the file and open a terminal in the folder where that file is stored. Then run node server.js and visit http://127.0.0.1:9000. You will see something like this:

Node server

Might not be very exciting but this is were everything starts.

Read More

Introduction to node.js

Node.js is a relatively new technology (born on 2009) that allows us run JavaScript code from outside a browser. This had already been done in the past by Mozilla Rhino, but node.js gained a lot of popularity for it’s ease of use. Node code runs inside Google’s V8 JavaScript Engine which is exactly the same engine that is used for Google Chrome. Because node code runs on this engine, when you write node code you don’t have to worry about browser compatibility issues. Node also gets rid of some restrictions that are given to JS when running on a browser, so with node you have access to things like the file system and hardware.

Node can be used for a lot of tasks like most other programming languages, but one of it’s most common uses is as a server side scripting language. Probably The main benefit of using JavaScript in the server is that if you are a web developer you will be using a syntax that you are already familiar with, so it will be easy to get up to speed.

Read More

Backbone routers

Routers are a common way for web frameworks to translate a URL to an action. On backbone they are used when creating single page applications to refresh the content of the page without actually refreshing the page.

Lets see an example of how they work:

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
var MyAppRouter = Backbone.Router.extend({
  // Define the routes we want to listen to
  routes: {
    '': 'indexAction',
    'main': 'mainAction',
    'listing/:page': 'listingAction',
    'profile/:id(/:username)': 'profileAction',
    'anything/*all': 'anythingAction'
  },

  // Acts as a constructor
  initialize: function() {
    console.log('Router started');
  },

  indexAction: function() {
    console.log('Index action');
  },

  // Now we list our router actions
  mainAction: function() {
    console.log('Main page');
  },

  listingAction: function(page) {
    console.log('Show page ' + page);
  },

  profileAction: function(id, username) {
    console.log('You passed id: ' + id + ' and username: ' + username);
  },

  anythingAction: function(all) {
    console.log(all);
  }
});

// We need to instantiate it
var router = new MyAppRouter();

// This tells backbone to start listening for URL changes
Backbone.history.start();
Read More

Backbone collections

Collections are a convenient way to group models together. An example usage of them is when you want to show a list of elements in your page. You can create a simple collection using Backbone.Collection (I am using the models I defined on Introduction to Backbone.js):

1
2
3
4
5
6
7
8
9
10
// Create some people
var juan = new Person({'weight': 80});
var carlos = new Person({'weight': 70});
var alicia = new Person({'weight': 75});

var persons = new Backbone.Collection([
  juan,
  carlos,
  alicia
]);
Read More