Coder Social home page Coder Social logo

node-wurfl-api's Introduction

wurfl.js

wurfl.js is a node.js library to make it easy to use wurfl. What is wurfl? From the site http://wurfl.sourceforge.net/:

WURFL is a Device Description Repository (DDR), i.e. a software
component which contains the descriptions of thousands of mobile
devices. In its simplest incarnation, WURFL is an XML
configuration file plus a set of programming APIs to access the
data in real-time environments.

Usage

Installation

Install node.js (http://nodejs.org) first, then npm (http://npmjs.org), then:

npm install wurfl

Usage

Basic usage allows getting up and running quickly.

var wurfl = require('wurfl');

wurfl.loadSync();

// wurfl xml file is finished loading into a memory-efficient structure

var userAgent = 'Mozilla/4.0 (compatible; MSIE 4.01; Windows CE; O2 Xda 2s;PPC;240x320; PPC; 240x320)';
var deviceInfo = wurfl.get(userAgent);
console.log(deviceInfo.model_name); // "Xda IIs"

You may want to use your own (read "updated") wurfl.xml file. This is easy:

var wurflFile = __dirname + '/wurfl.xml';
wurfl.loadSync(wurflFile);

Loading and parsing a large xml file can be take awhile and be CPU-heavy. You may use load() to asynchronously load the xml file:

wurfl.load(function() {
	// ready to use, wurfl.get(userAgent);
});

Or with your own xml file:

wurfl.load(myWurflFile, function() {
	// my own file
});

When using your own file, it would be great to update the file without needing to restart the node.js server:

wurfl.watch(myWurflFile);

When updating using watch it will always be asynchronous. The data structure will be updated automatically when the file is changed. You may also be notified when that happens if you want to know when it gets updated:

wurfl.watch(myWurflFile, function() {
	console.log('XML file updated');
});

If you want to reduce memory usage even further, you can specify the the groups you are interested in, the rest will be left out completely. For example, if you only care about the "display" capabilities:

wurfl.loadSync({ groups: [ 'display' ] });

When specifying groups the "file" parameter needs to be an options hash with groups and optionally file. You can use this in loadSync(options), load(options, callback), and watch(options, callback).

var options = { file: __dirname + '/wurfl.xml', groups: [ 'display' ] };
wurfl.loadSync(options);
wurfl.watch(options);

The most common use-case is to load the file up when your script starts synchronously since you need it to exist before you start querying against it, but then add a watch to asynchronously update it later:

var wurfl = require('wurfl');
var wurflFile = __dirname + '/wurfl.xml';

wurfl.loadSync(wurflFile);
wurfl.watch(myWurflFile);

var userAgent = 'Mozilla/4.0 (compatible; MSIE 4.01; Windows CE; O2 Xda 2s;PPC;240x320; PPC; 240x320)';
var deviceInfo = wurfl.get(userAgent);
console.log(deviceInfo.model_name); // "Xda IIs"

That's all for the API:

  • wurfl.loadSync(optFileName)
  • wurfl.load(optFileName, optCallback)
  • wurfl.watch(optFileName, optCallback)
  • wurfl.get(userAgent)

To see what properties are available in the info object you get back from wurfl.get() look at the wurfl.xml file. Each group is an object on info (e.g. info.css is a group) and each capability is a property on the group (e.g. info.css.css_spriting is a capability).

Optimizations

The devices in the wurfl.xml file "inherit" from other devices, specified in the XML by the fall_back attribute. In order to optimize for memory and not duplicate the base properties over and over again for every single device, I've structured the groups to have their own class that extends the fall_back group class. I've set all the capabilites for a group on its prototype object. And for groups that weren't overridden, the device just points to its fall_back device's group. This way, when you access a group's capability it will pull it from the group's prototype object or from further up the prototype chain back to the top-level group instance.

In addition to optimizing for memory I've also added a worker using node-webworkers to load and parse the XML file in a separate process from the main script. This allows the wurfl data to be loaded and/or updated without blocking a large chunk of time from your main script. This can be up to several seconds to load and parse the XML file. The worker is lazily created however, so if you only use loadSync() and get() it won't create the worker.

node-wurfl-api's People

Contributors

jacwright avatar touchads avatar pixeldrew avatar

Stargazers

 avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.