agentry 3.1.1 → 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 (118) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +32 -1
  3. data/README.md +101 -79
  4. data/data/bot_categories.json +1 -1
  5. data/data/bot_ip_ranges.json +1 -1
  6. data/data/bots.json +1 -1
  7. data/lib/{browser → agentry}/accept_language.rb +2 -2
  8. data/lib/agentry/action_controller.rb +27 -0
  9. data/lib/agentry/agentry.rb +213 -0
  10. data/lib/agentry/aliases.rb +33 -0
  11. data/lib/{browser → agentry}/alipay.rb +1 -1
  12. data/lib/{browser → agentry}/base.rb +3 -3
  13. data/lib/{browser → agentry}/blackberry.rb +1 -1
  14. data/lib/{browser → agentry}/bot/empty_user_agent_matcher.rb +1 -1
  15. data/lib/{browser → agentry}/bot/ip_ranges.rb +3 -3
  16. data/lib/{browser → agentry}/bot/keyword_matcher.rb +1 -1
  17. data/lib/{browser → agentry}/bot/known_bots_matcher.rb +2 -2
  18. data/lib/{browser → agentry}/bot.rb +9 -9
  19. data/lib/agentry/browser_constant.rb +15 -0
  20. data/lib/{browser → agentry}/cache.rb +6 -6
  21. data/lib/{browser → agentry}/chrome.rb +1 -1
  22. data/lib/{browser → agentry}/client_hints.rb +1 -1
  23. data/lib/{browser → agentry}/cloudflare.rb +2 -2
  24. data/lib/{browser → agentry}/detect_version.rb +1 -1
  25. data/lib/{browser → agentry}/detection.rb +1 -1
  26. data/lib/{browser → agentry}/device/android.rb +1 -1
  27. data/lib/{browser → agentry}/device/base.rb +1 -1
  28. data/lib/{browser → agentry}/device/blackberry_playbook.rb +1 -1
  29. data/lib/{browser → agentry}/device/ipad.rb +1 -1
  30. data/lib/{browser → agentry}/device/iphone.rb +1 -1
  31. data/lib/{browser → agentry}/device/ipod_touch.rb +1 -1
  32. data/lib/{browser → agentry}/device/kindle.rb +1 -1
  33. data/lib/{browser → agentry}/device/kindle_fire.rb +1 -1
  34. data/lib/{browser → agentry}/device/playstation3.rb +1 -1
  35. data/lib/{browser → agentry}/device/playstation4.rb +1 -1
  36. data/lib/{browser → agentry}/device/psp.rb +1 -1
  37. data/lib/{browser → agentry}/device/psvita.rb +1 -1
  38. data/lib/{browser → agentry}/device/samsung.rb +2 -2
  39. data/lib/{browser → agentry}/device/surface.rb +1 -1
  40. data/lib/{browser → agentry}/device/switch.rb +1 -1
  41. data/lib/{browser → agentry}/device/tv.rb +1 -1
  42. data/lib/{browser → agentry}/device/unknown.rb +1 -1
  43. data/lib/{browser → agentry}/device/wii.rb +1 -1
  44. data/lib/{browser → agentry}/device/wiiu.rb +1 -1
  45. data/lib/{browser → agentry}/device/xbox_360.rb +1 -1
  46. data/lib/{browser → agentry}/device/xbox_one.rb +1 -1
  47. data/lib/{browser → agentry}/device.rb +1 -1
  48. data/lib/{browser → agentry}/duck_duck_go.rb +1 -1
  49. data/lib/{browser → agentry}/edge.rb +1 -1
  50. data/lib/{browser → agentry}/electron.rb +1 -1
  51. data/lib/{browser → agentry}/epiphany.rb +1 -1
  52. data/lib/{browser → agentry}/facebook.rb +1 -1
  53. data/lib/{browser → agentry}/firefox.rb +1 -1
  54. data/lib/{browser → agentry}/google_search_app.rb +1 -1
  55. data/lib/{browser → agentry}/huawei_browser.rb +1 -1
  56. data/lib/{browser → agentry}/instagram.rb +1 -1
  57. data/lib/{browser → agentry}/internet_explorer.rb +1 -1
  58. data/lib/{browser → agentry}/maxthon.rb +1 -1
  59. data/lib/{browser → agentry}/meta/base.rb +1 -1
  60. data/lib/{browser → agentry}/meta/device.rb +1 -1
  61. data/lib/{browser → agentry}/meta/generic_browser.rb +1 -1
  62. data/lib/{browser → agentry}/meta/id.rb +1 -1
  63. data/lib/{browser → agentry}/meta/ie.rb +1 -1
  64. data/lib/{browser → agentry}/meta/ios.rb +1 -1
  65. data/lib/{browser → agentry}/meta/mobile.rb +1 -1
  66. data/lib/{browser → agentry}/meta/platform.rb +1 -1
  67. data/lib/{browser → agentry}/meta/proxy.rb +1 -1
  68. data/lib/{browser → agentry}/meta/safari.rb +1 -1
  69. data/lib/{browser → agentry}/meta/tablet.rb +1 -1
  70. data/lib/{browser → agentry}/meta/webkit.rb +1 -1
  71. data/lib/{browser → agentry}/micro_messenger.rb +1 -1
  72. data/lib/{browser → agentry}/middleware/context/additions.rb +4 -2
  73. data/lib/agentry/middleware/context/url_methods.rb +17 -0
  74. data/lib/{browser → agentry}/middleware/context.rb +9 -3
  75. data/lib/{browser → agentry}/middleware.rb +2 -2
  76. data/lib/{browser → agentry}/miui_browser.rb +1 -1
  77. data/lib/{browser → agentry}/nokia.rb +1 -1
  78. data/lib/{browser → agentry}/opera.rb +1 -1
  79. data/lib/{browser → agentry}/otter.rb +1 -1
  80. data/lib/{browser → agentry}/phantom_js.rb +1 -1
  81. data/lib/{browser → agentry}/platform/adobe_air.rb +1 -1
  82. data/lib/{browser → agentry}/platform/android.rb +1 -1
  83. data/lib/{browser → agentry}/platform/base.rb +1 -1
  84. data/lib/{browser → agentry}/platform/blackberry.rb +1 -1
  85. data/lib/{browser → agentry}/platform/chrome_os.rb +1 -1
  86. data/lib/{browser → agentry}/platform/firefox_os.rb +1 -1
  87. data/lib/{browser → agentry}/platform/ios.rb +1 -1
  88. data/lib/{browser → agentry}/platform/kai_os.rb +1 -1
  89. data/lib/{browser → agentry}/platform/linux.rb +1 -1
  90. data/lib/{browser → agentry}/platform/mac.rb +1 -1
  91. data/lib/{browser → agentry}/platform/unknown.rb +1 -1
  92. data/lib/{browser → agentry}/platform/windows.rb +1 -1
  93. data/lib/{browser → agentry}/platform/windows_mobile.rb +1 -1
  94. data/lib/{browser → agentry}/platform/windows_phone.rb +1 -1
  95. data/lib/{browser → agentry}/platform.rb +1 -1
  96. data/lib/{browser → agentry}/qq.rb +1 -1
  97. data/lib/agentry/rails.rb +44 -0
  98. data/lib/{browser → agentry}/safari.rb +1 -1
  99. data/lib/{browser → agentry}/samsung_browser.rb +1 -1
  100. data/lib/{browser → agentry}/snapchat.rb +1 -1
  101. data/lib/{browser → agentry}/sougou_browser.rb +1 -1
  102. data/lib/{browser → agentry}/sputnik.rb +1 -1
  103. data/lib/agentry/testing.rb +30 -0
  104. data/lib/{browser → agentry}/uc_browser.rb +1 -1
  105. data/lib/{browser → agentry}/unknown.rb +1 -1
  106. data/lib/{browser → agentry}/version.rb +2 -2
  107. data/lib/{browser → agentry}/weibo.rb +1 -1
  108. data/lib/{browser → agentry}/yandex.rb +1 -1
  109. data/lib/agentry.rb +2 -1
  110. data/lib/browser/aliases.rb +2 -31
  111. data/lib/browser/browser.rb +2 -202
  112. data/lib/browser/rails.rb +3 -22
  113. data/lib/browser/testing.rb +3 -27
  114. data/lib/browser.rb +2 -2
  115. metadata +272 -252
  116. data/lib/browser/action_controller.rb +0 -21
  117. data/lib/browser/middleware/context/url_methods.rb +0 -15
  118. /data/lib/{browser → agentry}/meta.rb +0 -0
