Skip to content

Commit

Permalink
[refs #7] short-circuits lookups of empty user agents
Browse files Browse the repository at this point in the history
  • Loading branch information
mneudert committed May 22, 2017
1 parent c7c30eb commit fc6208f
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 4 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,16 @@
## v0.14.0-dev

- Enhancements
- Empty user agents (`""` or `nil`) now return a result without performing
an actual lookup. By definition an empty user agent is never detected
as a bot
- System environment configuration can set an optional default value
to be used if the environment variable is unset

- Bug fixes
- Properly handles `nil` values passed to the lookup functions
([#7](https://github.com/elixytics/ua_inspector/issues/7))

## v0.13.0 (2016-09-08)

- Enhancements
Expand Down
11 changes: 11 additions & 0 deletions lib/ua_inspector/pool.ex
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ defmodule UAInspector.Pool do
Connects the plain UAInspector interface with the underlying pool.
"""

alias UAInspector.Result


@pool_name :ua_inspector_pool
@pool_options [
name: { :local, @pool_name },
Expand All @@ -28,6 +31,8 @@ defmodule UAInspector.Pool do
Sends a bot check request to a pool worker.
"""
@spec bot?(String.t) :: boolean
def bot?(nil), do: false
def bot?(""), do: false
def bot?(ua) do
:poolboy.transaction(
@pool_name,
Expand All @@ -39,6 +44,8 @@ defmodule UAInspector.Pool do
Sends a HbbTV check request to a pool worker..
"""
@spec hbbtv?(String.t) :: false | String.t
def hbbtv?(nil), do: false
def hbbtv?(""), do: false
def hbbtv?(ua) do
:poolboy.transaction(
@pool_name,
Expand All @@ -50,6 +57,8 @@ defmodule UAInspector.Pool do
Sends a parse request to a pool worker.
"""
@spec parse(String.t) :: map
def parse(nil), do: %Result{ user_agent: nil }
def parse(""), do: %Result{ user_agent: "" }
def parse(ua) do
:poolboy.transaction(
@pool_name,
Expand All @@ -61,6 +70,8 @@ defmodule UAInspector.Pool do
Sends a client parse request to a pool worker.
"""
@spec parse_client(String.t) :: map
def parse_client(nil), do: %Result{ user_agent: nil }
def parse_client(""), do: %Result{ user_agent: "" }
def parse_client(ua) do
:poolboy.transaction(
@pool_name,
Expand Down
17 changes: 13 additions & 4 deletions test/ua_inspector/parser_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,8 @@ defmodule UAInspector.ParserTest do
end

test "parse empty" do
agent = ""
parsed = %Result{ user_agent: agent }

assert parsed == UAInspector.parse(agent)
assert UAInspector.parse(nil) == %Result{ user_agent: nil }
assert UAInspector.parse("") == %Result{ user_agent: "" }
end

test "parse unknown" do
Expand All @@ -29,11 +27,17 @@ defmodule UAInspector.ParserTest do


test "bot?" do
refute UAInspector.bot?(nil)
refute UAInspector.bot?("")

assert UAInspector.bot?("generic crawler agent")
refute UAInspector.bot?("regular user agent")
end

test "hbbtv?" do
refute UAInspector.hbbtv?(nil)
refute UAInspector.hbbtv?(nil)

assert "1.1.1" == UAInspector.hbbtv?("agent containing HbbTV/1.1.1 (; ;) information")
refute UAInspector.hbbtv?("generic user agent")
end
Expand All @@ -56,4 +60,9 @@ defmodule UAInspector.ParserTest do

assert parsed == UAInspector.parse_client(agent)
end

test "parse_client empty" do
assert UAInspector.parse_client(nil) == %Result{ user_agent: nil }
assert UAInspector.parse_client("") == %Result{ user_agent: "" }
end
end

0 comments on commit fc6208f

Please sign in to comment.