Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .minitestfailures
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[]
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# Changelog

## Unreleased

- Reuse the detected browser for bot detection instead of parsing the user agent
twice. Custom bot matchers now receive that browser instead of a
`Browser::Unknown` built from the downcased user agent.

## 6.2.0

- Add `Browser::Base#epiphany?`.
Expand Down
13 changes: 8 additions & 5 deletions lib/browser/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ class Base
attr_reader :ua

def initialize(ua, accept_language: nil)
validate_size(:user_agent, ua.to_s)
validate_size(:user_agent, ua.to_s, Browser.user_agent_size_limit)

@ua = ua
@accept_language_raw = accept_language.to_s
Expand All @@ -21,7 +21,11 @@ def meta
# Return an array with all preferred languages that this browser accepts.
def accept_language
@accept_language ||= begin
validate_size(:accept_language, @accept_language_raw)
validate_size(
:accept_language,
@accept_language_raw,
Browser.accept_language_size_limit
)
AcceptLanguage.parse(@accept_language_raw)
end
end
Expand All @@ -44,7 +48,7 @@ def platform

# Return the bot info.
def bot
@bot ||= Bot.new(ua)
@bot ||= Bot.new(ua, browser: self)
end

# Detect if current user agent is from a bot.
Expand Down Expand Up @@ -273,9 +277,8 @@ def epiphany?(expected_version = nil)
instance_of?(Epiphany) && detect_version?(full_version, expected_version)
end

private def validate_size(subject, input)
private def validate_size(subject, input, size_limit)
actual_bytesize = input.bytesize
size_limit = Browser.public_send(:"#{subject}_size_limit")

return if actual_bytesize < size_limit

Expand Down
4 changes: 2 additions & 2 deletions lib/browser/bot.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,9 @@ def self.why?(ua)

attr_reader :ua, :browser

def initialize(ua)
def initialize(ua, browser: nil)
@ua = ua.downcase.strip
@browser = Browser.new(@ua)
@browser = browser || Browser.new(@ua)
end

def bot?
Expand Down
14 changes: 14 additions & 0 deletions test/unit/bots_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,20 @@ class BotsTest < Minitest::Test
assert_equal "Generic Bot", browser.bot.name
end

test "passes the detected browser to custom bot matchers" do
matched_browsers = []
Browser::Bot.matchers << lambda {|_ua, browser|
matched_browsers << browser
false
}
browser = Browser.new(Browser["CHROME"])

refute browser.bot?
assert_same browser, matched_browsers.last
ensure
Browser::Bot.matchers.pop
end

%w[
content-fetcher
content-crawler
Expand Down
Loading