-
Notifications
You must be signed in to change notification settings - Fork 3
/
os.clj
36 lines (33 loc) · 1.22 KB
/
os.clj
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
(ns uap-clj.os
"Useragent o/s lookup"
(:require [uap-clj.common :refer [regexes-all first-match field]]
[clj-yaml.core :refer [parse-string]]
[clojure.java.io :as io :refer [resource]]
[clojure.string :as s :refer [join trim]]))
(def regexes (:os_parsers regexes-all))
(defn os
[ua]
(try
(let [match (first-match ua regexes)
result (first (flatten (vector (:result match))))]
(if (= "Other" result)
{:family "Other" :major nil :minor nil :patch nil :patch_minor nil}
(let [family (field match :os_replacement 1)
major (field match :os_v1_replacement 2)
minor (field match :os_v2_replacement 3)
patch (field match :os_v3_replacement 4)
patch-minor (field match :os_v4_replacement 5)]
{:family family
:major major
:minor minor
:patch patch
:patch_minor patch-minor})))
(catch java.lang.IndexOutOfBoundsException e
{:family "Other"
:major nil
:minor nil
:patch nil
:patch_minor nil})))
; For use in production settings where speed may be preferred
; in exchange for the tradeoff of increased memory bloat:
(def os-memoized (memoize os))