WURFL Client

WURFL Client is a library to do WURFL (wurfl.sourceforge.net/) mobile device detection for web applications. Included are tools to keep the WURFL file up to date automatically and to prepare a customized lookup tables, which allow a fast device detection.

Mode of Operation

The complete WURFL file is as big as ~16MB. A single device detection against it in Ruby took me 12 seconds, which is inaceptable even for the first request of any client.

There are three optimizations to make the detection work faster: 1.) The WURFL file is customized to remove unused capabilities, reducing it’s size considerably. 2.) Client devices are roughly detected by type (e.g. iPhone like, Nokia devices, etc.) 3.) The WURFL Client works with previously prepared lookup tables for the roughly detected device types in a Ruby PStore data structure, so Ruby doesn’t have to parse XML files on each request.

The lookup tables are prepared during the WURFL update task, which takes quite some time. But this allows for a very fast and for our appliances also accurate recognition.

The WURFL Client can then be implemented f.i. as a thin Rack service, see Examples.

Requirements

Ruby Version supporting String.ord -> Possibly integrate gist.github.com/251465 as a fix WURFL gem (github.com/pwim/wurfl): Handset, UserAgentMatcher, WURFL Loader

Installation

sudo gem install wurfl_client

Setup

To setup the environment, you have to execute following steps: 1.) download and extract the latest WURFL file to wurfl-latest.xml

curl -L -o wurfl-latest.xml.gz 
gunzip wurfl-latest.xml.gz

2.) configure wurfl_minimize.rb by writing a wurfl_minimize.yml like so:

input_file: wurfl-latest.xml output_file: wurfl-custom.xml capabilities: [brand_name, model_name, max_image_width, max_image_height, wta_voice_call]

NOTE: You can adapt wurfl_minimize.yml capabilities to your needs. See wurfl.sourceforge.net/help_doc.php for a list of available capabilities.

3.) minimize the WURFL XML file.

wurfl_minimize.rb

4.) prepare the lookup tables (builds them into ./lookup directory by default). This will take some time!

mkdir lookup

wurfl_prepare_lookup.rb

Now, you are ready for device detection. If you make an automated script to do this update regularly, you must not forget to clear the lookup directory first. The preparation script doesn’t overwrite files. For minimal effect on your running webservice, you should use another lookup directory there and copy the new data after the update is complete.

Here is an example automatic update script:

#!/bin/sh
curl -L -o wurfl-latest.xml.gz http://sourceforge.net/projects/wurfl/files/WURFL/latest/wurfl-latest.xml.gz/download
gunzip wurfl-latest.xml.gz
wurfl_minimize.rb 
rm lookup/*
wurfl_prepare_lookup.rb
cp lookup/* /path/to/production/lookup

Examples

The device detection is as simple as this call:

require "wurfl_client"

user_agent = 'Mozilla/5.0 (iPhone; U; CPU iPhone like Mac OS X; en-us)'
device = WurflClient::detectMobileDevice(user_agent)

puts "DEVICE: #{hs["brand_name"]} #{hs["model_name"]}"

You can easily put the WURFL detection into a Rack (rack.rubyforge.org/) middleware. I chose to output the data in JSON format. Using following ‘config.ru’, you can put this online using ‘rackup’.

require "wurfl_client"

class RackInterface

  def getJSON(handset)
    result = %Q{"WURFL-Client":1}
    if handset
      handset.keys.each do |key|
        result << %Q{,"#{key}":"#{handset[key]}"}
      end
    end
    "{#{result}}"
  end

  def call(env)
    user_agent = env["HTTP_USER_AGENT"]

    hs = WurflClient.detectMobileDevice(user_agent)

    [200, {"Content-Type" => "text/javascript"}, ["#{getJSON(hs)}"] ]
  end

end

run RackInterface.new

Test by opening f.i. localhost:9292/. You should see the response

{"WURFL-Client":1}

You can get results for mobile devices by using the UserAgentSwitcher Firefox extension (chrispederick.com/work/user-agent-switcher/). This can be further extended by a caching mechanism to reduce the server load. Being a Rack layer, it should also be possible to integrate this into a rails app. If you know, how to do so, I would really appreciate your contribution.

Author

  • Guido Pinkas (Original Author)

Thanks

Thanks to S. Kamerman from TERA-WURFL (www.tera-wurfl.com/) for his WURFL Customizer (www.tera-wurfl.com/wiki/index.php/WURFL_Customizer). I only had to port the functionality to ruby.

Contribution

Contribution is welcome. Please use the github tools (fork, pull request, etc) here on github.com/bluecat76/wurfl_client.

License

The MIT License

Copyright © 2010 Binder Trittenwein Kommunikation GmbH (www.bindertrittenwein.com/)

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.