agentry 3.1.2 → 4.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (115) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +20 -1
  3. data/README.md +97 -75
  4. data/lib/{browser → agentry}/accept_language.rb +2 -2
  5. data/lib/agentry/action_controller.rb +27 -0
  6. data/lib/agentry/agentry.rb +213 -0
  7. data/lib/agentry/aliases.rb +33 -0
  8. data/lib/{browser → agentry}/alipay.rb +1 -1
  9. data/lib/{browser → agentry}/base.rb +3 -3
  10. data/lib/{browser → agentry}/blackberry.rb +1 -1
  11. data/lib/{browser → agentry}/bot/empty_user_agent_matcher.rb +1 -1
  12. data/lib/{browser → agentry}/bot/ip_ranges.rb +3 -3
  13. data/lib/{browser → agentry}/bot/keyword_matcher.rb +1 -1
  14. data/lib/{browser → agentry}/bot/known_bots_matcher.rb +2 -2
  15. data/lib/{browser → agentry}/bot.rb +9 -9
  16. data/lib/agentry/browser_constant.rb +15 -0
  17. data/lib/{browser → agentry}/cache.rb +6 -6
  18. data/lib/{browser → agentry}/chrome.rb +1 -1
  19. data/lib/{browser → agentry}/client_hints.rb +1 -1
  20. data/lib/{browser → agentry}/cloudflare.rb +2 -2
  21. data/lib/{browser → agentry}/detect_version.rb +1 -1
  22. data/lib/{browser → agentry}/detection.rb +1 -1
  23. data/lib/{browser → agentry}/device/android.rb +1 -1
  24. data/lib/{browser → agentry}/device/base.rb +1 -1
  25. data/lib/{browser → agentry}/device/blackberry_playbook.rb +1 -1
  26. data/lib/{browser → agentry}/device/ipad.rb +1 -1
  27. data/lib/{browser → agentry}/device/iphone.rb +1 -1
  28. data/lib/{browser → agentry}/device/ipod_touch.rb +1 -1
  29. data/lib/{browser → agentry}/device/kindle.rb +1 -1
  30. data/lib/{browser → agentry}/device/kindle_fire.rb +1 -1
  31. data/lib/{browser → agentry}/device/playstation3.rb +1 -1
  32. data/lib/{browser → agentry}/device/playstation4.rb +1 -1
  33. data/lib/{browser → agentry}/device/psp.rb +1 -1
  34. data/lib/{browser → agentry}/device/psvita.rb +1 -1
  35. data/lib/{browser → agentry}/device/samsung.rb +2 -2
  36. data/lib/{browser → agentry}/device/surface.rb +1 -1
  37. data/lib/{browser → agentry}/device/switch.rb +1 -1
  38. data/lib/{browser → agentry}/device/tv.rb +1 -1
  39. data/lib/{browser → agentry}/device/unknown.rb +1 -1
  40. data/lib/{browser → agentry}/device/wii.rb +1 -1
  41. data/lib/{browser → agentry}/device/wiiu.rb +1 -1
  42. data/lib/{browser → agentry}/device/xbox_360.rb +1 -1
  43. data/lib/{browser → agentry}/device/xbox_one.rb +1 -1
  44. data/lib/{browser → agentry}/device.rb +1 -1
  45. data/lib/{browser → agentry}/duck_duck_go.rb +1 -1
  46. data/lib/{browser → agentry}/edge.rb +1 -1
  47. data/lib/{browser → agentry}/electron.rb +1 -1
  48. data/lib/{browser → agentry}/epiphany.rb +1 -1
  49. data/lib/{browser → agentry}/facebook.rb +1 -1
  50. data/lib/{browser → agentry}/firefox.rb +1 -1
  51. data/lib/{browser → agentry}/google_search_app.rb +1 -1
  52. data/lib/{browser → agentry}/huawei_browser.rb +1 -1
  53. data/lib/{browser → agentry}/instagram.rb +1 -1
  54. data/lib/{browser → agentry}/internet_explorer.rb +1 -1
  55. data/lib/{browser → agentry}/maxthon.rb +1 -1
  56. data/lib/{browser → agentry}/meta/base.rb +1 -1
  57. data/lib/{browser → agentry}/meta/device.rb +1 -1
  58. data/lib/{browser → agentry}/meta/generic_browser.rb +1 -1
  59. data/lib/{browser → agentry}/meta/id.rb +1 -1
  60. data/lib/{browser → agentry}/meta/ie.rb +1 -1
  61. data/lib/{browser → agentry}/meta/ios.rb +1 -1
  62. data/lib/{browser → agentry}/meta/mobile.rb +1 -1
  63. data/lib/{browser → agentry}/meta/platform.rb +1 -1
  64. data/lib/{browser → agentry}/meta/proxy.rb +1 -1
  65. data/lib/{browser → agentry}/meta/safari.rb +1 -1
  66. data/lib/{browser → agentry}/meta/tablet.rb +1 -1
  67. data/lib/{browser → agentry}/meta/webkit.rb +1 -1
  68. data/lib/{browser → agentry}/micro_messenger.rb +1 -1
  69. data/lib/{browser → agentry}/middleware/context/additions.rb +4 -2
  70. data/lib/agentry/middleware/context/url_methods.rb +17 -0
  71. data/lib/{browser → agentry}/middleware/context.rb +9 -3
  72. data/lib/{browser → agentry}/middleware.rb +2 -2
  73. data/lib/{browser → agentry}/miui_browser.rb +1 -1
  74. data/lib/{browser → agentry}/nokia.rb +1 -1
  75. data/lib/{browser → agentry}/opera.rb +1 -1
  76. data/lib/{browser → agentry}/otter.rb +1 -1
  77. data/lib/{browser → agentry}/phantom_js.rb +1 -1
  78. data/lib/{browser → agentry}/platform/adobe_air.rb +1 -1
  79. data/lib/{browser → agentry}/platform/android.rb +1 -1
  80. data/lib/{browser → agentry}/platform/base.rb +1 -1
  81. data/lib/{browser → agentry}/platform/blackberry.rb +1 -1
  82. data/lib/{browser → agentry}/platform/chrome_os.rb +1 -1
  83. data/lib/{browser → agentry}/platform/firefox_os.rb +1 -1
  84. data/lib/{browser → agentry}/platform/ios.rb +1 -1
  85. data/lib/{browser → agentry}/platform/kai_os.rb +1 -1
  86. data/lib/{browser → agentry}/platform/linux.rb +1 -1
  87. data/lib/{browser → agentry}/platform/mac.rb +1 -1
  88. data/lib/{browser → agentry}/platform/unknown.rb +1 -1
  89. data/lib/{browser → agentry}/platform/windows.rb +1 -1
  90. data/lib/{browser → agentry}/platform/windows_mobile.rb +1 -1
  91. data/lib/{browser → agentry}/platform/windows_phone.rb +1 -1
  92. data/lib/{browser → agentry}/platform.rb +1 -1
  93. data/lib/{browser → agentry}/qq.rb +1 -1
  94. data/lib/agentry/rails.rb +44 -0
  95. data/lib/{browser → agentry}/safari.rb +1 -1
  96. data/lib/{browser → agentry}/samsung_browser.rb +1 -1
  97. data/lib/{browser → agentry}/snapchat.rb +1 -1
  98. data/lib/{browser → agentry}/sougou_browser.rb +1 -1
  99. data/lib/{browser → agentry}/sputnik.rb +1 -1
  100. data/lib/agentry/testing.rb +30 -0
  101. data/lib/{browser → agentry}/uc_browser.rb +1 -1
  102. data/lib/{browser → agentry}/unknown.rb +1 -1
  103. data/lib/{browser → agentry}/version.rb +2 -2
  104. data/lib/{browser → agentry}/weibo.rb +1 -1
  105. data/lib/{browser → agentry}/yandex.rb +1 -1
  106. data/lib/agentry.rb +2 -1
  107. data/lib/browser/aliases.rb +2 -31
  108. data/lib/browser/browser.rb +2 -202
  109. data/lib/browser/rails.rb +3 -22
  110. data/lib/browser/testing.rb +3 -27
  111. data/lib/browser.rb +2 -2
  112. metadata +272 -252
  113. data/lib/browser/action_controller.rb +0 -21
  114. data/lib/browser/middleware/context/url_methods.rb +0 -15
  115. /data/lib/{browser → agentry}/meta.rb +0 -0
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 1bf4b8d8d170a4609f598c575de484e46cfa10aada6d51e3b38f37d56062caa0
4
- data.tar.gz: 621944def943117bda2d0365ea7ae4c265d0f9afc8afbca12c3aecbb2ce5fd2a
3
+ metadata.gz: d937115d978eb57830eb871a9761f43d25c138837dcaa23b8d5cf693589e1743
4
+ data.tar.gz: c26abad73e0ae27829c345959b6ba510b805de7fc48a85f72ec667c1c3948ab9
5
5
  SHA512:
