Last year at lastminute.com labs we started developing mobile web services in Rails. The first service we launched is called fonefood. It is a restaurant booking site you can use from your phone. We’ve got restaurants across lots of countries: Austria, Belgium, Denmark, Finland, France, Germany, Ireland, Netherlands, Spain, Sweden, Switzerland and United Kingdom. You can read more about it at: http://labs.lastminute.com/fonefood/
Developing a booking service across Europe does have its fair number of delicate issues: In which country is the user currently? Which language? Which device? To answer some of these questions we have been heavily using DeviceAtlas, to add and remove tweaks for many different handsets based on patterns in our analytics of users and their browsers. As the code base increased we re-factored our Rails DeviceAtlas code into a small plugin. We have recently released the plugin to the public and written this small tutorial to get your Rails code up and running and device aware.
This plugin is a Rails wrapper around the DeviceAtlas Ruby library. It allows you to integrate the DeviceAtlas database into your action controllers. DeviceAtlas is a database of mobile handsets properties. For each view request you can check properties such as display widths and heights, maximum image dimensions etc. and scale content appropriately. You can find the plugin on the following url: http://github.com/lastminutelabs/Device-Aware
To begin with we assume you have Ruby installed and the following gems installed: rails(2.3.x+), json(1.1.6+), rubyzip(0.9.1+). We also assume you have prior basic knowledge of creating and building a basic Rails web site. If you are interested in learning about Rails, we recommend that you read the introduction tutorial on rubyonrails.org.
To begin, enter your Rails application folder and install the plugin 'script/plugin install git://github.com/lastminutelabs/Device-Aware.git'
After the plugin is installed make sure that the lib/device_atlas
folder exists in your Rails app. We are storing the DeviceAtlas database there and it should have been created by the installer. Next edit the Rake task for the plugin,
, enter your DeviceAtlas license key into the variable DEVICE_ATLAS_LICENSE
. (Note that you can get a free developer license at http://deviceatlas.com)
Run the Rake task device_atlas_update
, this task will fetch and unpack the latest data from deviceatlas.com, according to your license key.
Inside either your ApplicationController
or a single specific controller include the ActASDeviceAware
module like this:
1 2 3 4 5 6 |
class WelcomeController < ApplicationController include ActsAsDeviceAware def welcome end end |
Inside all your views you will now have access to a hash of device properties called @device_atlas_data
which contains all the properties for the current request. The properties inside the hash are matching the names as they are listed on the device atlas web page. Below is an example of displaying the DeviceAtlas properties for a web request:
1 2 3 4 5 6 |
<h2>Device Atlas Properties</h2> <dl> <% @device_atlas_data.each_pair do |key, value| %> <dt><%=h key %></dt><dd> <%=h value %></dd> <% end %> </dl> |
We recommend that you create a custom device action in your test environment that dumps the DeviceAtlas hash to a nicely formatted table. It’s very useful for determining if your device is detected correctly, to determine where a reported bug is originating.
We encourage all feedback, or help to make this rather basic plugin more useful. Please visit us on http://labs.lastminute.com for an update on what we are working on at the moment!
Leave a Reply