@@ -1,6 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- module Browser
3
+ module Agentry
4
4
  module Meta
5
5
  class IE < Base
6
6
  def version
@@ -1,6 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- module Browser
3
+ module Agentry
4
4
  module Meta
5
5
  class IOS < Base
6
6
  def meta
@@ -1,6 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- module Browser
3
+ module Agentry
4
4
  module Meta
5
5
  class Mobile < Base
6
6
  def meta
@@ -1,6 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- module Browser
3
+ module Agentry
4
4
  module Meta
5
5
  class Platform < Base
6
6
  def meta
@@ -1,6 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- module Browser
3
+ module Agentry
4
4
  module Meta
5
5
  class Proxy < Base
6
6
  def meta
@@ -1,6 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- module Browser
3
+ module Agentry
4
4
  module Meta
5
5
  class Safari < Base
6
6
  def meta
@@ -1,6 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- module Browser
3
+ module Agentry
4
4
  module Meta
5
5
  class Tablet < Base
6
6
  def meta
@@ -1,6 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- module Browser
3
+ module Agentry
4
4
  module Meta
5
5
  class Webkit < Base
6
6
  def meta
@@ -1,6 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- module Browser
3
+ module Agentry
4
4
  class MicroMessenger < Base
5
5
  def id
6
6
  :micro_messenger
