Pragmatism in the real world

Getting started writing an Alexa Skill

We now have 4 Amazon Echo devices in the house, and, inspired by a demo LornaJane gave me at DPC, I have decided to write some skills for it. This article covers what I learnt in order to get my first Swift skill working. Our bins are collected by the council every other week; one week it's the green recycling bin and the other week, it's the black waste bin. Rather than looking it up,… continue reading.

Simple way to add a filter to Zend-InputFilter

Using Zend-InputFilter is remarkably easy to use: use Zend\InputFilter\Factory as InputFilterFactory; // set up InputFilter $specification = [ 'my_field' => [ 'required' => false, 'filters' => [ ['name' => 'StringTrim'], ], ], ]; $factory = new InputFilterFactory(); $inputFilter = $factory->createInputFilter($specification); // use InputFilter on some data $data['my_field] = 'Some string'; $inputFilter->setData($data); if ($inputFilter->isValid()) { Return false; } return $inputFilter->getValues(); // my_field is now trimmed How do you add your filter to it though? This is… continue reading.

Default route arguments in Slim

A friend of mine recently asked how to do default route arguments and route specific configuration in Slim, so I thought I'd write up how to do it. Consider a simple Hello route: $app->get("/hello[/{name}]", function ($request, $response, $args) { $name = $request->getAttribute('name'); return $response->write("Hello $name"); }) This will display "Hello " for the URL /hello and "Hello Rob" for the URL /hello/Rob. If we wanted a default of "World", we can set an argument on… continue reading.

Slim's route cache file

When you have a lot of routes, that have parameters, consider using the router's cache file to speed up performance. To do this, you set the routerCacheFile setting to a valid file name. The next time the app is run, then the file is created which contains an associative array with data that means that the router doesn't need to recompile the regular expressions that it uses. For example: $config = [ 'settings' => [… continue reading.

Inserting binary data into SQL Server with ZF1 & PHP 7

If you want to insert binary data into SQL Server in Zend Framework 1 then you probably used the trick of setting an array as the parameter's value with the info required by the sqlsrv driver as noted in Some notes on SQL Server blobs with sqlsrv. Essentially you do this; $data['filename'] = 'test.gif'; $data["file_contents"] = array( $binaryData, SQLSRV_PARAM_IN, SQLSRV_PHPTYPE_STREAM(SQLSRV_ENC_BINARY), SQLSRV_SQLTYPE_VARBINARY('max') ); $db->insert($data); Where $db is an instance of Zend_Db_Adapter_Sqlsrv. If you use SQL Server… continue reading.

Autocomplete Composer script names on the command line

As I add more and more of my own script targets to my composer.json files, I find that it would be helpful to have tab autocomplete in bash. I asked on Twitter and didn't get an immediate solution and as I had already done something similar for Phing, I rolled up my sleeves and wrote my own. Start by creating a new bash completion file called composer in the bash_completion.d directory. This file needs executable… continue reading.

Switching OpenWhisk environments

When developing with OpenWhisk, it's useful to use separate environments for working locally or on the cloud for development, staging and production of your application. In OpenWhisk terms, this means setting the host and the API key for your wsk command line application. $ wsk property set –apihost $host –auth $key (Of course, for live and staging, ideally, you will be using a build server!) For a Vagrant install of OpenWhisk, the host is 192.168.33.13… continue reading.

POSTing data using KituraNet

I had a need to send a POST request with a JSON body from Swift and as I had KituraNet and SwiftyJSON already around, it proved to reasonably easy. To send a POST request using KituraNet, I wrote this code: let url = "http://httpbin.org/post". // Change to the actual URL let dataToSend = ["foo": ["bar": "baz"]] // Change to the actual data // body needs to be of type Data, use SwiftyJSON to convert let… continue reading.

Using CircleCI for a PHP project

For a new client project, I've decided to use CircleCI to run my tests every time I push to GitHub. This turned out to be quite easy; this is how I did it. I started by creating a config file, .circleci/config.yml containing the following: version: 2 jobs: build: working_directory: /var/www/html docker: – image: php:7.1-apache environment: APP_ENV: test steps: – run: name: Install system packages command: apt-get update && apt-get -y install git – run: name:… continue reading.

Automatically converting PDF to Keynote

I use rst2pdf to create presentations which provides me with a PDF file. When it comes to presenting on stage, on Linux there are tools such as pdfpc and on Mac there's Keynote. Keynote doesn't read PDF files by default, so we have to convert them and the tool I use for this is Melissa O'Neill's PDF to Keynote. This is a GUI tool, so I manually create the Keynote file when I need it… continue reading.