Giter Site home page Giter Site logo

uap-erlang's Introduction

Erlang User-Agent Parser Library for the uap-core project.

Related Links

Preflight

Get a copy of regexes.yaml with:

make priv/regexes.yaml

Usage

Library

From a make all shell you should be able to just run:

application:ensure_all_started(yamerl),
rr(uap),
f(),
{ok, UAP} = uap:state(file, "priv/regexes.yaml"),
UA = "Mozilla/5.0 (X11; Linux x86_64; rv:55.0) Gecko/20100101 Firefox/55.0",
uap:parse(UA, UAP).

{ok, [
   #uap_ua{family = "Firefox",major = "55",minor = "0",
         patch = undefined},
   #uap_os{family = "Linux",major = undefined,minor = undefined,
         patch = undefined,patch_minor = undefined},
   #uap_device{family = "Other",brand = undefined,
             model = undefined}
]}

Standalone Server

application:ensure_all_started(uap),
rr(uap).
uap_server:parse(UA, [os]).

Application

Add to your rel/sys.config so that when the application (re)starts it knows how to build its internal state:

{uap, [{priv,APP}]}

Now just add to the applications section of your APP.app.src file uap.

erlang.mk

Add to your Makefile:

DEPS += uap
dep_uap = git https://gitlab.com/jimdigriz/uap-erlang.git master

API

Library

state(file | string, iodata()) -> {ok, uap()}

Loads in YAML in the expected format from either a provided filepath or in-memory string.

Throws on error.

parse(iodata(), uap()) -> {ok, [uap_ua() | uap_os() | uap_device()]} | {error,atom()}

Same as parse(iodata(), [ua, os, device], uap()).

parse(iodata(), Order, uap()) -> {ok, [uap_ua() | uap_os() | uap_device()]} | {error,atom()}

Parses the User-Agent in passed in as the string UA.

N.B. from the unit tests, it seems that parsing binaries is faster than lists

Server/Application

start_link()

Same as start_link([]).

start_link(list(proplists:property()))

Supported configuration properties are:

  • priv (default: uap): application name for the priv directory where regexes.yaml is located
  • file (default: regexes.yaml): name of the regexes file to load
  • cache (default: 10000): number of entries for the lookup cache (can also be 0 for disabled and unlimited)

parse(iodata(), list(ua | os | device)) -> {ok, [uap_ua() | uap_os() | uap_device()]} | {error,atom()}`

Same as parse(iodata(), list(ua | os | device), []).

parse(iodata(), list(ua | os | device), list(proplists:property())) -> {ok, [uap_ua() | uap_os() | uap_device()]} | {error,atom()}`

Supported properties are:

  • normalize (default: false):
    • improves the effectiveness of the cache
    • recommended only when the cardinality of User-Agents is high
    • suitable for use only when you require #uap_{ua,os}.{family,major} as it is inaccurate for all other fields!

An an error is returned if you duplicate any of the atoms ua os or device in the list of results to return.

Testing

Fetch a copy of the test data:

make testdata

Now run the tests (takes about five minutes and needs 4GB of RAM) with:

make tests

If you just want to test the uap-core test data (takes about 30 seconds and needs 4GB of RAM) use:

make tests CT_SUITES=uap

uap-erlang's People

Contributors

jimdigriz avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.