-
Notifications
You must be signed in to change notification settings - Fork 11
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
Showing
9 changed files
with
126 additions
and
32 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,63 @@ | ||
defmodule UserAgentParser.Parser do | ||
@moduledoc """ | ||
Handle parsing the user-agent string | ||
""" | ||
|
||
alias UserAgentParser.UserAgent, as: Agent | ||
alias UserAgentParser.Parsers.{Device, OperatingSystem, UserAgent} | ||
|
||
@doc """ | ||
Parse a user-agent string given a set of patterns | ||
""" | ||
def parse({ua_patterns, os_patterns, device_patterns}, user_agent) do | ||
user_agent | ||
|> sanitize | ||
|> parse_os(os_patterns) | ||
|> parse_device(device_patterns) | ||
|> parse_user_agent(ua_patterns) | ||
end | ||
|
||
defp find_and_parse(patterns, user_agent, module) do | ||
patterns | ||
|> search(user_agent) | ||
|> module.parse | ||
end | ||
|
||
defp match(nil, _string), do: nil | ||
defp match(group, string) do | ||
match = | ||
group | ||
|> Keyword.fetch!(:regex) | ||
|> Regex.run(string) | ||
|
||
{group, match} | ||
end | ||
|
||
defp parse_device({user_agent, acc}, patterns) do | ||
device = find_and_parse(patterns, user_agent, Device) | ||
{user_agent, Map.put(acc, :device, device)} | ||
end | ||
|
||
defp parse_os(user_agent, patterns) do | ||
os = find_and_parse(patterns, user_agent, OperatingSystem) | ||
{user_agent, %{os: os}} | ||
end | ||
|
||
defp parse_user_agent({user_agent, acc}, patterns) do | ||
patterns | ||
|> find_and_parse(user_agent, UserAgent) | ||
|> Map.merge(acc) | ||
end | ||
|
||
defp sanitize(user_agent), do: String.trim(user_agent) | ||
|
||
defp search(groups, string) do | ||
groups | ||
|> Enum.find(fn(group) -> | ||
group | ||
|> Keyword.fetch!(:regex) | ||
|> Regex.match?(string) | ||
end) | ||
|> match(string) | ||
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
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,16 @@ | ||
defmodule UserAgentParser.ParserTest do | ||
use ExUnit.Case | ||
|
||
alias UserAgentParser.{Parser, Storage} | ||
|
||
@user_agent "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" | ||
|
||
test "parse a user agent given patterns and a string" do | ||
%{family: family, os: os, version: version} = Parser.parse(Storage.list, @user_agent) | ||
|
||
assert family == "Skyfire" | ||
assert to_string(version) == "2.0" | ||
assert os.family == "Mac OS X" | ||
assert to_string(os.version) == "10.5.7" | ||
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
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
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 |
---|---|---|
@@ -1,8 +1,4 @@ | ||
defmodule UserAgentParserTest do | ||
use ExUnit.Case | ||
doctest UserAgentParser | ||
|
||
test "the truth" do | ||
assert 1 + 1 == 2 | ||
end | ||
end |