strongly typed capabilities?

sm_support2
Posts: 294
Joined: Mon Jun 20, 2011 5:04 pm

strongly typed capabilities?

Postby sm_support2 » Tue Jun 28, 2011 10:44 am

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?

sm_support2
Posts: 294
Joined: Mon Jun 20, 2011 5:04 pm

Re: strongly typed capabilities?

Postby sm_support2 » Tue Jun 28, 2011 10:45 am

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.

kamermans
Posts: 393
Joined: Mon Jun 06, 2011 9:50 am

Re: strongly typed capabilities?

Postby kamermans » Thu Jul 07, 2011 9:01 am

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.
Thanks,

Steve Kamerman
ScientiaMobile

Make sure you check out our WURFL Cloud, WURFL InSight and WURFL InFuze products!


Who is online

Users browsing this forum: No registered users and 49 guests