Git Product home page Git Product logo

ua_parser's Introduction

UAParser

Build Status License

A simple, fast user-agent parsing library based on BrowserScope's UA database with a good default dictionary.

Installation

Add ua_parser to your mix.exs dependencies:

def deps do
  [{:ua_parser, "~> 1.4"}]
end

Usage

iex> ua = UAParser.parse("Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10_5_7; en-us) AppleWebKit/530.17 (KHTML, like Gecko) Version/4.0 Safari/530.17 Skyfire/2.0")
%UAParser.UA{
  device: %UAParser.Device{family: nil},
  family: "Skyfire",
  os: %UAParser.OperatingSystem{
    family: "Mac OS X",
    version: %UAParser.Version{major: "10", minor: "5", patch: "7", patch_minor: nil}
  },
  version: %UAParser.Version{major: "2", minor: "0", patch: nil, patch_minor: nil}
}
iex> to_string(ua)
"Skyfire 2.0"
iex> to_string(ua.os)
"Mac OS X 10.5.7"
iex> to_string(ua.device)
"Other"

Retrieving and parsing a user-agent string from Plug:

ua =
  conn
  |> Plug.Conn.get_req_header("user-agent")
  |> List.first()
  |> UAParser.parse()

Example

There is an example Phoenix application with a simple endpoint/route to retrieve the user agent header. To run it, simply cd into the example directory, and run the following:

Phoenix

$ mix do deps.get, compile
$ npm i && brunch build
# change the configuration to match your own postgres username/password
$ mix do ecto.create, ecto.migrate
$ iex -S mix phoenix.server
# visit localhost:4000/user_agent

Plug

It can also be used as a simple plug:

mix new ua_plug
cd ua_plug

And now set up mix.exs:

# ...
  defp deps do
    [
     {:cowboy, "~> 1.0.0"},
     {:plug, "~> 1.0"},
     {:ua_parser, git: "https://github.com/doomspork/ua_parser.git",
                  branch: "master"},
    ]
  end
# ...

And we get our deps and compile:

$ mix do deps.get, compile

And now we can make our plug:

defmodule UAPlug do
  import Plug.Conn

  def init(options), do: options

  def call(conn, _opts) do
    ua =
      conn
         |> Plug.Conn.get_req_header("user-agent")
         |> List.first()
         |> UAParser.parse()
    send_resp(200, ua)
  end
end

From here we can pipe requests through it in other applications or use it on its own as we please.

License

UAParser source code is released under Apache 2.0 License.

See LICENSE for more information.

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.