Using Angular’s html5Mode with Harp

Angular is great framework for creating client side web applications as you may already know. One of the greatest benefits is that you don’t need any server processing nor compilation of your code since everything is happening in web browser. Because of that something like Python’s SimpleHTTPServer is sufficient tool for development on localhost.

This is cool but just until you don’t want to write your awesome applications in some prettier way – using JADE instead of bare HTML, SCSS or LESS instead of CSS and CoffeeScript instead of raw JavaScript. Fortunately there is harp. And no, it’s not that musical instrument. Harp is simple static web server with build-in, realtime preprocessing for all these fancy compile-to languages. If you don’t know about it check their web. Let me show You what caveats you may face developing Angular app with Harp.

html5Mode

When html5Mode for nice URLs during navigation in your Angular application, you effectively shadowing real file structure of your application. This is problem with Harp because whenever you try to reload or open URL known to your Angular app, Harp server will look up for the file and shows error 404 (except the file actually exists):

Browser Harp
Open / Serves /index.html
Loads JS and templates Serves your assets
Navigate to /todos Nothing
Reload -> Browser send request for /todos Look up for /todos.html -> Not Found 404

 

Unfortunately Harp doesn’t provide any configuration options to add any fallback or mapping of URLs to files. One solution that I found is to use Harp as module with Express framework.

First of all make sure that you have installed express:

Then create server.js file inside your Harp application directory and put there code below. I’m using Harp in “framework style” so my application is under /public dir.

To start server, run

 

Introducing Time Heroic – Your heroic time tracker

Recently, I had problem to remember what the hell I was doing all the time. So I decided that I need somehow track my time. I searched few of existing tools, both on-line and desktop, like Harvest (www.getharvest.com) or Hamster for Gnome (since I use Linux desktop). But they feel confusing and complicated to me.

Beside it, we are struggling with underestimations of needed time for our projects in company where I work. But we had no data to analyse why and actually how much we underestimates it. So I come up with idea to make tool that lets me set what I am going to do now and what is my estimation of how long it will take me to done that task.

By that I would track how much time I spend on what, plus I would get more info about how my estimations are wrong.

Public release!

So I’m glad to present You a public release of this tool I called Time Heroic. It’s an alpha version, so it is meant to proof concept of the tool:

http://timeheroic.gdmt.cz

I am looking forward for Your opinions!

Ruby on Rails – Internal redirect and render of action from another controller

You probably need to render action from other controller than the one currently processing user’s request and you don’t want to redirect user’s browser. Just in case, make sure you know why you want it :)

I need it because I want to render my 404 and 500 error pages (which I handle throught routes) but I don’t want to raise exception that will be handled so far in ShowExceptions middleware which makes notification in Airbrake which has middleware just bellow it.

So my solution is to create simple middleware placed just above my application which will play request/response ping pong with it as long as the application want to redirect somewhere else. To make this condition I used headers list returned from application to middleware on top of it. When application return respond with X-Internal-Redirect-To header, my middleware throws the request back to the application just with changed PATH_INFO environment variable.

Code

Here is implementation of middleware class. Save it somewhere to your lib folder, eg lib/internal_redirect.rb

Then add it to middleware stack of your application. To do that change your config/application.rb file:

Add require for create class file at the top of application.rb file:

And add ocnfiguration directive

After that, try run “rake middleware” command. “use InternalRedirect” should show up just above the application.

Use it

To use internal redirect, just use “head” method of Action Controller in your actions:

That’s it. Enjoy :)

Personal Timetabling – Bachelor’s thesis

Abstract

Despite all the advancements in Artificial Intelligence, we still do not have a broadly available application for automated scheduling of personal activities. The main difficulty in creating such an application is satisfying user’s diverse expectations about time organization. In this study we focused on creating a tool that can help users with organizing their time. We designed a model for describing personal activities with user preferences. We formulated scheduling of personal activities as an optimization problem for which we designed and implemented a solving algorithm, aiming to schedule activities with precision of seconds.

We created a prototype of web-calendar application powered by this model and an algorithm which we designed with the focus on ability to clearly display user’s time and easily insert activities for automated scheduling. The web application is backed by RESTful API which enables implementing application on various platforms.

Preview

Personal Timetabling - adding new request Personal Timetabling - solving Personal Timetabling - example calendar

About

How it works

Instead of placing tasks to fixed time as you can do in classical calendar, here you have to in what time window you want to do that task. This information is then available for all tasks in calendar and so when you want to add new one, it was searched how other tasks can be reorganized in order to make time for the new one. During search it is trying to maintain time of already scheduled tasks.

It uses variation of local search techniques used in artificial intelligence field.

Technologies

I wanted to use the right technology for every part of the software. So user interface is done purelt in JavaScript with use of Backbone.js library. Server interface was build in Ruby on Rails and core algorithms was written in Java. Connection of these technolgies to work together was quite a challenging but I managed to make it.

Download

Personal Timetabling thesis (PDF, in czech)

Project on GitHub

Cropping image to aspect ratio with ImageMagick in Rails

I wanted to crop image in paperclip to some aspec ratio so that cropped image will use full width or height of original image.

A little bit math

It is simple to see that if croping is “wider” than original image, it must crop top and bottom of image and use full width. Otherwise, if cropping is “taller” than original image, it crops left and right while using full image height. This can be luckilly computed simply like this:
Let r_c is ratio of width:height of desired cropped image and r_i is ratio of original image.
If r_c > r_i (wider) then cropped image should have width = original_width and height = original_width / r_c
Otherwise, if r_c <= r_c (taller) then it should crop to width = original_height * r_c and height = original_height.
Problem is that it needs to know aspect ratio of original image. Retrieving that information somehow before hand is quite complicated and ineffective moreover since ImageMagick always know it. Can’t it do that computation by its own? ImageMagick never stops surprising me, yes, it can do that. Let’s look on it:

Convert options

It’s Ruby syntax, but it should be understandable :)

Example

Original image:
Chucky-Childs-Play
With ratio = 2 (width = 2* height):
chucky_crop2
With ratio = 0.5 (height = 2* width):
chucky_crop05