Skip to content

Erlang implementation of ua-parser (user agent parser)

License

Notifications You must be signed in to change notification settings

silviucpp/erluap

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

38 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Build Status GitHub Hex.pm

erluap - Erlang User-Agent Parser

This is the Erlang implementation of ua-parser. It provides a NIF wrapper around uap-cpp for efficient user-agent parsing.

Getting Started

Dependencies

Ensure the following dependencies are installed before compiling:

  • re2
  • yaml-cpp (0.5 API)
MacOS Installation

You can install the required dependencies using Homebrew:

brew install re2 yaml-cpp
Ubuntu Installation

On Ubuntu, install the dependencies using APT:

sudo apt-get install libyaml-cpp-dev libre2-dev

Integration

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"}}
]}.

Updating User-Agent Regexes

The user-agent regex definitions are sourced from uap-core. To update them, modify UAP_CORE_REV inside build_deps.sh and rebuild.


API

Parsing as records

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}}

Parsing as proplist

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}]}]

Detecting Crawlers (Spiders)

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).

Running Tests

To execute the test suite run:

make ct

About

Erlang implementation of ua-parser (user agent parser)

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published