class BrowserSniffer

Constants

REGEX_MAP
STRING_MAP
VERSION

Attributes

user_agent[R]

Public Class Methods

new(user_agent) click to toggle source
# File lib/browser_sniffer.rb, line 7
def initialize(user_agent)
  @user_agent = user_agent
end

Public Instance Methods

android?() click to toggle source
# File lib/browser_sniffer.rb, line 15
def android?
  os == :android
end
browser() click to toggle source
# File lib/browser_sniffer.rb, line 73
def browser
  browser_info[:type]
end
browser_info() click to toggle source
# File lib/browser_sniffer.rb, line 126
def browser_info
  @browser_info ||= parse_user_agent_for(BrowserSniffer::REGEX_MAP[:browser])
end
browser_name() click to toggle source
# File lib/browser_sniffer.rb, line 77
def browser_name
  browser_info[:name]
end
browser_version() click to toggle source
# File lib/browser_sniffer.rb, line 86
def browser_version
  browser_info[:version]
end
chromium_based?() click to toggle source
# File lib/browser_sniffer.rb, line 195
def chromium_based?
  browser_name ? browser_name.downcase.match(/chrom(e|ium)/) : false
end
desktop?() click to toggle source
# File lib/browser_sniffer.rb, line 44
def desktop?
  form_factor == :desktop
end
device() click to toggle source
# File lib/browser_sniffer.rb, line 98
def device
  device_info[:name]
end
device_info() click to toggle source
# File lib/browser_sniffer.rb, line 138
def device_info
  @device_info ||= parse_user_agent_for(BrowserSniffer::REGEX_MAP[:device])
end
device_name() click to toggle source
# File lib/browser_sniffer.rb, line 102
def device_name
  device_info[:model]
end
device_vendor() click to toggle source
# File lib/browser_sniffer.rb, line 106
def device_vendor
  device_info[:vendor]
end
engine() click to toggle source
# File lib/browser_sniffer.rb, line 54
def engine
  engine_info[:type]
end
engine_info() click to toggle source
# File lib/browser_sniffer.rb, line 134
def engine_info
  @engine_info ||= parse_user_agent_for(BrowserSniffer::REGEX_MAP[:engine])
end
engine_name() click to toggle source
# File lib/browser_sniffer.rb, line 58
def engine_name
  engine_info[:name]
end
engine_version() click to toggle source
# File lib/browser_sniffer.rb, line 67
def engine_version
  engine_info[:version]
end
form_factor() click to toggle source
# File lib/browser_sniffer.rb, line 48
def form_factor
  device_info[:type] || :desktop
end
handheld?() click to toggle source
# File lib/browser_sniffer.rb, line 36
def handheld?
  form_factor == :handheld
end
ie11?() click to toggle source
# File lib/browser_sniffer.rb, line 23
def ie11?
  browser == :ie && major_browser_version == 11
end
ie11_actual?() click to toggle source

This method checks ie 11 mobile or ie11 rendering an older version in compatibility mode, in addition to ‘ie11?`. The `ie11?` method would return false in both those scenarios.

# File lib/browser_sniffer.rb, line 29
def ie11_actual?
  ie11_engine = major_engine_version == 7 && engine_name == 'Trident'
  ie_mobile11 = major_browser_version == 11 && browser_name == 'IEMobile'

  ie11? || ie11_engine || ie_mobile11
end
ie8?() click to toggle source
# File lib/browser_sniffer.rb, line 19
def ie8?
  browser == :ie && major_browser_version == 8
end
in_app_browser() click to toggle source
# File lib/browser_sniffer.rb, line 92
def in_app_browser
  in_app_browser_info[:type]
end
in_app_browser_info() click to toggle source
# File lib/browser_sniffer.rb, line 130
def in_app_browser_info
  @in_app_browser_info = parse_user_agent_for(BrowserSniffer::REGEX_MAP[:in_app_browser])
end
ios?() click to toggle source
# File lib/browser_sniffer.rb, line 11
def ios?
  os == :ios
end
major_browser_version() click to toggle source
# File lib/browser_sniffer.rb, line 81
def major_browser_version
  str = browser_info[:major]
  str && str.to_i
end
major_engine_version() click to toggle source
# File lib/browser_sniffer.rb, line 62
def major_engine_version
  str = engine_info[:major]
  str && str.to_i
end
os() click to toggle source
# File lib/browser_sniffer.rb, line 112
def os
  os_info[:type]
end
os_info() click to toggle source
# File lib/browser_sniffer.rb, line 142
def os_info
  @os_info ||= parse_user_agent_for(BrowserSniffer::REGEX_MAP[:os])
end
os_name() click to toggle source
# File lib/browser_sniffer.rb, line 116
def os_name
  os_info[:name]
end
os_version() click to toggle source
# File lib/browser_sniffer.rb, line 120
def os_version
  os_info[:version]
end
parse_user_agent_for(type) click to toggle source
# File lib/browser_sniffer.rb, line 146
def parse_user_agent_for(type)
  result = {}
  type.each_slice(2) do |regexps, format|
    regexps.each do |regex|
      regex.match(user_agent) do |match|
        format.each_with_index do |field, index|
          if field.class == Symbol
            result[field] = match[index + 1]
          elsif field.class == Array
            if field[1].class == String || field[1].class == Symbol
              result[field[0]] = field[1]
            elsif field[1].class == Hash
              if field[1][match[index + 1]]
                result[field[0]] = field[1][match[index + 1]]
              else
                result[field[0]] = match[index + 1]
              end
            elsif field[1].class == Proc
              result[field[0]] = field[1].call(match[index + 1])
            end
          end
        end
        return result
      end
    end
  end
  result
end
same_site_none_compatible?() click to toggle source
# File lib/browser_sniffer.rb, line 175
def same_site_none_compatible?
  return false unless user_agent

  webkit_same_site_compatible? && same_site_recognized_browser?
end
same_site_recognized_browser?() click to toggle source
# File lib/browser_sniffer.rb, line 188
def same_site_recognized_browser?
  return false unless major_browser_version

  !(chromium_based? && major_browser_version >= 51 && major_browser_version <= 66) &&
    !(uc_browser? && !uc_browser_version_at_least?(12, 13, 2))
end
tablet?() click to toggle source
# File lib/browser_sniffer.rb, line 40
def tablet?
  form_factor == :tablet
end
uc_browser?() click to toggle source
# File lib/browser_sniffer.rb, line 199
def uc_browser?
  user_agent ? user_agent.downcase.match(/uc\s?browser/) : false
end
uc_browser_version_at_least?(major, minor, build) click to toggle source
# File lib/browser_sniffer.rb, line 203
def uc_browser_version_at_least?(major, minor, build)
  return false unless browser_version
  digits = browser_version.split('.').map(&:to_i)
  return false unless digits.count >= 3

  return digits[0] > major if digits[0] != major
  return digits[1] > minor if digits[1] != minor
  digits[2] >= build
end
webkit_same_site_compatible?() click to toggle source
# File lib/browser_sniffer.rb, line 181
def webkit_same_site_compatible?
  return false unless os && os_version && browser

  !(os == :ios && os_version.match(/^([0-9]|1[12])[\.\_]/)) &&
     !(os == :mac && browser == :safari && os_version.match(/^10[\.\_]14/))
end