-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Georgy Sychev
committed
Oct 29, 2019
1 parent
bd84115
commit 74ee4d7
Showing
3 changed files
with
104 additions
and
55 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
defmodule Wootheex.UserAgent do | ||
|
||
@type t :: %__MODULE__{ | ||
category: Wootheex.category(), | ||
browser_name: Wootheex.browser_name(), | ||
browser_type: Wootheex.browser_type(), | ||
browser_version: Wootheex.browser_version(), | ||
os: Wootheex.os(), | ||
os_version: Wootheex.os_version(), | ||
vendor: Wootheex.vendor() | ||
} | ||
@enforce_keys [ | ||
:category, | ||
:browser_name, | ||
:browser_type, | ||
:browser_version, | ||
:os, | ||
:os_version, | ||
:vendor | ||
] | ||
defstruct @enforce_keys | ||
|
||
@doc """ | ||
Parse user agent to a struct or raises an error | ||
iex> Wootheex.UserAgent.parse! "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/44.0.2403.155 Safari/537.36" | ||
%Wootheex.UserAgent{ | ||
browser_name: "Chrome", | ||
browser_type: :browser, | ||
browser_version: "44.0.2403.155", | ||
category: :pc, | ||
os: "Mac OSX", | ||
os_version: "10.10.4", | ||
vendor: "Google" | ||
} | ||
""" | ||
@spec parse!(Wootheex.user_agent) :: t() | ||
def parse!(ua) do | ||
case Wootheex.parse(ua) do | ||
:other -> raise WootheexError, "unknown user agent" | ||
{cat, bname, btype, bver, os, osver, vendor} -> | ||
%__MODULE__{ | ||
category: cat, | ||
browser_name: bname, | ||
browser_type: btype, | ||
browser_version: bver, | ||
os: os, | ||
os_version: osver, | ||
vendor: vendor | ||
} | ||
end | ||
end | ||
|
||
@doc """ | ||
Safely parses user agent to a struct | ||
iex> Wootheex.UserAgent.parse "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/44.0.2403.155 Safari/537.36" | ||
{:ok, | ||
%Wootheex.UserAgent{ | ||
browser_name: "Chrome", | ||
browser_type: :browser, | ||
browser_version: "44.0.2403.155", | ||
category: :pc, | ||
os: "Mac OSX", | ||
os_version: "10.10.4", | ||
vendor: "Google" | ||
} | ||
} | ||
""" | ||
@spec parse(Wootheex.user_agent) :: {:ok, t()} | {:error, any()} | ||
def parse(ua) do | ||
case Wootheex.parse(ua) do | ||
:other -> {:error, "unknown user agent"} | ||
{cat, bname, btype, bver, os, osver, vendor} -> | ||
{:ok, | ||
%__MODULE__{ | ||
category: cat, | ||
browser_name: bname, | ||
browser_type: btype, | ||
browser_version: bver, | ||
os: os, | ||
os_version: osver, | ||
vendor: vendor | ||
} | ||
} | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters