Skip to content

Commit

Permalink
Allow formatter
Browse files Browse the repository at this point in the history
  • Loading branch information
doomspork committed Nov 27, 2017
1 parent 64d50f3 commit 6b49db5
Show file tree
Hide file tree
Showing 9 changed files with 61 additions and 42 deletions.
2 changes: 1 addition & 1 deletion lib/ua_parser.ex
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,5 @@ defmodule UAParser do
"""
def parse(ua), do: Parser.parse(pattern(), ua)

defp pattern, do: Storage.list
defp pattern, do: Storage.list()
end
11 changes: 6 additions & 5 deletions lib/ua_parser/parser.ex
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ defmodule UAParser.Parser do
end

defp match(nil, _string), do: nil

defp match(group, string) do
match =
group
Expand Down Expand Up @@ -52,11 +53,11 @@ defmodule UAParser.Parser do

defp search(groups, string) do
groups
|> Enum.find(fn(group) ->
group
|> Keyword.fetch!(:regex)
|> Regex.match?(string)
end)
|> Enum.find(fn group ->
group
|> Keyword.fetch!(:regex)
|> Regex.match?(string)
end)
|> match(string)
end
end
7 changes: 5 additions & 2 deletions lib/ua_parser/parsers/base.ex
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,19 @@ defmodule UAParser.Parsers.Base do
@callback parse(args :: term) :: result :: term | nil

defmacro replacement_parser(opts) do
[family_key|replacements] = Keyword.fetch!(opts, :keys)
[family_key | replacements] = Keyword.fetch!(opts, :keys)
mod = opts[:struct]

quote do
def parse(nil), do: struct(unquote(mod), %{})

def parse({group, match}) do
family =
group
|> Keyword.get(unquote(family_key))
|> UAParser.Parsers.Base.replace(1, match)

match = Enum.slice(match, 1, 4)
match = Enum.slice(match, 1, 4)
version = UAParser.Parsers.Version.parse({group, match}, unquote(replacements))

struct(unquote(mod), %{family: family, version: version})
Expand All @@ -26,8 +27,10 @@ defmodule UAParser.Parsers.Base do
end

def replace(nil, position, match), do: Enum.at(match, position)

def replace(string, position, match) do
val = Enum.at(match, position)

if val do
String.replace(string, "$#{position}", val)
else
Expand Down
22 changes: 14 additions & 8 deletions lib/ua_parser/parsers/device.ex
Original file line number Diff line number Diff line change
Expand Up @@ -10,21 +10,27 @@ defmodule UAParser.Parsers.Device do

alias UAParser.Device

@replacements [brand: :brand_replacement, family: :device_replacement, model: :model_replacement]
@replacements [
brand: :brand_replacement,
family: :device_replacement,
model: :model_replacement
]

def parse(nil), do: %Device{}
def parse({group, match}),
do: do_replacement(@replacements, {group, match}, %Device{})
def parse({group, match}), do: do_replacement(@replacements, {group, match}, %Device{})

def do_replacement([], _, device), do: device
def do_replacement([{key, replacement}| replacements], {group, match}, device) do

def do_replacement([{key, replacement} | replacements], {group, match}, device) do
replace = Keyword.get(group, replacement)

replace =
match
|> Enum.with_index
|> Enum.reduce(replace, fn({_, index}, acc) ->
replace(acc, index, match)
end)
|> Enum.with_index()
|> Enum.reduce(replace, fn {_, index}, acc ->
replace(acc, index, match)
end)

do_replacement(replacements, {group, match}, Map.put(device, key, replace))
end
end
13 changes: 7 additions & 6 deletions lib/ua_parser/parsers/operating_system.ex
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,11 @@ defmodule UAParser.Parsers.OperatingSystem do

alias UAParser.OperatingSystem

replacement_parser struct: OperatingSystem,
keys: [:os_replacement,
:os_v1_replacement,
:os_v2_replacement,
:os_v3_replacement,
:os_v4_replacement]
replacement_parser(struct: OperatingSystem, keys: [
:os_replacement,
:os_v1_replacement,
:os_v2_replacement,
:os_v3_replacement,
:os_v4_replacement
])
end
13 changes: 7 additions & 6 deletions lib/ua_parser/parsers/user_agent.ex
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,11 @@ defmodule UAParser.Parsers.UA do

import Base

replacement_parser struct: UA,
keys: [:family_replacement,
:v1_replacement,
:v2_replacement,
:v3_replacement,
:v4_replacement]
replacement_parser(struct: UA, keys: [
:family_replacement,
:v1_replacement,
:v2_replacement,
:v3_replacement,
:v4_replacement
])
end
4 changes: 3 additions & 1 deletion lib/ua_parser/parsers/version.ex
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,10 @@ defmodule UAParser.Parsers.Version do
alias UAParser.Version

def parse(nil), do: %Version{}

def parse(grouping, keys \\ []) do
keys
|> Enum.with_index
|> Enum.with_index()
|> Enum.map(&parse_version(grouping, &1))
|> version
end
Expand All @@ -25,6 +26,7 @@ defmodule UAParser.Parsers.Version do
end

defp version([major]), do: %Version{major: major}

defp version([major, minor, patch, patch_minor]),
do: %Version{major: major, minor: minor, patch: patch, patch_minor: patch_minor}
end
27 changes: 16 additions & 11 deletions lib/ua_parser/processor.ex
Original file line number Diff line number Diff line change
Expand Up @@ -16,49 +16,54 @@ defmodule UAParser.Processor do

defp atom_key(key) do
key
|> String.Chars.to_string
|> String.to_atom
|> String.Chars.to_string()
|> String.to_atom()
end

defp compile(groups) do
# result: {user_agents, os, devices}
groups
|> Enum.map(&compile_groups/1)
|> to_tuple # result: {user_agents, os, devices}
|> to_tuple
end

defp compile_group(group) do
pattern =
group
|> Keyword.fetch!(:regex)
|> Regex.compile!
|> Regex.compile!()

Keyword.put(group, :regex, pattern)
end

defp compile_groups(groups), do: Enum.map(groups, &compile_group/1)

defp convert([]), do: []
defp convert([head|tail]) do

defp convert([head | tail]) do
result = Enum.map(head, &to_keyword/1)
[result|convert(tail)]
[result | convert(tail)]
end

defp extract([document|_]) do
[{'user_agent_parsers', user_agents}, {'os_parsers', os}, {'device_parsers', devices}] = document
defp extract([document | _]) do
[{'user_agent_parsers', user_agents}, {'os_parsers', os}, {'device_parsers', devices}] =
document

[user_agents, os, devices]
end

defp to_keyword([]), do: []
defp to_keyword([{key, value}|tails]) do

defp to_keyword([{key, value} | tails]) do
keyword = {atom_key(key), String.Chars.to_string(value)}
[keyword | to_keyword(tails)]
end

defp to_tuple(values, tuple \\ {})
defp to_tuple([], tuple), do: tuple
defp to_tuple([head|tail], tuple) do
tuple = Tuple.append(tuple, head)

defp to_tuple([head | tail], tuple) do
tuple = Tuple.append(tuple, head)
to_tuple(tail, tuple)
end
end
4 changes: 2 additions & 2 deletions lib/ua_parser/storage.ex
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@ defmodule UAParser.Storage do

data =
:ua_parser
|> :code.priv_dir
|> :code.priv_dir()
|> Kernel.++('/patterns.yml')
|> to_string
|> :yamerl_constr.file([])
|> Processor.process
|> Processor.process()

@data data

Expand Down

0 comments on commit 6b49db5

Please sign in to comment.