class DeviceAtlas

Copyright
Author

Used to load the recognition tree and perform lookups of all properties, or get individual properties. Typical usage is as follows:

deviceAtlas = DeviceAtlas.new
path = File.join(File.dirname(__FILE__), '..', 'data_file/sample.json')
tree = deviceAtlas.getTreeFromFile(path)
properties = deviceAtlas.getProperties(tree, "Nokia6680...")
property = deviceAtlas.getProperty(tree, "Nokia6680...", "displayWidth")

Note that you should normally use the user-agent that was received in the device's HTTP request. In a Rails environment, you would do this as follows:

userAgent = request.env['HTTP_USER_AGENT']
displayWidth = deviceAtlas.getPropertyAsInteger(tree, userAgent,
"displayWidth")

Third-party Browsers:

In some contexts, the user-agent you want to recognise may have been provided in a different header. Opera's mobile browser, for example, makes requests via an HTTP proxy, which rewrites the headers. in that case, the original device's user-agent is in the “X-OperaMini-Phone-UA” header, and the following code could be used:

operaHeader = "X-OperaMini-Phone-UA"
if request.env.include?(operaHeader)
        userAgent = request.env[operaHeader]
else
        userAgent = request.env['HTTP_USER_AGENT']
end
displayWidth = deviceAtlas.getPropertyAsInteger(tree, userAgent,
"displayWidth")

See here for more information: deviceatlas.com/resources/side-loaded-browser-handling

Client side properties:

Client side properties can be collected and merged into the results by using the DeviceAtlas Javascript detection file. The results from the client side are sent to the server inside a cookie. The contents of this cookie can be passed to the DeviceAtlas getProperty and getProperties methods. The client side properties over-ride any data file properties and also serve as an input into additional logic to determine other properties such as the iPhone models that are otherwise not detectable. The following code shows how this can be done in Rails:

userAgent = request.env['HTTP_USER_AGENT']
cookieContents = request.cookies['DAPROPS']
properties = deviceAtlas.getPropertiesAsTyped(tree, userAgent,
cookieContents)

Public Class Methods

new() click to toggle source
# File ../../device_api/trunk/ruby/src/lib/deviceatlas.rb, line 89
def initialize
  @device_api = Mobi::Mtld::Da::Device::DeviceApi.new
  @tree_loaded = false
end

Public Instance Methods

getApiRevision() click to toggle source

Return the revision number of the API.

# File ../../device_api/trunk/ruby/src/lib/deviceatlas.rb, line 193
def getApiRevision
  return get_revision_from_keyword "$Rev: 40211 $"
end
getProperties(tree, userAgent, cookie = nil, typedValues = false, sought = nil, uaPropsNeeded = true) click to toggle source

Return a associative array of known properties merged with properties from the client side JavaScript. The client side JavaScript sets a cookie with collected properties. The contents of this cookie must be passed to this method for it to work. The client properties over-ride any properties discovered from the main JSON data file.

tree

Previously generated associative array tree.

userAgent

The device's User-Agent header string.

cookie

The content of the cookie containing the client side properties.

typedValues

Whether values in the results are typed.

sought

A set of properties to return values for.

uaPropsNeeded

Whether the extra properties from the UA String are

needed.

DEPRECATED: Please use Mobi::Mtld::Da::Device::DeviceApi.properties instead.

# File ../../device_api/trunk/ruby/src/lib/deviceatlas.rb, line 228
def getProperties(tree, userAgent, cookie = nil, typedValues = false,
sought = nil, uaPropsNeeded = true)
  get_props_as_hash @device_api.properties(userAgent, cookie)
end
getPropertiesAsTyped(tree, userAgent, cookie = nil) click to toggle source

Return an associative array of known properties merged with properties from the client side JavaScript. The client side JavaScript sets a cookie with collected properties. The contents of this cookie must be passed to this method for it to work. The client properties over-ride any properties discovered from the main JSON data file.

tree

Previously generated associative array tree.

userAgent

The device's User-Agent header string.

cookie

The content of the cookie containing the client side properties.

DEPRECATED: Please use Mobi::Mtld::Da::Device::DeviceApi.properties instead.

# File ../../device_api/trunk/ruby/src/lib/deviceatlas.rb, line 245
def getPropertiesAsTyped(tree, userAgent, cookie = nil)
  properties_out = {}
  properties_in = @device_api.properties userAgent, cookie
  properties_in.each do |name, property_in|
    case property_in.data_type_id
    when Mobi::Mtld::Da::DataType::BOOLEAN
      properties_out[name.to_s] = property_in.value.to_i == 1
    when Mobi::Mtld::Da::DataType::STRING
      properties_out[name.to_s] = property_in.value.to_s
    when Mobi::Mtld::Da::DataType::INTEGER
      properties_out[name.to_s] = property_in.value.to_i
    when Mobi::Mtld::Da::DataType::DOUBLE
      properties_out[name.to_s] = property_in.value.to_f
    else
      properties_out[name.to_s] = property_in.value
    end
  end
  return properties_out
