Using 51Degrees device data with Scala and Maven
Scala is becoming increasingly popular as it allows concise code with a strong type system. It can also access all Java libraries as it runs in the the Java Virtual Machine (JVM). In this blog we'll use the 51Degrees Maven Java library to perform device detection using Scala code. The blog assumes the reader is familiar with Maven, Scala and the Eclipse IDE.
Checking Everything's Working with
I've been using the Scala IDE for Eclipse. This IDE allows us to easily get just about any Java library from Maven. See Scala Maven guide to see how to create a project with Maven for Scala.
You should see some code in the new project that looks like this:
package com.degrees.scalademo
/**
* Hello world!
*
*/
object HelloApp extends App {
println( "Hello World!" )
}
Note that some templates use old Scala (pre 2.11) that won't compile with the latest Scala distributions. Make sure the code inherits from App, not Application. You may need to rename the App super class if it was called Application before.
You should be able to run this program and see 'Hello world!' returned in the console. We'll change this so that the results of a device detection are output using the 51Degrees Maven pacakage.
Adding 51Degrees from Maven
Right click on the project (not the source files), and go to Maven -> Add Dependency.
You can search for 51Degrees or add the ids yourself. The Group Id for the 51Degrees package is com.51degrees. The Artifact Id is 51Degrees.detection.core.
Device Detection Code
Start by creating a standard device detection provider which references 51Degrees device data. Device data can be stored as a single file on disks, loaded into memory, or in the default implementation is embedded into the 51Degrees package. The choice of data source is made when the data set passed to the provided is created. The followed example code initialises the provider using the embedded device data.
val embeddedProvider = new Provider()
val deviceMatch = embeddedProvider.`match`("Mozilla/5.0 (iPhone; CPU iPhone OS 5_0 like Mac OS X) AppleWebKit/534.46 (KHTML, like Gecko) Version/5.1 Mobile/9A334 Safari/7534.48.3")
val list = List(
"IsMobile",
"ScreenPixelsWidth",
"ScreenPixelsHeight",
"BrowserName",
"BrowserVersion"
).map(p => deviceMatch.getValues(p).toString)
println(list)
The example will result in the following output to the console.
List(True, 640, 960, Mobile Safari, 5.1)
Specifying a Data File
To use an external data set from a file you need to pass the file path to a factory method:
val fileProvider = new Provider(StreamFactory.create("path/to/datafile"))
This provider can be used in exactly the same way the embedded provider, they are both 'Provider' objects.
Using this your Scala project can use the latest lite or premium device data.
Further Options
It's quick and easy to get 51Degrees device detection working with Scala we almost forgot we'd already done it and it was available as an option for customers. You can download the Scala project.
For a full run down of the classes, properties and methods available see our Java API documentation. To understand the properties returned about your customers devices see our property dictionary.