@@ -2,7 +2,7 @@
2
2
 
3
3
  require_relative "url_methods"
4
4
 
5
- module Browser
5
+ module Agentry
6
6
  class Middleware
7
7
  class Context
8
8
  module Additions
@@ -10,7 +10,9 @@ module Browser
10
10
 
11
11
  included do
12
12
  include Rails.application.routes.url_helpers
13
- include UrlMethods
13
+ # url_helpers defines default_url_options on the class itself, which an
14
+ # included module cannot override.
15
+ prepend UrlMethods
14
16
  end
15
17
  end
16
18
  end
@@ -0,0 +1,17 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Agentry
4
+ class Middleware
5
+ class Context
6
+ module UrlMethods
7
+ def default_url_options
8
+ config = Rails.configuration
9
+
10
+ config.agentry.default_url_options ||
11
+ (config.action_mailer.default_url_options if config.respond_to?(:action_mailer)) ||
12
+ {}
13
+ end
14
+ end
15
+ end
16
+ end
17
+ end
@@ -1,18 +1,24 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- module Browser
3
+ module Agentry
4
4
  class Middleware
5
5
  class Context
6
- attr_reader :browser, :request
6
+ attr_reader :agentry, :request
7
7
 
8
8
  def initialize(request)
9
9
  @request = request
10
10
 