end
getProperty(tree, userAgent, property, cookie = nil, typedValue = false) click to toggle source

Return a value for the named property for this user agent.

tree

Previously generated associative array tree.

userAgent

The device's User-Agent header string.

property

The name of the property to return.

cookie

The contents of the cookie containing the client side properties.

typedValue

Whether value in the associative array are typed.

DEPRECATED: Please use Mobi::Mtld::Da::Device::DeviceApi.properties instead.

# File ../../device_api/trunk/ruby/src/lib/deviceatlas.rb, line 275
def getProperty(tree, userAgent, property, cookie = nil, typedValue = false)
  return get_property_object(userAgent, property, cookie).to_s
end
getPropertyAsBoolean(tree, userAgent, property, cookie = nil) click to toggle source

Strongly typed property access. Return a boolean property (or throws an exception if the property is actually of another type).

tree

Previously generated associative array tree.

userAgent

The device's User-Agent header string.

property

The name of the property to return.

DEPRECATED: Please use Mobi::Mtld::Da::Device::DeviceApi.properties and Mobi::Mtld::Da::Property.to_binstead.

# File ../../device_api/trunk/ruby/src/lib/deviceatlas.rb, line 290
def getPropertyAsBoolean(tree, userAgent, property, cookie = nil)
  begin
    property_obj = get_property_object userAgent, property, cookie
    if property_obj.data_type_id != Mobi::Mtld::Da::DataType::BOOLEAN
      raise IncorrectPropertyTypeException,
        "#{property} is not of type Boolean"
    end
    value = property_obj.to_s
    return value == "1" || value[0].downcase == 't'
  rescue UnknownPropertyException => e
    raise IncorrectPropertyTypeException, e.message
  end
end
getPropertyAsDate(tree, userAgent, property, cookie = nil) click to toggle source

Strongly typed property access. Returns a date property (or throws an exception if the property is actually of another type).

tree

Previously generated associative array tree.

userAgent

The device's User-Agent header string.

property

The name of the property to return.

DEPRECATED: Date data type is not supported anymore.

# File ../../device_api/trunk/ruby/src/lib/deviceatlas.rb, line 313
def getPropertyAsDate(tree, userAgent, property, cookie = nil)
  raise IncorrectPropertyTypeException, "Date data type is not supported
    anymore as there are no Date device properties."
end
getPropertyAsInteger(tree, userAgent, property, cookie = nil) click to toggle source

Strongly typed property access. Return an integer property (or throws an exception if the property is actually of another type).

tree

Previously generated associative array tree.

userAgent

The device's User-Agent header string.

property

The name of the property to return.

DEPRECATED: Please use DeviceApi.properties and Property.to_i instead.

# File ../../device_api/trunk/ruby/src/lib/deviceatlas.rb, line 328
def getPropertyAsInteger(tree, userAgent, property, cookie = nil)
  begin
    property_obj = get_property_object userAgent, property, cookie
    if property_obj.data_type_id != Mobi::Mtld::Da::DataType::INTEGER
      raise IncorrectPropertyTypeException,
        "#{property} is not of type Boolean"
    end
    return property_obj.to_i
  rescue UnknownPropertyException => e
    raise IncorrectPropertyTypeException, e.message
  end
end
getPropertyAsString(tree, userAgent, property, cookie = nil) click to toggle source

Strongly typed property access. Return a string property (or throws an exception if the property is actually of another type).

tree

Previously generated associative array tree.

userAgent

The device's User-Agent header string.

property

The name of the property to return.

DEPRECATED: Please use DeviceApi.properties and Property.to_s instead.

# File ../../device_api/trunk/ruby/src/lib/deviceatlas.rb, line 351
def getPropertyAsString(tree, userAgent, property, cookie = nil)
  begin
    property_obj = get_property_object userAgent, property, cookie
    if property_obj.data_type_id != Mobi::Mtld::Da::DataType::STRING
      raise IncorrectPropertyTypeException,
        "#{property} is not of type Boolean"
    end
    return property_obj.to_s
  rescue UnknownPropertyException => e
    raise IncorrectPropertyTypeException, e.message
  end