6
- metadata.gz: 21fbac309c93dd1ff79dd34d98bda6c55306542c984e2c2eb7dc6fe34787f65d4f6a358f7b084a85ca8d7e032138225fa35c723883014eda8ad387fcef42c455
7
- data.tar.gz: 483bd020992d126d9b81e89ea5019e5caed2d493106fd520ba701b68af849eb249ccd740d1c2f20f5899d0828acb7689ec5d0ea606490965c0b59f1e63a4f8bc
6
+ metadata.gz: d2b50fffa940bfe956aeae286ebea5ae434e95a5ebcb80960d03e145a18884451e3e2c31e4b92c247c7d1221ea777745b3f97b14a9971beb347568b705b42249
7
+ data.tar.gz: 5528d469ac3126e176ed2fd12e333133901f563b4dad77415611b801a6662b5f13f146859e407f65edfe3e6f3107daab57a777237880b3b4e6788dcfd7cdcb87
data/CHANGELOG.md CHANGED
@@ -2,6 +2,24 @@
2
2
 
3
3
  ## [Unreleased]
4
4
 
5
+ ## [4.0.0] — 2026-11-22
6
+
7
+ Agentry answers under its own name: `Agentry` instead of `Browser`, browser's names kept as deprecated aliases until 5.0.
8
+
9
+ **Breaking**
10
+ - `require "agentry"` (a bare `gem "agentry"`) no longer defines `Browser`. To keep the old names a while longer, write `gem "agentry", require: "browser"`, or keep `require: "browser/browser"`. The [migration guide](https://levups.github.io/agentry/migration.html) lists every rename and gives a prompt for a coding agent.
11
+ - Of the `browser/*` require paths, only `browser`, `browser/browser`, `browser/aliases`, `browser/testing` and `browser/rails` remain; others such as `browser/middleware` or `browser/version` raise `LoadError`.
12
+ - Class names read `Agentry::Chrome`, `Agentry::Bot`…, even where code calls them through `Browser`, and the Railtie initializer is named `"agentry"`.
13
+ - Agentry 4 refuses to load its `browser/*` paths next to the browser gem, since both define `Browser`: a bundle can't hold both until 5.0.
14
+
15
+ **New**
16
+ - `Agentry.new`, `Agentry::Bot`, `Agentry::Middleware`…, `require "agentry"`, the `agentry` helper in controllers, views and middleware blocks, and `config.agentry`.
17
+ - browser's names (`Browser`, the `browser` helper, `browser` in a middleware block, `config.browser`) still work and warn once each under `RUBYOPT=-W:deprecated`.
18
+ - `require "agentry/testing"` and `require "agentry/rails"` work on their own.
19
+
20
+ **Fixed**
21
+ - `config.agentry.default_url_options` reaches the url helpers of middleware blocks, which no longer raise when the app has no Action Mailer.
22
+
5
23
  ## [3.1.2] — 2026-11-22
6
24
 
7
25
  Six more bots recognized, and fresh IP ranges for verified Google, Bing and OpenAI crawlers.
@@ -124,7 +142,8 @@ First release, forked from browser 6.2.0 (`dd88933`). Same API: `gem "agentry"`
124
142
  - A corpus of 20,328 user agents, with snapshot and diff scripts proving answers match upstream, and a
125
143
  request benchmark.
126
144
 
127
- [Unreleased]: https://github.com/levups/agentry/compare/v3.1.2...HEAD
145
+ [Unreleased]: https://github.com/levups/agentry/compare/v4.0.0...HEAD
146
+ [4.0.0]: https://github.com/levups/agentry/compare/v3.1.2...v4.0.0
128
147
  [3.1.2]: https://github.com/levups/agentry/compare/v3.1.1...v3.1.2
129
148
  [3.1.1]: https://github.com/levups/agentry/compare/v3.1.0...v3.1.1
130
149
  [3.1.0]: https://github.com/levups/agentry/compare/v3.0.0...v3.1.0
data/README.md CHANGED
@@ -4,19 +4,20 @@ Agentry is a Ruby gem that tells you who sent an HTTP request: which browser and
4
4
  and device, or which bot. It reads the `User-Agent` header, a line of text every browser and crawler sends with each
5
5
  request, such as `Mozilla/5.0 (iPhone; CPU iPhone OS 18_1 like Mac OS X) … Version/18.1 Mobile/15E148
6
6
  Safari/604.1`. Each browser and crawler writes it in its own way, and Agentry knows the formats so that your code
7
- can ask plain questions such as `browser.bot?` or `browser.device.mobile?`.
7
+ can ask plain questions such as `agentry.bot?` or `agentry.device.mobile?`.
8
8
 
9
9
  Apps use the answers to leave bots out of their analytics, to turn crawlers away, to adapt a page to phones, or to
10
- warn visitors on an old browser. Agentry works in any Ruby program, and gives Rails controllers and views a
11
- `browser` helper for the current request.
10
+ warn visitors on an old browser. Agentry works in any Ruby program, and gives Rails controllers and views an
11
+ `agentry` helper for the current request.
12
12
 
13
13
  Website and guide: https://levups.github.io/agentry/
14
14
 
15
15
  ## Why Agentry
16
16
 
17
17
  Agentry is a fork of [browser](https://github.com/fnando/browser), the gem Nando Vieira wrote, which has not changed
18
- since June 2025. Code written for browser runs on Agentry as it is: same `Browser` class, same `require "browser"`,
19
- same methods. Here is what is different:
18
+ since June 2025. It has the same methods under its own name, `Agentry` instead of `Browser`, and Agentry 4 still
19
+ answers to the old names, so code written for browser runs on it as it is (see
20
+ [Switching from browser](#switching-from-browser)). Here is what is different:
20
21
 
21
22
  - It answers in 25 µs per request instead of 126 µs with browser 6.2.0, and in 2 µs for a user agent the process
22
23
  has seen before (Ruby 4.0.9 with YJIT, see [Performance](#performance)).
@@ -38,60 +39,58 @@ Agentry needs Ruby 3.3 or later. It is tested on Rails 8.0 and 8.1, and works wi
38
39
  gem "agentry"
39
40
  ```
40
41
 
41
- If the Gemfile already has `gem "browser"`, replace that line: both gems define the `Browser` class, so Agentry
42
- refuses to load next to browser. Nothing else in your code changes, though some answers do (see
43
- [Switching from browser](#switching-from-browser)).
42
+ If the Gemfile already has `gem "browser"`, see [Switching from browser](#switching-from-browser).
44
43
 
45
44
  ## A first example
46
45
 
47
46
  ```ruby
48
- require "browser"
47
+ require "agentry"
49
48
 
50
49
  user_agent = "Mozilla/5.0 (iPhone; CPU iPhone OS 18_1 like Mac OS X) AppleWebKit/605.1.15 " \
51
50
  "(KHTML, like Gecko) Version/18.1 Mobile/15E148 Safari/604.1"
52
- browser = Browser.new(user_agent, accept_language: "fr-FR,fr;q=0.9,en;q=0.8")
53
-
54
- browser.name # => "Safari"
55
- browser.version # => "18"
56
- browser.full_version # => "18.1"
57
- browser.platform.name # => "iOS (iPhone)"
58
- browser.device.mobile? # => true
59
- browser.bot? # => false
60
- browser.accept_language.first.full # => "fr-FR"
51
+ agentry = Agentry.new(user_agent, accept_language: "fr-FR,fr;q=0.9,en;q=0.8")
52
+
53
+ agentry.name # => "Safari"
54
+ agentry.version # => "18"
55
+ agentry.full_version # => "18.1"
56
+ agentry.platform.name # => "iOS (iPhone)"
57
+ agentry.device.mobile? # => true
58
+ agentry.bot? # => false
59
+ agentry.accept_language.first.full # => "fr-FR"
61
60
  ```
62
61
 
63
- The file to require is `browser` and the class is `Browser`, the names browser used. `accept_language:` is optional:
64
- it takes the `Accept-Language` header, the languages the visitor's browser asks for.
62
+ `accept_language:` is optional: it takes the `Accept-Language` header, the languages the visitor's browser asks
63
+ for.
65
64
 
66
- In Rails there is nothing to build. The `browser` helper reads the current request's headers:
65
+ In Rails there is nothing to build. The `agentry` helper reads the current request's headers:
67
66
 
68
67
  ```erb
69
- <% if browser.device.mobile? %>
68
+ <% if agentry.device.mobile? %>
70
69
  <%= render "mobile_menu" %>
71
70
  <% end %>
72
71
  ```
73
72
 
74
73
  Outside Rails, pass the Rack env as `headers:` so that Agentry can read Client Hints and Cloudflare headers too:
75
- `Browser.new(user_agent, accept_language: language, headers: env)`.
74
+ `Agentry.new(user_agent, accept_language: language, headers: env)`.
76
75
 
77
76
  The [guide](https://levups.github.io/agentry/guide.html#usage) lists every method: browsers from Chrome and Safari
78
77
  to WeChat and Yandex, devices such as the iPad, Kindle, PlayStation and Xbox, and platforms down to each version of
79
- Windows. Version checks take a number or constraints, such as `browser.chrome?(">= 65")` or
80
- `browser.platform.ios?(9)`.
78
+ Windows. Version checks take a number or constraints, such as `agentry.chrome?(">= 65")` or
79
+ `agentry.platform.ios?(9)`.
81
80
 
82
81
  ## Bots
83
82
 
84
83
  ```ruby
85
- browser = Browser.new("Mozilla/5.0 (compatible; GPTBot/1.2; +https://openai.com/gptbot)")
84
+ agentry = Agentry.new("Mozilla/5.0 (compatible; GPTBot/1.2; +https://openai.com/gptbot)")
86
85
 
87
- browser.bot? # => true
88
- browser.bot.name # => "ChatGPT"
89
- browser.bot.category # => :ai_crawler
90
- browser.bot.ai? # => true
91
- browser.bot.user_triggered? # => false
86
+ agentry.bot? # => true
87
+ agentry.bot.name # => "ChatGPT"
88
+ agentry.bot.category # => :ai_crawler
89
+ agentry.bot.ai? # => true
90
+ agentry.bot.user_triggered? # => false
92
91
  ```
93
92
 
94
- `browser.bot?` is true for an empty user agent, for one that matches `/crawl|fetch|search|monitoring|spider|bot/`,
93
+ `agentry.bot?` is true for an empty user agent, for one that matches `/crawl|fetch|search|monitoring|spider|bot/`,
95
94
  and for one that contains a name from [bots.yml](bots.yml). The net is wide on purpose, as it was in browser.
96
95
 
97
96
  Every Monday, a workflow adds the bots of [crawler-user-agents](https://github.com/monperrus/crawler-user-agents)
@@ -102,7 +101,7 @@ browser and Agentry detect.
102
101
 
103
102
  ### Categories
104
103
 
105
- `browser.bot.category` is what the bot says it is, named after Cloudflare's verified bot categories where one fits:
104
+ `agentry.bot.category` is what the bot says it is, named after Cloudflare's verified bot categories where one fits:
106
105
  `search_engine_crawler`, `ai_crawler`, `ai_search`, `ai_assistant`, `page_preview`, `feed_fetcher`,
107
106
  `monitoring_and_analytics`, `advertising_and_marketing`, `archiver`, `security` and a few more. Agentry adds
108
107
  `ai_agent`, `http_library`, `scanner` and `browser_automation`. `bot.ai?` is true for the four AI categories, and
@@ -112,7 +111,7 @@ ChatGPT-User or Claude-User. The category is `nil` for a browser, for a bot exce
112
111
  HTTP libraries (`curl`, `python-requests`) and scanners (sqlmap, Nikto) are bots. Test drivers (headless Chrome,
113
112
  Playwright, Puppeteer, Selenium) are not: your own system tests drive a real browser through them, and would
114
113
  otherwise count as bots. They get the `browser_automation` category, so production code can still catch them with
115
- `browser.bot? || browser.bot.category == :browser_automation`. PhantomJS stays a bot, as in browser.
114
+ `agentry.bot? || agentry.bot.category == :browser_automation`. PhantomJS stays a bot, as in browser.
116
115
 
117
116
  ### Your own rules
118
117
 
@@ -120,11 +119,11 @@ A matcher is anything that responds to `call(ua, browser)`. This one makes every
120
119
  `externalhit` a bot:
121
120
 
122
121
  ```ruby
123
- Browser::Bot.matchers << ->(ua, _browser) { ua.match?(/externalhit/i) }
122
+ Agentry::Bot.matchers << ->(ua, _browser) { ua.match?(/externalhit/i) }
124
123
  ```
125
124
 
126
- `browser.bot.why?` returns the matcher that fired. The lists can change at runtime too
127
- (`Browser::Bot.bots.merge!(new_bots_hash)`), and the
125
+ `agentry.bot.why?` returns the matcher that fired. The lists can change at runtime too
126
+ (`Agentry::Bot.bots.merge!(new_bots_hash)`), and the
128
127
  [Bots page](https://levups.github.io/agentry/bots.html#matchers) shows how to remove the built-in matchers or go
129
128
  back to the detection of browser 2.x.
130
129
 
@@ -135,10 +134,10 @@ IP ranges their crawlers use, and Agentry ships them, refreshed by the weekly sy
135
134
  a request comes from the ranges of the company its user agent names:
136
135
 
137
136
  ```ruby
138
- browser.bot.owner # => :google for a user agent claiming to be Googlebot
139
- browser.bot.verified?(request.remote_ip) # => true when the IP is in Google's ranges
137
+ agentry.bot.owner # => :google for a user agent claiming to be Googlebot
138
+ agentry.bot.verified?(request.remote_ip) # => true when the IP is in Google's ranges
140
139
 
141
- if browser.bot.owner && !browser.bot.verified?(request.remote_ip)
140
+ if agentry.bot.owner && !agentry.bot.verified?(request.remote_ip)
142
141
  head :forbidden # claims to be a known crawler, comes from somewhere else
143
142
  end
144
143
  ```
@@ -152,10 +151,10 @@ for the timeout (1 second per nameserver of `/etc/resolv.conf`), so a cache, off
152
151
  answer, failures included:
153
152
 
154
153
  ```ruby
155
- Browser::Bot::IPRanges.dns_cache_size = 10_000 # 0, the default, caches nothing
156
- Browser::Bot::IPRanges.dns_cache_ttl = 3600 # seconds
157
- Browser::Bot::IPRanges.dns_timeout = 0.5 # seconds per query and nameserver
158
- Browser::Bot::IPRanges.refresh # fetches the ranges again, in a long-lived process
154
+ Agentry::Bot::IPRanges.dns_cache_size = 10_000 # 0, the default, caches nothing
155
+ Agentry::Bot::IPRanges.dns_cache_ttl = 3600 # seconds
156
+ Agentry::Bot::IPRanges.dns_timeout = 0.5 # seconds per query and nameserver
157
+ Agentry::Bot::IPRanges.refresh # fetches the ranges again, in a long-lived process
159
158
  ```
160
159
 
161
160
  The reverse lookup reads answers from a name server the requester chooses, and versions of the `resolv` gem before
@@ -169,33 +168,33 @@ When the Rack env is passed as `headers:` (the Rails helper and the middleware d
169
168
  more sources.
170
169
 
171
170
  Behind Cloudflare, a Transform Rule can copy what Cloudflare knows about a request into headers, and
172
- `browser.cloudflare` reads them:
171
+ `agentry.cloudflare` reads them:
173
172
 
174
173
  ```ruby
175
- browser.cloudflare.verified_bot? # => true for a bot Cloudflare verified
176
- browser.cloudflare.bot_category # => :search_engine_crawler, with the names of bot.category
177
- browser.cloudflare.bot_score # => 1 (automated) to 99 (human), with Bot Management only
178
- browser.cloudflare.country # => "FR", from CF-IPCountry
174
+ agentry.cloudflare.verified_bot? # => true for a bot Cloudflare verified
175
+ agentry.cloudflare.bot_category # => :search_engine_crawler, with the names of bot.category
176
+ agentry.cloudflare.bot_score # => 1 (automated) to 99 (human), with Bot Management only
177
+ agentry.cloudflare.country # => "FR", from CF-IPCountry
179
178
  ```
180
179
 
181
- The header names can be changed with `Browser::Cloudflare.header_names = {bot_score: "X-My-Score"}`. These headers
180
+ The header names can be changed with `Agentry::Cloudflare.header_names = {bot_score: "X-My-Score"}`. These headers
182
181
  prove something only when every request goes through Cloudflare: if clients can reach your server directly, they
183
182
  can send the headers themselves. The [guide](https://levups.github.io/agentry/guide.html#cloudflare) has the rule to
184
183
  create and how to lock the origin.
185
184
 
186
185
  Chromium browsers no longer put the real Windows, macOS or Android version in their user agent. They send it in
187
186
  Client Hints (`Sec-CH-UA`, `Sec-CH-UA-Mobile`, `Sec-CH-UA-Platform`, `Sec-CH-UA-Platform-Version`), which Agentry
188
- uses to refine the platform and the device: `browser.platform.windows11?` needs them. Without `headers:`, every
187
+ uses to refine the platform and the device: `agentry.platform.windows11?` needs them. Without `headers:`, every
189
188
  answer comes from the user agent alone, as in browser. The
190
189
  [guide](https://levups.github.io/agentry/guide.html#client-hints) shows how to ask browsers for the platform
191
190
  version. `bench/signals.rb` measures what reading these headers costs.
192
191
 
193
192
  ## Accept-Language
194
193
 
195
- `browser.accept_language` turns the `Accept-Language` header into a list of languages, the most wanted first:
194
+ `agentry.accept_language` turns the `Accept-Language` header into a list of languages, the most wanted first:
196
195
 
197
196
  ```ruby
198
- language = Browser.new(user_agent, accept_language: "en-us").accept_language.first
197
+ language = Agentry.new(user_agent, accept_language: "en-us").accept_language.first
199
198
 
200
199
  language.code # => "en"
201
200
  language.region # => "US"
@@ -209,57 +208,80 @@ accepted, so it is left out.
209
208
 
210
209
  ## Middleware
211
210
 
212
- `Browser::Middleware` redirects requests based on the browser, before they reach your app. In Rails, from an
213
- initializer such as `config/initializers/browser.rb`, route helpers work and `request` is the `Rack::Request`:
211
+ `Agentry::Middleware` redirects requests based on the browser, before they reach your app. In Rails, from an
212
+ initializer such as `config/initializers/agentry.rb`, route helpers work and `request` is the `Rack::Request`:
214
213
 
215
214
  ```ruby
216
- Rails.configuration.middleware.use Browser::Middleware do
217
- redirect_to upgrade_path if browser.ie? && request.env["PATH_INFO"] != "/exclude_me"
215
+ Rails.configuration.middleware.use Agentry::Middleware do
216
+ redirect_to upgrade_path if agentry.ie? && request.env["PATH_INFO"] != "/exclude_me"
218
217
  end
219
218
  ```
220
219
 
221
- In another Rack app, `use Browser::Middleware do … end` takes the same block. To use Agentry in Rails without the
222
- `browser` helper, require only the library: `gem "agentry", require: "browser/browser"`.
220
+ In another Rack app, `use Agentry::Middleware do … end` takes the same block. To use Agentry in Rails without the
221
+ `agentry` helper, require only the library: `gem "agentry", require: "agentry/agentry"`.
223
222
 
224
223
  ## Cache, limits and boot
225
224
 
226
225
  Each process remembers what it found for the last 1,000 user agents (browser, platform, device and bot answers)
227
226
  and the last 1,000 `Accept-Language` headers, about 1 KB per user agent. A user agent seen before costs about 2 µs.
228
- The cache empties itself when it is full, and when `Browser.matchers`, `Browser::Bot.matchers`,
229
- `Browser::Platform.matchers`, `Browser::Device.matchers`, the bot list or its exceptions change. Setting
230
- `Browser.cache_size` empties it too, for example after renaming an entry of `Browser::Bot.bots` in place:
227
+ The cache empties itself when it is full, and when `Agentry.matchers`, `Agentry::Bot.matchers`,
228
+ `Agentry::Platform.matchers`, `Agentry::Device.matchers`, the bot list or its exceptions change. Setting
229
+ `Agentry.cache_size` empties it too, for example after renaming an entry of `Agentry::Bot.bots` in place:
231
230
 
232
231
  ```ruby
233
- Browser.cache_size = 5_000 # 0 turns it off
232
+ Agentry.cache_size = 5_000 # 0 turns it off
234
233
  ```
235
234
 
236
- A user agent or an `Accept-Language` header longer than 2048 bytes raises `Browser::Error`. Both limits can be
237
- raised with `Browser.user_agent_size_limit = 4096` and `Browser.accept_language_size_limit = 4096`. An
235
+ A user agent or an `Accept-Language` header longer than 2048 bytes raises `Agentry::Error`. Both limits can be
236
+ raised with `Agentry.user_agent_size_limit = 4096` and `Agentry.accept_language_size_limit = 4096`. An
238
237
  `Accept-Language` header listing more than 32 languages is parsed on each request instead of cached, so that
239
238
  crafted headers cannot fill the cache with thousands of languages each.
240
239
 
241
240
  Headers whose bytes are not valid UTF-8, forged or read back from a log file, do not raise: Agentry reads the user
242
241
  agent and the `Accept-Language` header with each invalid byte replaced by U+FFFD, and ignores such a Client Hint or
243
- Cloudflare header. Version checks such as `browser.chrome?(">= 65")` leave the versions they read from a request
242
+ Cloudflare header. Version checks such as `agentry.chrome?(">= 65")` leave the versions they read from a request
244
243
  out of RubyGems' version cache, where those an attacker sends would pile up for the life of the process.
245
244
 
246
245
  The lists and matchers are built on first use: the first request of each process takes about 13 ms more, the
247
- following ones 0.2 ms. `Browser.eager_load!` builds them at boot, so that a server that loads the app before
246
+ following ones 0.2 ms. `Agentry.eager_load!` builds them at boot, so that a server that loads the app before
248
247
  forking (Puma's `preload_app!`, Unicorn, Pitchfork) shares them with every worker. Rails calls it whenever it eager
249
- loads, which production does by default. Elsewhere, call `Browser.eager_load!` before the fork.
248
+ loads, which production does by default. Elsewhere, call `Agentry.eager_load!` before the fork.
250
249
 
251
250
  ## Switching from browser
252
251
 
253
- Replace `gem "browser"` with `gem "agentry"` in the Gemfile. Nothing else in the code changes.
252
+ Replace `gem "browser"` with `gem "agentry", require: "browser"` in the Gemfile (keep `require: "browser/browser"`
253
+ if the app had it). Code written for browser keeps working on Agentry 4: the require paths browser documents
254
+ (`browser`, `browser/browser`, `browser/aliases`) and `browser/testing`, `browser/rails` still load and define
255
+ `Browser` as another name for `Agentry`, and the `browser` helper still answers. A bare `gem "agentry"` leaves out
256
+ the `Browser` constant, which then raises `NameError`. The old names are deprecated and go away in Agentry 5,
257
+ so move to the new ones when you can:
258
+
259
+ | browser, deprecated in Agentry 4 | Agentry |
260
+ |---|---|
261
+ | `gem "browser"`, `require "browser"` | `gem "agentry"`, `require "agentry"` |
262
+ | `require: "browser/browser"` (without Rails) | `require: "agentry/agentry"` |
263
+ | `require "browser/aliases"`, `"browser/testing"`, `"browser/rails"` | `require "agentry/aliases"`, `"agentry/testing"`, `"agentry/rails"` |
264
+ | `Browser.new(…)`, `Browser::Bot`, `Browser::Middleware`… | `Agentry.new(…)`, `Agentry::Bot`, `Agentry::Middleware`… |
265
+ | the `browser` helper, `browser` in a middleware block | `agentry` |
266
+ | `@browser`, in controllers, views, tests and middleware blocks (no warning) | `@agentry` |
267
+ | `config.browser` | `config.agentry` |
268
+
269
+ Each deprecation warns once per process, only where Ruby shows deprecations: run the tests with
270
+ `RUBYOPT=-W:deprecated` to list what is left to move. The
271
+ [migration guide](https://levups.github.io/agentry/migration.html) walks through the change, and gives a prompt
272
+ for a coding agent to make it. Class names change even where code keeps calling `Browser`: `browser.class.name`
273
+ reads `Agentry::Chrome`. Agentry 4 ships the same `browser/*` files as the browser gem, so it cannot share a
274
+ bundle with it, for example through a dependency that still needs browser; Agentry 5 drops those files.
254
275
 
255
276
  Some answers change from the day you switch, because of the fresher bot list: 600 user agents of the corpus that
256
277
  browser took for people are bots in Agentry, so analytics that leave bots out will count fewer visitors, and 792
257
278
  user agents that browser called `Generic Bot` get a real name. No browser, platform or device answer changes, and
258
279
  test drivers still are not bots.
259
280
 
260
- To go back, remove the calls to what browser does not have (`Browser.cache_size=`, the `headers:` argument,
261
- `bot.category`, `bot.ai?`, `bot.user_triggered?`, `bot.verified?`, `browser.cloudflare`), then put
262
- `gem "browser"` back. The [migration guide](https://levups.github.io/agentry/guide.html#migrating) has the details.
281
+ To go back, remove the calls to what browser does not have (`cache_size=`, the `headers:` argument,
282
+ `bot.category`, `bot.ai?`, `bot.user_triggered?`, `bot.verified?`, `cloudflare`), bring back the browser names of
283
+ the table above, then put `gem "browser"` back. The
284
+ [migration guide](https://levups.github.io/agentry/migration.html#going-back) has the details.
263
285
 
264
286
  ## Performance
265
287
 
@@ -285,9 +307,9 @@ numbers from that file.
285
307
 
286
308
  ## How the answers are checked
287
309
 
288
- Agentry runs browser's own test suite, unmodified, next to its own tests. The repository also holds a corpus of
289
- 20,328 user agents, gathered from uap-core, crawler-user-agents and browser's test fixtures. On every pull request,
290
- CI asks both browser and Agentry about each of them and compares the answers. An answer that differs is either
310
+ Agentry runs browser's own test suite, unmodified (it calls `Browser`, which Agentry 4 still answers to), next to
311
+ its own tests. The repository also holds a corpus of 20,328 user agents, gathered from uap-core,
312
+ crawler-user-agents and browser's test fixtures. On every pull request, CI asks both browser and Agentry about each of them and compares the answers. An answer that differs is either
291
313
  listed in `corpus/expected_differences.jsonl` as an intended change, such as a newly known bot, or fails the build.
292
314
 
293
315
  ## Development
@@ -1,9 +1,9 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- module Browser
3
+ module Agentry
4
4
  class AcceptLanguage
5
5
  def self.languages
6
- @languages ||= Browser.load_data("languages")
6
+ @languages ||= Agentry.load_data("languages")
7
7
  end
8
8
 
9
9
  def self.parse(accept_language)
@@ -0,0 +1,27 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "action_controller/railtie"
4
+
5
+ module Agentry
6
+ module ActionController
7
+ extend ActiveSupport::Concern
8
+
9
+ included do
10
+ helper_method(:agentry, :browser) if respond_to?(:helper_method)
11
+ end
12
+
13
+ private def agentry
14
+ @agentry ||= Agentry.new(
15
+ request.headers["User-Agent"],
16
+ accept_language: request.headers["Accept-Language"],
17
+ headers: request.env
18
+ )
19
+ end
20
+
21
+ # Fills and honours @browser, as browser's helper did: apps and tests set it to fake a device.
22
+ private def browser
23
+ Agentry.deprecate("the browser helper is deprecated and goes away in 5.0: call agentry")
24
+ @browser ||= agentry
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,213 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "json"
4
+ require "pathname"
5
+
6
+ require_relative "version"
7
+ require_relative "detect_version"
8
+ require_relative "accept_language"
9
+ require_relative "base"
10
+ require_relative "safari"
11
+ require_relative "chrome"
12
+ require_relative "internet_explorer"
13
+ require_relative "firefox"
14
+ require_relative "edge"
15
+ require_relative "opera"
16
+ require_relative "blackberry"
17
+ require_relative "unknown"
18
+ require_relative "phantom_js"
19
+ require_relative "uc_browser"
20
+ require_relative "nokia"
21
+ require_relative "micro_messenger"
22
+ require_relative "weibo"
23
+ require_relative "qq"
24
+ require_relative "alipay"
25
+ require_relative "electron"
26
+ require_relative "facebook"
27
+ require_relative "otter"
28
+ require_relative "instagram"
29
+ require_relative "yandex"
30
+ require_relative "sputnik"
31
+ require_relative "snapchat"
32
+ require_relative "duck_duck_go"
33
+ require_relative "samsung_browser"
34
+ require_relative "huawei_browser"
35
+ require_relative "miui_browser"
36
+ require_relative "maxthon"
37
+ require_relative "sougou_browser"
38
+ require_relative "google_search_app"
39
+ require_relative "epiphany"
40
+
41
+ require_relative "bot"
42
+ require_relative "bot/empty_user_agent_matcher"
43
+ require_relative "bot/keyword_matcher"
44
+ require_relative "bot/known_bots_matcher"
45
+ require_relative "bot/ip_ranges"
46
+
47
+ require_relative "middleware"
48
+ require_relative "platform"
49
+ require_relative "device"
50
+ require_relative "meta"
51
+ require_relative "client_hints"
52
+ require_relative "cloudflare"
53
+ require_relative "detection"
54
+ require_relative "cache"
55
+
56
+ module Agentry
57
+ EMPTY_STRING = ""
58
+
59
+ Bot::DEFAULT_MATCHERS =
60
+ [Bot::EmptyUserAgentMatcher, Bot::KnownBotsMatcher, Bot::KeywordMatcher]
61
+ .freeze
62
+
63
+ Error = Class.new(StandardError)
64
+
65
+ def self.root
66
+ @root ||= Pathname.new(File.expand_path("../..", __dir__))
67
+ end
68
+
69
+ MIGRATION_GUIDE_URL = "https://levups.github.io/agentry/migration.html"
70
+
71
+ @deprecations = Set.new
72
+ @deprecations_lock = Mutex.new
73
+
74
+ # Once per message, and only where Ruby shows deprecations (-W:deprecated, Warning[:deprecated] = true): a
75
+ # message hidden when it first fires still prints once deprecations are shown.
76
+ def self.deprecate(message)
77
+ return if $VERBOSE.nil? || !Warning[:deprecated]
78
+ return unless @deprecations_lock.synchronize { @deprecations.add?(message) }
79
+
80
+ warn("[agentry] #{message}, see #{MIGRATION_GUIDE_URL}", category: :deprecated)
81
+ end
82
+
83
+ # data/bots.json for "bots", written from bots.yml by script/build_data.rb: edit the YAML.
84
+ def self.load_data(name)
85
+ JSON.load_file(root.join("data", "#{name}.json"))
86
+ end
87
+
88
+ def self.eager_load!
89
+ [self, Platform, Device, Bot].each(&:matchers)
90
+ Meta.rules
91
+ AcceptLanguage.languages
92
+ Device::Samsung.names
93
+ Bot.known_bots_trie
94
+ [Bot.bot_exceptions, Bot.search_engines, Bot.category_by_keyword].each { |keywords| Bot.keywords_pattern(keywords) }
95
+ Bot::IPRanges.ranges_by_owner
96
+ nil
97
+ end
98
+
99
+ class << self
100
+ attr_accessor :user_agent_size_limit, :accept_language_size_limit
101
+ attr_reader :cache_size
102
+ end
103
+
104
+ self.user_agent_size_limit = 2048
105
+ self.accept_language_size_limit = 2048
106
+
107
+ def self.cache_size=(size)
108
+ unless size.is_a?(Integer) && size >= 0
109
+ raise ArgumentError, "cache_size must be an Integer >= 0, got #{size.inspect}"
110
+ end
111
+
112
+ @cache_size = size
113
+ Cache.clear
114
+ end
115
+
116
+ self.cache_size = 1000
117
+
118
+ # Hold the list of browser matchers.
119
+ # Order is important.
120
+ def self.matchers
121
+ @matchers ||= [
122
+ Nokia,
123
+ UCBrowser,
124
+ PhantomJS,
125
+ BlackBerry,
126
+ Opera,
127
+ Edge,
128
+ InternetExplorer,
129
+ Firefox,
130
+ Otter,
131
+ Epiphany,
132
+ Facebook,
133
+ Instagram,
134
+ Snapchat,
135
+ Weibo,
136
+ MicroMessenger, # must be placed before QQ
137
+ QQ,
138
+ Alipay,
139
+ Electron,
140
+ Yandex,
141
+ Sputnik,
142
+ DuckDuckGo,
143
+ SamsungBrowser,
144
+ HuaweiBrowser,
145
+ MiuiBrowser,
146
+ Maxthon,
147
+ SougouBrowser,
148
+ GoogleSearchApp,
149
+
150
+ # Must be last
151
+ Chrome,
152
+ Safari,
153
+ Unknown
154
+ ]
155
+ end
156
+
157
+ # A pattern that every user agent a built-in browser matches also matches:
158
+ # Agentry.new skips, without building it, a browser whose pattern fails.
159
+ REQUIRED_PATTERN_BY_MATCHER = {
160
+ Nokia => /S40OviBrowser/,
161
+ UCBrowser => /UCBrowser/,
162
+ PhantomJS => /PhantomJS/,
163
+ BlackBerry => /BlackBerry|BB10/,
164
+ Opera => %r{Opera|OPR/},
165
+ Edge => %r{Edg|Trident/8},
166
+ InternetExplorer => %r{MSIE|Trident/},
167
+ Firefox => /Firefox|FxiOS/,
168
+ Otter => /Otter/,
169
+ Epiphany => /Epiphany/,
170
+ Facebook => /FBAV|FBAN/,
171
+ Instagram => /Instagram/,
172
+ Snapchat => /Snapchat/,
173
+ Weibo => /__weibo__/i,
174
+ MicroMessenger => /MicroMessenger/i,
175
+ QQ => %r{QQ/|QQBrowser}i,
176
+ Alipay => /AlipayClient/i,
177
+ Electron => /Electron/,
178
+ Yandex => /YaBrowser/,
179
+ Sputnik => /SputnikBrowser/,
180
+ DuckDuckGo => /DuckDuckGo/,
181
+ SamsungBrowser => /SamsungBrowser/,
182
+ HuaweiBrowser => /HuaweiBrowser/i,
183
+ MiuiBrowser => /MiuiBrowser/,
184
+ Maxthon => /Maxthon/i,
185
+ SougouBrowser => /(?i:SogouMobileBrowser)|\bSE\b/,
186
+ GoogleSearchApp => /GSA/
187
+ }.compare_by_identity.freeze
188
+
189
+ def self.new(user_agent, **)
190
+ user_agent ||= EMPTY_STRING
191
+ # Before the first regexp: an oversized user agent then costs nothing, and
192
+ # a byte that is not UTF-8 (forged, or read from a log) would make it raise.
193
+ Base.validate_size(:user_agent, user_agent.to_s)
194
+ user_agent = user_agent.scrub unless user_agent.valid_encoding?
195
+ browser_class = Cache.cached_detection(user_agent)&.browser_class
196
+ return browser_class.new(user_agent, **) if browser_class
197
+
198
+ browser = detect(user_agent, **)
199
+ Cache.detection(user_agent).browser_class = browser.class
200
+ browser
201
+ end
202
+
203
+ def self.detect(user_agent, **)
204
+ matchers.each do |matcher_class|
205
+ required_pattern = REQUIRED_PATTERN_BY_MATCHER[matcher_class]
206
+ next if required_pattern && !required_pattern.match?(user_agent)
207
+
208
+ matcher = matcher_class.new(user_agent, **)
209
+ return matcher if matcher.match?
210
+ end
211
+ end
212
+ private_class_method :detect
213
+ end