How requirements shaped my code, AKA Rails 5 and ActiveRecord before_destroy callbacks

I recently started a new project with Rails 5, and at some point I found out the code was not behaving as expected.
The initial scenario was quite simple, Admins can have many Phones, as this code suggests:

Continue reading “How requirements shaped my code, AKA Rails 5 and ActiveRecord before_destroy callbacks”

Wrap and iterate with the power of yield

Iterating over a collection isn’t rocket science. Obviously if you need to iterate over a really big collection you may need to adjust your strategy but it is something that in the vast majority of programming languages you get quite for free.

In Ruby for example you can just send the each message to your collection together with a block of code. The block will be executed for each element of the collection itself.

But what if you need “something more” from the objects you’re iterating on? If, for example, each element of your initial collection is an array and you want to abstract away how you access each of its elements? What options do you have?

Continue reading “Wrap and iterate with the power of yield”

Swift: How to send SOAP requests with AlamoFire 4.0

We do love our JSON APIs, but sometimes we need to work and talk with the Dark Side and perform SOAP requests exchanging XML responses with a server far far away.

In our Objective-C projects we grew fond of AFNetworking, but having switched to Swift (and Swift 3 nonetheless) we had to rely on the new Alamofire 4.0.

Why new if it is a 4.0 version?

Alamofire 4.0 brings a big rewrite and some breaking changes, needed to address the new Swift 3 syntax and to ease some things up. The breaking changes involve how to make a request and how to encode our parameters, key aspects if you want to talk with a SOAP server. The changes have been introduced only recently and it seems few people had the same problem of having POST requests on a legacy SOAP server, so the literature on the web is quite scarse and this post wants to fill this gap.

As with everything, the solution when found, is quite simple.

At line #6 we set the headers up, the SOAPAction one is quite important, since without it nothing worked. As always, YMMV, but you should find the correct value inside your WSDL of reference.

At line #16 we setup the request, the encoding parameter is the key of this post: to encode the request we write an extension to String:

In this extension we specify the format for a correct header as requested by our SOAP server and serve it during every request.

And we are ready to go.

Then, if you need to parse the XML response (otherwise why talk with the server to begin with?), we suggest you to use the XMLHash library of SwiftyJSON inspiration.

We hope this little bit of information will save you some research time.

Elastic IP Address (EIP) and ECS (EC2 Container Service) cluster, a naive solution

Recently I had the opportunity to set up another ECS cluster for a Ruby on Rails application that exposes a few API endpoints and a backend to manage some contents, i.e. images, videos and so on.

Considering our previous experience we decided to automate the provisioning of the infrastructure by using Ansible and after a bit we ended up with a few playbooks that allowed us to bring up everything we needed, from the DB to the instances, ELB, task definitions and services.

Everything was working quite well until we were asked to provide a static IP that could be used to access the aforementioned API endpoints.

Continue reading “Elastic IP Address (EIP) and ECS (EC2 Container Service) cluster, a naive solution”

How to create and customize a slider

A few days ago I had to work as a frontend developer because I needed to create some html static pages for mobile devices. One requirement was to have a scrollable component that mimics a calendar interface.

Building a slider from scratch takes a long time for sure, so I decided to use Swiper, an open-source slider compatible with most browsers and platforms.

I chose Swiper among many other candidates because it can be easily installed via Bower, which is currently one of the most popular package manager tools. Continue reading “How to create and customize a slider”