end
getTreeFromFile(filename, reload = false, includeChangeableUserAgentProperties = true) click to toggle source

Return a tree from a JSON file. The loaded tree is stored in a static cache to avoid multiple reloads if this method is repeatedly called. To reload from the JSON file set “reload” to true.

Some properties cannot be known before runtime and can change from user-agent to user-agent. The most common of these are the OS Version and the Browser Version. This API is able to dynamically detect these changing properties but introduces a small overhead to do so. To disable returning these extra properties set “includeChangeableUserAgentProperties” to false.

filename

The location of the file to read in. Use an absolute path

name to be sure of success if the current working directory is not clear.

reload

Set true to reload regardless of static cache.

includeChangeableUserAgentProperties

Also detect changeable user-agent

properties.

DEPRECATED: Please use Mobi::Mtld::Da::Device::DeviceApi.load_data_from_file.

# File ../../device_api/trunk/ruby/src/lib/deviceatlas.rb, line 112
def getTreeFromFile(filename, reload = false,
includeChangeableUserAgentProperties = true)
  if !reload && @tree_loaded
    return
  end

  # We need to reset our DeviceApi object with new settings in order to allow
  # the detection of user-agent properties.
  if !includeChangeableUserAgentProperties
    settings = Mobi::Mtld::Da::Device::Settings.new
    settings.include_ua_props = false
    @device_api = Mobi::Mtld::Da::Device::DeviceApi.new settings
  end

  @device_api.load_data_from_file filename
  @tree_loaded = true

  return {}
end
getTreeFromString(json, includeChangeableUserAgentProperties = true) click to toggle source

Return a loaded JSON tree from a string of JSON data.

Some properties cannot be known before runtime and can change from user-agent to user-agent. The most common of these are the OS Version and the Browser Version. This API is able to dynamically detect these changing properties but introduces a small overhead to do so. To disable returning these extra properties set “includeChangeableUserAgentProperties” to false.

This method does not use the built in static cache.

json

The string of json data.

includeChangeableUserAgentProperties

Also detect changeable user-agent

properties.

DEPRECATED: Please use Mobi::Mtld::Da::Device::DeviceApi.load_data_from_string.

# File ../../device_api/trunk/ruby/src/lib/deviceatlas.rb, line 148
def getTreeFromString(json, includeChangeableUserAgentProperties = true)
  # We need to reset our DeviceApi object with new settings in order to allow
  # the detection of user-agent properties.
  if !includeChangeableUserAgentProperties
    settings = Mobi::Mtld::Da::Device::Settings.new
    settings.include_ua_props = false
    @device_api = Mobi::Mtld::Da::Device::DeviceApi.new settings
  end

  @device_api.load_data_from_string json
  @tree_loaded = true

  return {}
end
getTreeGeneration(tree) click to toggle source

Get the generation date for this tree.

tree

Hash object with the tree.

return

Date and time the tree was generated.

DEPRECATED: Please use Mobi::Mtld::Da::Device::DeviceApi.data_creation_timestamp.

# File ../../device_api/trunk/ruby/src/lib/deviceatlas.rb, line 169
def getTreeGeneration(tree)
  time = @device_api.data_creation_timestamp
  return Time.at(time).strftime("%b %d, %Y %H:%m:%S %p")
end
getTreeGenerationAsTimestamp(tree) click to toggle source

Get the generation date for this tree as a UNIX timestamp.

tree

Hash object with the tree.

return

Timestamp the tree was generated.

DEPRECATED: Please use Mobi::Mtld::Da::Device::DeviceApi.data_creation_timestamp.

# File ../../device_api/trunk/ruby/src/lib/deviceatlas.rb, line 180
def getTreeGenerationAsTimestamp(tree)
  return @device_api.data_creation_timestamp
end
getTreeRevision(tree) click to toggle source

Return the revision number of the tree.

DEPRECATED: Please use Mobi::Mtld::Da::Device::DeviceApi.data_revision instead.

# File ../../device_api/trunk/ruby/src/lib/deviceatlas.rb, line 188
def getTreeRevision(tree)
  return get_revision_from_keyword @device_api.data_revision
end
listProperties(tree) click to toggle source

Return an associative array of all property names available for all user agents in this tree, with their data type names.

DEPRECATED: Please use Mobi::Mtld::Da::Device::DeviceApi.property_names instead.

# File ../../device_api/trunk/ruby/src/lib/deviceatlas.rb, line 202
def listProperties(tree)
  properties = {}
  property_names = @device_api.property_names
  property_names.each do |property_name|
    properties[property_name.name] =
      get_property_type_as_string property_name.data_type_id
  end
  return properties
end