Page 1 of 1

strongly typed capabilities?

Posted: Tue Jun 28, 2011 10:44 am
by sm_support2
I see that all capabilities are represented as strings. In some cases, for example is_tablet, I would really love if I could have it returned as a Boolean. Suggestions?

Re: strongly typed capabilities?

Posted: Tue Jun 28, 2011 10:45 am
by sm_support2
In the ASP.NET MVC example (download from here: http://wurfl.sourceforge.net/dotNet/), you can find some code that helps you out.
In general, WURFL capabilities are represented as strings because that’s the most general way of dealing with this kind of data. As you can see, there are capabilities which are perfect true/false values, others which are plain numeric values, and others whose representation with a strong type would probably create consistency problems.

You can create a bunch of extension methods for the String type and use them to quickly attempt to convert to a more specific type. For example, suppose you have a method like AsBoolean:

public static Boolean AsBoolean(this String theString)
{
var theBooleanValue = false;
var lower = theString.ToLower();
var isBooleanText = (lower == "false") || (lower == "true");
if (isBooleanText)
Boolean.TryParse(theString, out theBooleanValue);
return theBooleanValue;
}

When you read the value of is_tablet you can quickly and fluently get an easy-to-handle Boolean value as below:

var device = wurflManager.GetDeviceForRequest(userAgent);
var caps = device.GetCapabilities();
var is_tablet = caps["is_tablet"].Value.AsBoolean();

The same can done for integers.

Re: strongly typed capabilities?

Posted: Thu Jul 07, 2011 9:01 am
by kamermans
It should be noted that in the Database API (aka Tera-WURFL) and WURFL Cloud, numeric and boolean values are strongly typed, so is_wireless_device === true.