Skip to content

Commit

Permalink
Delay parsing AcceptLanguage until Browser::Base#accept_language is…
Browse files Browse the repository at this point in the history
… first called (#499)

Co-authored-by: Keiji Yoshimi <walf443@gmail.com>
  • Loading branch information
fnando and walf443 authored Oct 2, 2020
1 parent acf6a74 commit 98ea6d9
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 7 deletions.
7 changes: 6 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# Changelog

## Unreleased

- Delay parsing `Accept-Language` until `Browser::Base#accept_language` is
called for the first time.

## 5.0.0

- Rename `Browser::Platform#other?` to `Browser::Platform#unknown?`.
Expand All @@ -18,7 +23,7 @@
- Fix QQ detection.
- Fix Alipay detection.
- Add Sougou Browser detection.
- User agent has a size limit of 512 bytes. This can be customized through
- User agent has a size limit of 512 bytes. This can be customized through
`Browser.user_agent_size_limit`.
- Accept-Language has a size limit of 256 bytes. This can be customized through
`Browser.accept_language_size_limit`.
Expand Down
14 changes: 9 additions & 5 deletions lib/browser/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,26 @@ class Base

attr_reader :ua

# Return an array with all preferred languages that this browser accepts.
attr_reader :accept_language

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

@ua = ua
@accept_language = AcceptLanguage.parse(accept_language)
@accept_language_raw = accept_language.to_s
end

# Return a meta info about this browser.
def meta
Meta.get(self)
end

# Return an array with all preferred languages that this browser accepts.
def accept_language
@accept_language ||= begin
validate_size(:accept_language, @accept_language_raw)
AcceptLanguage.parse(@accept_language_raw)
end
end

alias_method :to_a, :meta

# Return meta representation as string.
Expand Down
2 changes: 1 addition & 1 deletion test/browser_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ class BrowserTest < Minitest::Test
"is 257 bytes"

assert_raises(Browser::Error, message) do
Browser.new("Chrome", accept_language: "a" * 257)
Browser.new("Chrome", accept_language: "a" * 257).accept_language
end
end
end

0 comments on commit 98ea6d9

Please sign in to comment.