This is the Erlang implementation of ua-parser. It provides a NIF wrapper around uap-cpp for efficient user-agent parsing.
Ensure the following dependencies are installed before compiling:
re2
yaml-cpp
(0.5 API)
You can install the required dependencies using Homebrew:
brew install re2 yaml-cpp
On Ubuntu, install the dependencies using APT:
sudo apt-get install libyaml-cpp-dev libre2-dev
To add it as a dependency in your project, include the following in your rebar.config
:
{deps, [
{erluap, ".*", {git, "https://github.com/silviucpp/erluap.git", "master"}}
]}.
The user-agent regex definitions are sourced from uap-core. To update them, modify UAP_CORE_REV
inside build_deps.sh
and rebuild.
erluap returns parsed data as records. The structures are defined in erluap.hrl
:
-record(device, {
device_type :: device_type(),
family :: value(),
model :: value(),
brand :: value()
}).
-record(agent, {
family :: value(),
version_major :: value(),
version_minor :: value(),
version_patch :: value(),
version_patch_minor :: value()
}).
The parse/1
function returns a tuple {Device::#device{}, Os::#agent{}, Browser::#agent{}}
:
erluap:parse(<<"Mozilla/5.0 (iPhone; CPU iPhone OS 12_2 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Mobile/15E148">>).
{{device,mobile,<<"iPhone">>,<<"iPhone">>,<<"Apple">>},
{agent,<<"iOS">>,<<"12">>,<<"2">>,null,null},
{agent,<<"Mobile Safari UI/WKWebView">>,null,null,null,null}}
erluap can also return parsed data as a proplist using erluap:parse_as_proplist/1
:
erluap:parse_as_proplist(<<"Mozilla/5.0 (iPhone; CPU iPhone OS 12_2 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Mobile/15E148">>).
[{device,[{device_type,mobile},
{family,<<"iPhone">>},
{model,<<"iPhone">>},
{brand,<<"Apple">>}]},
{os,[{family,<<"iOS">>},
{version_major,<<"12">>},
{version_minor,<<"2">>},
{version_patch,null},
{version_patch_minor,null}]},
{browser,[{family,<<"Mobile Safari UI/WKWebView">>},
{version_major,null},
{version_minor,null},
{version_patch,null},
{version_patch_minor,null}]}]
To check if a user-agent is a web crawler (spider), verify if the device family is <<"Spider">>
or use the helper function:
erluap:is_spider(UserAgent).
To execute the test suite run:
make ct