Class: DeviceAtlas::Device::DeviceApi

Inherits:
Object
  • Object
show all
Defined in:
lib/deviceatlas/device/device_api.rb

Overview

The DeviceAtlas device detection API provides a way to detect devices based on the HTTP headers. Using these headers, the API returns device properties such as screen width, screen height, is mobile, vendor, model etc.

Direct Known Subclasses

DeviceApiWeb

Constant Summary collapse

API_VERSION =
'2.1.0'

Instance Method Summary collapse

Constructor Details

#initialize(config = nil) ⇒ Object

Creates a new DeviceAtlas::Device::DeviceApi instance.

Examples:

Create an object with default configuration

device_api = DeviceAtlas::Device::DeviceApi.new

Create an object passing custom configuration

config = DeviceAtlas::Device::Config.new
config.include_ua_props = false
config.include_lang_props = false
device_api = DeviceAtlas::Device::DeviceApi.new(config)

Parameters:

Instance Method Details

#data_creation_timestampString

Get the device data creation (JSON file) timestamp.

Returns:

  • (String)

    the creation timestamp of the device data file

Raises:

#data_revisionString

Deprecated.

Get the device data generator revision.

Returns:

  • (String)

    the revision of the device data file

Raises:

#data_versionString

Get the device data (JSON file) version.

Returns:

  • (String)

    the version of the device data file

Raises:

#get_properties(user_agent_or_headers, client_side = nil) ⇒ Hash<Symbol, Object>

Get the properties for a User-Agent or a set of HTTP headers merged with the client side properties (provided by the DeviceAtlas Client-side component).

The DeviceAtlas Client-side component (JavaScript library) collects the client-side properties and puts them in a cookie. The contents of this cookie must be passed to this method. The client properties will override any properties discovered from the main JSON data file.

Parameters:

  • user_agent_or_headers (String, Hash<String, String>)

    the User-Agent or the client's request HTTP headers

  • client_side (String) (defaults to: nil)

    the content of the cookie containing the client-side properties

Returns:

  • (Hash<Symbol, Object>)

    properties detected

Raises:

  • (ArgumentError)

#load_data_from_file(path) ⇒ DeviceAtlas::Device::Tree

Load the DeviceAtlas device detection data into the API from a JSON file. The JSON data file is provided by the DeviceAtlas website.

Examples:

Load the DeviceAtlas JSON data file

device_api = DeviceAtlas::Device::DeviceApi.new
device_api.load_data_from_file('/path/to/datafile.json')

Parameters:

  • path (String)

    the path to the DeviceAtlas JSON file

Returns:

Raises:

#load_data_from_string(json) ⇒ DeviceAtlas::Device::Tree

Load the DeviceAtlas device detection data into the API from a string.

Examples:

Load the DeviceAtlas JSON data file

device_api = DeviceAtlas::Device::DeviceApi.new
device_api.load_data_from_string('{...}')

Parameters:

  • json (String)

    the JSON data

Returns:

#property_namesHash<Symbol, String>

Get a set of all possible property names. Note that the client side properties are not included in this set.

Returns:

Raises:

#set_config(config) ⇒ Object

Updates the DeviceApi instance with a new config.

Parameters: