diff --git a/lib/ua_parser.ex b/lib/ua_parser.ex index d615c1b..10a5a35 100644 --- a/lib/ua_parser.ex +++ b/lib/ua_parser.ex @@ -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 diff --git a/lib/ua_parser/parser.ex b/lib/ua_parser/parser.ex index f78765c..6765aa5 100644 --- a/lib/ua_parser/parser.ex +++ b/lib/ua_parser/parser.ex @@ -23,6 +23,7 @@ defmodule UAParser.Parser do end defp match(nil, _string), do: nil + defp match(group, string) do match = group @@ -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 diff --git a/lib/ua_parser/parsers/base.ex b/lib/ua_parser/parsers/base.ex index 5ebef62..01b2f49 100644 --- a/lib/ua_parser/parsers/base.ex +++ b/lib/ua_parser/parsers/base.ex @@ -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}) @@ -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 diff --git a/lib/ua_parser/parsers/device.ex b/lib/ua_parser/parsers/device.ex index a14c6db..8b76d25 100644 --- a/lib/ua_parser/parsers/device.ex +++ b/lib/ua_parser/parsers/device.ex @@ -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 diff --git a/lib/ua_parser/parsers/operating_system.ex b/lib/ua_parser/parsers/operating_system.ex index d006d15..2844cec 100644 --- a/lib/ua_parser/parsers/operating_system.ex +++ b/lib/ua_parser/parsers/operating_system.ex @@ -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 diff --git a/lib/ua_parser/parsers/user_agent.ex b/lib/ua_parser/parsers/user_agent.ex index bcf0a33..9851a80 100644 --- a/lib/ua_parser/parsers/user_agent.ex +++ b/lib/ua_parser/parsers/user_agent.ex @@ -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 diff --git a/lib/ua_parser/parsers/version.ex b/lib/ua_parser/parsers/version.ex index 0a2e62a..a458f7f 100644 --- a/lib/ua_parser/parsers/version.ex +++ b/lib/ua_parser/parsers/version.ex @@ -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 @@ -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 diff --git a/lib/ua_parser/processor.ex b/lib/ua_parser/processor.ex index c388d46..0f022e3 100644 --- a/lib/ua_parser/processor.ex +++ b/lib/ua_parser/processor.ex @@ -16,21 +16,22 @@ 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 @@ -38,27 +39,31 @@ defmodule UAParser.Processor do 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 diff --git a/lib/ua_parser/storage.ex b/lib/ua_parser/storage.ex index 026ae12..6c394ae 100644 --- a/lib/ua_parser/storage.ex +++ b/lib/ua_parser/storage.ex @@ -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