Today, I detached another chunk of my codebase into a separate library. This time, it is for parsing User-Agent headers with Clojure. For a long time, I’ve been copying it from project to project, but now I decided to keep it aside from the code because it is not connected to the business logic. All the technical stuff that is not related to the very problem we are trying to solve should be kept as a separate library, I believe. The main code is all about the business problem.

The library ships only one function, which is parse. It takes a string and returns a detailed map with information about the browser’s type, its version, manufacturer, operating system, etc. The library relies on good old Java UA Detector with the latest set of patterns. I know, there are already some solutions for that. But they either do not provide full information about a user or maintainers do not accept pull requests.

Add it to your project:

:dependencies [[user-agent "0.1.0-SNAPSHOT"]]

Usage:

(ns some.ns
  (:require [user-agent :as ua]))

(def ua-sample
  "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.99 Safari/537.36")

(ua/parse ua-sample)

{:producer "Google Inc."
 :family :CHROME
 :name "Chrome"
 :type :BROWSER
 :icon "chrome.png"
 :producer-url "http://www.google.com/"
 :url "http://www.google.com/chrome"
 :device {:category :PERSONAL_COMPUTER
          :name "Personal computer"}
 :os
 {:family :OS_X
  :family-name "OS X"
  :name "OS X"
  :producer "Apple Computer, Inc."
  :producer-url "http://www.apple.com/"
  :url "http://en.wikipedia.org/wiki/Mac_OS_X"
  :version
  {:bug-fix "6"
   :extension ""
   :groups ["10" "11" "6"]
   :major "10"
   :minor "11"
   :version "10.11.6"}}
 :type-name "Browser"
 :version
 {:bug-fix "3396"
  :extension ""
  :groups ["67" "0" "3396" "99"]
  :major "67"
  :minor "0"
  :version "67.0.3396.99"}}

It supports plenty of desktop and mobile browsers as well as unusual ones like bots, crawlers or TVs. Hope you’ll enjoy it, your suggestions and PR’s are welcome.