11
- @browser = Browser.new(
11
+ @agentry = Agentry.new(
12
12
  request.user_agent,
13
13
  accept_language: request.env["HTTP_ACCEPT_LANGUAGE"],
14
14
  headers: request.env
15
15
  )
16
+ @browser = @agentry # browser's name, read by middleware blocks written for it, until 5.0
17
+ end
18
+
19
+ def browser
20
+ Agentry.deprecate("browser in an Agentry::Middleware block is deprecated and goes away in 5.0: call agentry")
21
+ @browser
16
22
  end
17
23
 
18
24
  def redirect_to(path)
@@ -3,7 +3,7 @@
3
3
  require "uri"
4
4
  require_relative "middleware/context"
5
5
 
6
- module Browser
6
+ module Agentry
7
7
  class Middleware
8
8
  # Detect the most common assets.
9
9
  ASSETS_REGEX =
@@ -13,7 +13,7 @@ module Browser
13
13
  ACCEPT_REGEX = %r{(text/html|\*/\*)}
14
14
 
15
15
  def initialize(app, &block)
16
- raise ArgumentError, "Browser::Middleware requires a block" unless block
16
+ raise ArgumentError, "Agentry::Middleware requires a block" unless block
17
17
 
18
18
  @app = app
19
19
  @block = block
@@ -1,6 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- module Browser
3
+ module Agentry
4
4
  class MiuiBrowser < Base
5
5
  def id
6
6
  :miui_browser
@@ -1,6 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- module Browser
3
+ module Agentry
4
4
  class Nokia < Base
5
5
  def id
6
6
  :nokia
@@ -1,6 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- module Browser
3
+ module Agentry
4
4
  class Opera < Base
5
5
  def id
6
6
  :opera
@@ -1,6 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- module Browser
3
+ module Agentry
4
4
  class Otter < Base
5
5
  def id
6
6
  :otter
@@ -1,6 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- module Browser
3
+ module Agentry
4
4
  class PhantomJS < Base
5
5
  def id
6
6
  :phantom_js
@@ -1,6 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- module Browser
3
+ module Agentry
4
4
  class Platform
5
5
  class AdobeAir < Base
6
6
  def match?
@@ -1,6 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- module Browser
3
+ module Agentry
4
4
  class Platform
5
5
  class Android < Base
6
6
  def match?
@@ -1,6 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- module Browser
3
+ module Agentry
4
4
  class Platform
5
5
  class Base
6
6
  attr_reader :ua, :platform
@@ -1,6 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- module Browser
3
+ module Agentry
4
4
  class Platform
5
5
  class BlackBerry < Base
6
6
  def match?
@@ -1,6 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- module Browser
3
+ module Agentry
4
4
  class Platform
5
5
  class ChromeOS < Base
6
6
  def match?
@@ -1,6 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- module Browser
3
+ module Agentry
4
4
  class Platform
5
5
  class FirefoxOS < Base
6
6
  def version
@@ -1,6 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- module Browser
3
+ module Agentry
4
4
  class Platform
5
5
  class IOS < Base
6
6
  MATCHER = /(iPhone|iPad|iPod|CriOS)/
@@ -1,6 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- module Browser
3
+ module Agentry
4
4
  class Platform
5
5
  class KaiOS < Base
6
6
  def version
@@ -1,6 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- module Browser
3
+ module Agentry
4
4
  class Platform
5
5
  class Linux < Base
6
6
  def version
@@ -1,6 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- module Browser
3
+ module Agentry
4
4
  class Platform
5
5
  class Mac < Base
6
6
  def version
@@ -1,6 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- module Browser
3
+ module Agentry
4
4
  class Platform
5
5
  class Unknown < Base
6
6
  def version
@@ -1,6 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- module Browser
3
+ module Agentry
4
4
  class Platform
5
5
  class Windows < Base
6
6
  def version
@@ -1,6 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- module Browser
3
+ module Agentry
4
4
  class Platform
5
5
  class WindowsMobile < Base
6
6
  def version
@@ -1,6 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- module Browser
3
+ module Agentry
4
4
  class Platform
5
5
  class WindowsPhone < Base
6
6
  def version
@@ -15,7 +15,7 @@ require_relative "platform/chrome_os"
15
15
  require_relative "platform/adobe_air"
16
16
  require_relative "platform/kai_os"
17
17
 
18
- module Browser
18
+ module Agentry
19
19
  class Platform
20
20
  include DetectVersion
21
21
 
@@ -1,6 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- module Browser
3
+ module Agentry
4
4
  class QQ < Base
5
5
  def id
6
6
  :qq
@@ -0,0 +1,44 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "delegate"
4
+ require "rails"
5
+ require_relative "agentry"
6
+ require_relative "action_controller"
7
+ require_relative "middleware/context/additions"
8
+
9
+ module Agentry
10
+ class Railtie < Rails::Railtie
11
+ class BrowserConfig < SimpleDelegator
12
+ def method_missing(...) # rubocop:disable Style/MissingRespondToMissing -- Delegator's answers for the target
13
+ Agentry.deprecate("config.browser is deprecated and goes away in 5.0: use config.agentry")
14
+ super
15
+ end
16
+ end
17
+
18
+ config.agentry = ActiveSupport::OrderedOptions.new
19
+ config.browser = BrowserConfig.new(config.agentry)
20
+ config.eager_load_namespaces << Agentry
21
+
22
+ # An app that assigned config.browser a new object replaced the wrapper: its options become config.agentry.
23
+ def self.adopt_reassigned_browser_config(config)
24
+ return if config.browser.is_a?(BrowserConfig)
25
+
26
+ Agentry.deprecate("config.browser is deprecated and goes away in 5.0: use config.agentry")
27
+ config.agentry = config.browser
28
+ end
29
+
30
+ config.after_initialize { |app| adopt_reassigned_browser_config(app.config) }
31
+
32
+ initializer "agentry" do
33
+ ActiveSupport.on_load(:action_controller) do
34
+ ::ActionController::Base.include(Agentry::ActionController)
35
+
36
+ ::ActionController::Metal.include(Agentry::ActionController) if defined?(::ActionController::Metal) # rubocop:disable Layout/LineLength
37
+
38
+ Agentry::Middleware::Context.include(
39
+ Agentry::Middleware::Context::Additions
40
+ )
41
+ end
42
+ end
43
+ end
44
+ end
@@ -1,6 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- module Browser
3
+ module Agentry
4
4
  class Safari < Base
5
5
  def id
6
6
  :safari
@@ -1,6 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- module Browser
3
+ module Agentry
4
4
  class SamsungBrowser < Chrome
5
5
  def id
6
6
  :samsung_browser
@@ -1,6 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- module Browser
3
+ module Agentry
4
4
  class Snapchat < Base
5
5
  def id
6
6
  :snapchat
@@ -1,6 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- module Browser
3
+ module Agentry
4
4
  class SougouBrowser < Base
5
5
  def id
6
6
  :sougou_browser
@@ -1,6 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- module Browser
3
+ module Agentry
4
4
  class Sputnik < Base
5
5
  def id
6
6
  :sputnik
@@ -0,0 +1,30 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "yaml"
4
+ require_relative "agentry"
5
+
6
+ module Agentry
7
+ def self.user_agents
8
+ @user_agents ||= browser_user_agents
9
+ .merge(bot_user_agents)
10
+ .merge(search_engine_user_agents)
11
+ end
12
+
13
+ def self.browser_user_agents
14
+ @browser_user_agents ||= YAML.load_file(Agentry.root.join("test/ua.yml"))
15
+ end
16
+
17
+ def self.bot_user_agents
18
+ @bot_user_agents ||= YAML.load_file(Agentry.root.join("test/ua_bots.yml"))
19
+ end
20
+
21
+ def self.search_engine_user_agents
22
+ @search_engine_user_agents ||= YAML.load_file(
23
+ Agentry.root.join("test/ua_search_engines.yml")
24
+ )
25
+ end
26
+
27
+ def self.[](key)
28
+ user_agents.fetch(key)
29
+ end
30
+ end
@@ -1,6 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- module Browser
3
+ module Agentry
4
4
  class UCBrowser < Base
5
5
  def id
6
6
  :uc_browser
@@ -1,6 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- module Browser
3
+ module Agentry
4
4
  class Unknown < Base
5
5
  NAMES = {
6
6
  "QuickTime" => "QuickTime",
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- module Browser
4
- VERSION = "3.1.1"
3
+ module Agentry
4
+ VERSION = "4.0.0"
5
5
  end
@@ -1,6 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- module Browser
3
+ module Agentry
4
4
  class Weibo < Base
5
5
  def id
6
6
  :weibo
@@ -1,6 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- module Browser
3
+ module Agentry
4
4
  class Yandex < Base
5
5
  def id
6
6
  :yandex
data/lib/agentry.rb CHANGED
@@ -1,3 +1,4 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require_relative "browser"
3
+ require_relative "agentry/agentry"
4
+ require_relative "agentry/rails" if defined?(Rails)
@@ -1,33 +1,4 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require_relative "../browser"
4
- require "forwardable"
5
-
6
- module Browser
7
- module Aliases
8
- PLATFORM_ALIASES = %w[
9
- adobe_air? android? blackberry? chrome_os? firefox_os? ios? ios_app?
10
- ios_webview? linux? mac? windows10? windows7? windows8? windows8_1?
11
- windows? windows_mobile? windows_phone? windows_rt?
12
- windows_touchscreen_desktop? windows_vista? windows_wow64? windows_x64?
13
- windows_x64_inclusive? windows_xp?
14
- ].freeze
15
-
16
- DEVICE_ALIASES = %w[
17
- blackberry_playbook? console? ipad? iphone? ipod_touch? kindle?
18
- kindle_fire? mobile? nintendo? nintendo_switch? nintendo_wii?
19
- nintendo_wiiu? playbook? playstation3? playstation4? playstation?
20
- playstation_vita? ps3? ps4? psp? psp_vita? silk? surface? tablet? tv?
21
- vita? wii? wiiu? xbox? xbox_360? xbox_one?
22
- ].freeze
23
-
24
- def self.included(target)
25
- target.class_eval do
26
- extend Forwardable
27
-
28
- def_delegators :platform, *PLATFORM_ALIASES
29
- def_delegators :device, *DEVICE_ALIASES
30
- end
31
- end
32
- end
33
- end
3
+ require_relative "../agentry/aliases"
4
+ require_relative "../agentry/browser_constant"