Sending text messages with Ruby

After reading the hacker-scripts story I got inspired to make my own automated set of scripts. One challenge was finding text messaging APIs that work in my area. Twillio wasn't an option as this doesn't support my current area. 

Enter textlocal I have no idea how they do it, but they support my area and probably any area in the world. Sure its not free but it works and you get some free credit when you first signup. Their API is well documented providing examples on how to send messages with a variety of programming languages.

Here is how you might send a text to someone or a group of people using Ruby

require "rubygems"
require "net/https"
require "uri"
require "json"

requested_url = ''

uri = URI.parse(requested_url)
http = Net::HTTP.start(, uri.port)
request =

res = Net::HTTP.post_form(uri, 'username' => [email protected]', 'hash' => 'your-secret-hash', 'message' => 'example message', 'sender' => 'your-name', 'numbers' => 'phone-number-here')
response = JSON.parse(res.body)
puts (response)


Automatic backups using Ruby and Cron

I recently read an article about a guy who lost all of his data after one of the servers his VPS was hosted on had a RAID card failure, the host was unable to provide a backup since that would interfere with their privacy policy. Luckily the guy had a back up and was up and running again within 30 minutes.

This got me to thinking. What would happen if someone were to steal my laptop today? What would happen if my house caught fire and I lost all my data. As much as a geek as I may be, sadly I don't backup my local computer very often at all. My servers get backed up weekly thanks to Digital Ocean's built in backup system. But today I sat down for half an hour and decided its time to change that.

And so 30 minutes later, the script was complete. Primitive, but it works, and works well. Essentially what happens is: The script backs up all my databases, backs up all the files and folders inside a predefined directory, then puts all of that into a nice little .zip file, and sends that to my backup server via rsync.

I then setup a cron job on my laptop to run the script every week around 11 PM when I know the laptop is on and has internet access. Something else I'm thinking of adding to the script is an email notification when a backup was completed successfully (or not).

The script can be found here. You can do with it as you please. 

How to send base64 data to REST API

If you're working with PhoneGap, Ionic or building any kind of application where you need to send image rendered on a  HTML5 canvas image over to a REST API then this is one way to do it.

A few things you're going to need

  1. Access to the API code and server
  2. Your base64 string

Sending the image with jQuery isn't going to work, jQuery just can't handle that amount of data. So we're going to be using XMLHttpRequest instead. 

The first thing we're going to have to do is make sure our post_max_size is set to a size thats comfortable for our use, this all depends on how large the images you're going to be sending are in size. You can modify this line in your php.ini file if you're using apache. 

Next thing is our post request we're going to send to our API, below is an example of how such a post request might look like.

var data  = '/9j/4AAQSkZJRgABAQAAAQABAAD//gA7Q1JFQ...';

xhttp = new XMLHttpRequest();
  xhttp.onreadystatechange = function() {
    if (xhttp.readyState == 4 && xhttp.status == 200) {
      document.getElementById("result").innerHTML = xhttp.responseText;
  };"POST", "" + data, true);
xhttp.setRequestHeader("Content-type", "JSON");

Once you make your api call and try to turn the data back into a file after decoding the base64 string you might find that the image is corrupted. This is because when we send the string over http the slashes get replaced with spaces. To fix it all we have to do is replace the spaces back with slashes and we should be good to go.

$base64 = $_POST['data'];
$base64 = str_replace(' ', '/');
$image = base64_decode($data);

Remember to always use a POST request for this method and all should be fine.

How to bootstrap your php application

As your applications get larger, and code becomes harder to maintain, it may be time to implement proper standards and folder structure. Essentially what Bootstrapping does is load the environment our php scripts need to run. In the case of php it also means implementing a front controller. There are 3 key problems bootstrapping solves for us:.

  1. All request go through the front controller
  2. Everything except the front controller is placed outside of the publicly accessible folder.
  3. Allows us to implement routing 

Organization is key

Everything starts with a sane directory structure for the application. Here's an example


The important part to note here is that is that the application itself is self-contained in the app directory. Inside there we get the app/public directory, which is the only one we publish to the web by making this the DocumentRoot in our virtual host file.

The .htaccess file redirects all URLs to the front controller index.php

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [QSA,L]

The app/lib directory contains application specific code, and the vendor directory contains third-party code such as ORM, templating engines, logging and so on.

The share directory contains data like log files, configuration files, and other stuff that is not strictly part of the application. Finally the bootstrap.php file is where the environment gets loaded. Wer could do it inside index.php but since that's also our router and it's publicly visible its a good idea to separate those concerns.

Loading components

This is where we load all of our sweet and tasty third-party components. Since we plan on using composer, we can just include composer's autoload.php here. Initialize composer if you haven't already done so. This way we can have our composer.json and autoload.php file created for us.

composer init

Once that's done you can add this line to the top of your bootstrap.php file.

require_once dirname(__FILE__) . '/../vendor/autoload.php';

Loading the environment and Routing

Inside bootstrap.php is where you're going to define your default configs and other environment necessities. Besides loading them just in the bootstrap.php file you can also create a share/config/default.php file and include that in the bootstrap.php file.

$configFile = dirname(__FILE__) . '/../share/config/default.php';
if (is_readable($configFile)) {
    require_once $configFile;

Depending on whether you've required a router in your composer.json file or not, you can write all your routes or endpoints in the index.php file.

So that's it, now we have a basic modern application template.

© 2016 Timothy de Jongh Back to Top