07 December 2016

08 October 2016

Pusher with Redux

You might have heard about a wonderful service called Pusher. It is a service which allows you to make live updates without handling infrastructure on your own.

When I wanted to add it into my React - Redux application I was surprised to find out that there are no libraries which help you. So I decided to write my own. I called it pusher-redux.

I think it turned out to have a very convenient API you can call anywhere from your Redux application. Let me explain how to use it below.

First you need to configure Pusher in your app.js:

Then you need to subscribe to it in your component:

This is how you handle it in your reducer

Pusher-redux dispatches actions of the following format, you can find data you send in action.data:

Smart page titles in Ruby on Rails

Having separate titles for each page in Rails application is a very common requirement and yet RoR does not provide any built in ways to deal with it. I am going to present you with some handy code that can solve this problem without use of gems.

There are going to be 3 ways to define a page title:

  • For tho whole controller.
  • For a specific action inside a controller.
  • Implicitly from a controller name.
Last is gonna be especially useful, you can just drop the following code into your application and have reasonably good titles.

So lets add code to our parent ApplicationController:

And reference page title in our layout:

After that setting pages titles is extremely easy. You don't even have to do it if you are naming your controllers properly.

30 September 2016

DNS over HTTPS

In the land of Russia where your freedom is becoming ever more limited goverment is trying to censor the Internet. Those who are not content with website bans have to find ways how to get around this problem. One way to get around the block is to use Google's DNS instead of the DNS provided by your ISP. After reading Hacker News today I stumbled on an intersting arcticle. The gist is that Google provides an encrypted way to access DNS (unlike traditional UDP which can be easily monitored by your ISP).

Unfortunately there's no way to just point your router or computer to it because DNS has a protocol and since Google secure DNS doesn't conform to it existing programs can't access it directly. So in this article we are going to set up our own DNS server, and yes, it can have blackjack and promiscuous women.

Some kind soul has written a Go package to interact with this secure DNS server. It is located here: https://github.com/wrouesnel/dns-over-https-proxy . We are going to need Go language in order to run that package. Details of how to install and run Go are outside of this article but you can refer to this page https://golang.org/doc/install .

After package was downloaded you need to execute is using the following command (we add debug there to see output):

 
sudo ./bin/dns-over-https-proxy -debug=true -address=127.0.0.1:53 -log.level=debug
 

In order to test if it's working we need to go into OS network settings and add DNS servers there. We can't do it via router because ISP often adds their own DNS and not all routers have options to ignore it.

There's one more issue. The go script which is running DNS uses a domain name itself. Go to your /etc/hosts file and add the following line:

 
8.8.4.4 dns.google.com
 

We can check if it's working or not by executing the following command

 
dig -p 53 @127.0.0.1 google.com
 

Now unfortunately some resources are not just banned by DNS but also by IP. For those you will have to use a proxy in addition to DNS. I highly recommend FoxyProxy browser plugin. Also make sure to connect via HTTPS to these websites otherwise your ISP will be able to inspect the information you are sending to your proxy and will still be able to stop your requests.

P.s. original article by Google: https://developers.google.com/speed/public-dns/docs/dns-over-https

07 January 2016

Easy error handling in Rails

Let me start by asking what do you think about errors? Many people hate errors and are trying to avoid them as much as possible.

But today I want to show how errors can make your life easier!

Let's say user submitted some form in your application. Where do you check if it's valid? In the controller? Controller's should not be concerned with all that is going on in our models. Should the controller ask a model if data is valid or not? Model should be already checking incoming data.What is you introduce some change deep inside your code, you can't expect to know all the place where it's used!

In Java World (and in others) errors are not actually called "errors", they are called "exceptions" and they can help you to handle exceptional situations. Ruby language has similar exception system. Errors can be raised and rescued.

Let's distinguish between 2 types or errors. 1) Ones that we can actually anticipate and fix. 2) All those bug induced errors, memory errors etc.

First type of error can be fixed by users. Second usually can only be fixed by programmers.

So where should we handle exceptions? Many agree that the most suitable is at system boundaries. We are going to handle it in UI.

First of all, let's define our new class that can be handled.

Second, let's think how we would like our code to look.

Now let's place this method into application controller.

And make it handle optional redirect path.

Let's see how javascript code may look like (jQuery example)