Concurrency in Objective-C using Grand Central Dispatch (GCD)

by Abu Lewis

To demonstrate execution of concurrent Objective-C code, I wrote this simple example below. The two functions you have to read about to understand the code are; dispatch_get_global_queue and dispatch_async.

Example of output you may get, may look like this. And notice that the output may vary. To understand what is happeneing, run the code several times.

TDD and JavaScript

by Abu Lewis

I felt that I had to do this for my own good.:) To do TDD, the easiest way is to have nodeJS installed and up and running.

What I shortly did is to create my project skeleton

Then, you have to work in that folder

Create now two folders lib & test

Bring the package.json to your project by:

To do the testing, you will need mocha & chai

Now when everything is setup, lets start coding.
Suppose now that We have to implement the class Person, in which the method getFullName() will return concatenated firstName & lastName passed to the class constructor.

Create a test file and place it in the test folder. Also, create the person.js and place it in the lib folder.

Lets do the TDD now. The person.test.js may look like this for instance!?

To run the test:

The tests will fail for sure. Code your person.js in the lib folder:

Now, you have your first passed test case when running:

Now you are good to go with the rest. Read the documentation about chai & mocha and write your other test cases!

Writing a wrapper WebSocket client class in JS

by Abu Lewis

I released this custom WebSocketChatServer gem earlier this month and thought that it may be a good idea to wrap the server’s client in a JS class just to make it easier to communicate with the server.

Here is the JS client:

And the example of usage:

Getting started with PostgreSQL’s PL/PGSQL

by Abu Lewis

Relational database management system (RDBMS) are more than creating tables and execute SQL-queries against them. PostgreSQL is one of my favourite RDBMS and if you happened to use it in the back end, I suggest that you should look at this feature called PL/PGSQL. As wikipedia says, it is a fully featured programming language, allowing more procedural control than SQL.

And in this blog entry, I’m going to demonstrate a simple example on how the PL/PGSQL can be used. The demonstration comprises an authentication logic executing on the server side within the PostgreSQL server.

So, imagine that with have a table holding the user credentials as in the example below:

And we should agree that the email has to be indexed uniquely:

What we are doing next is to move the authentication logic to the PostgreSQL server. We simply are going to create a login(email, password) function that is both stored and executed within the PostgreSQL. This function works in a way, in which the passed parameters (email, password) are validated first. If the parameters does not pass the validation, the function will throw an Exception. The next step in the function is to query from the table for the user records based on the passed parameters.

Before listing the login function, we create two functions taking care of the validation of the email and password. These functions are called inside the login() function.

Now that we have validation functions, lets do the login:

To test the method, insert dummy data to the users table:

Test now the login() function by:

Encrypt text in iPhone & decrypt it in your Rails server

by Abu Lewis

What I did to perform the task is that i used RNCryptor in my IOS application.

And I engaged this gem to do the decryption in Rails.

Parsing an URL with Ruby

by Abu Lewis

Can not be easier than this…

Prevent CSRF-attacks in your PHP files using tokens

If you do not know how a CSRF-attack work, read here

This is a simple class for taking care of generating and validating the tokens.

Let’s say that you have login.php in which a form with two text fields and a submission button are included. What you want to do here is to generate an access token and apply it to the login form as a hidden value.

To validate the token, apply the code below in the file where the login form has been posted to.

Filter pattern in Rails

by Abu Lewis

Imagine you have an User < ActiveRecord::Base object and you want to apply filtering implementation to it. The filtering will be based on certain criteria like get me all the adult users or all the users that are females or males. That kan be achieved in different ways. One way is using Rails's scoping or by defining class methods. If you like the idea of the design patterns, it turned out that there is a design pattern for this problem and it is called the Filter design pattern. And to demonstrate, We will create an interface class to outline the behaviour of our Filtering criteria. This interface can then be extending with concrete criteria classes containing concrete implementation. This is the domain class I skissed to describe it better: cd

And the code:

By doing so, the User class will look cleaner for me…

To play around with the newly created filters in rails console:

Thank you!

Observer pattern exemplified in Ruby

by Abu Lewis

The observer design pattern is best demonstrated using the broadcaster <-> listener example.

A broadcaster in this example is an object having implementation for attaching listeners to itself. When the broadcaster will broadcast a new message, all the attached listeners are notified.

A listener class will register the broadcaster to itself so that it can access the message that has been broadcasted by the broadcaster. It will also have a method, which will be called by the broadcaster when notifying.

Let’s play around…