Class: AgentUser
- Inherits:
-
Object
- Object
- AgentUser
- Defined in:
- lib/agent_user.rb
Instance Attribute Summary collapse
-
#bot_name ⇒ Object
readonly
Returns the value of attribute bot_name.
-
#bot_version ⇒ Object
readonly
Returns the value of attribute bot_version.
-
#browser_name ⇒ Object
readonly
Returns the value of attribute browser_name.
-
#browser_version ⇒ Object
readonly
Returns the value of attribute browser_version.
-
#mobile_device_name ⇒ Object
readonly
Returns the value of attribute mobile_device_name.
-
#mobile_device_version ⇒ Object
readonly
Returns the value of attribute mobile_device_version.
-
#operating_system_name ⇒ Object
readonly
Returns the value of attribute operating_system_name.
-
#operating_system_version ⇒ Object
readonly
Returns the value of attribute operating_system_version.
Instance Method Summary collapse
-
#get_bot_data(name_version_pairs) ⇒ Object
Parse out bot name and version.
-
#get_browser_data(name_version_pairs) ⇒ Object
Parse out operating system name and version.
-
#get_mobile_data(name_version_pairs) ⇒ Object
Parse out mobile device name and version.
-
#get_operating_system_data(name_version_pairs) ⇒ Object
Parse out operating system name and version.
-
#initialize(str = "") ⇒ AgentUser
constructor
A new instance of AgentUser.
-
#is_bot? ⇒ Boolean
Check if AgentUser detects a bot.
-
#is_mobile? ⇒ Boolean
Check if AgentUser detects a mobile device.
- #to_s ⇒ Object
Constructor Details
#initialize(str = "") ⇒ AgentUser
Returns a new instance of AgentUser.
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
# File 'lib/agent_user.rb', line 9 def initialize (str = "") # Device/Browser/OS name version pairs name_version_pairs = str.scan %r`((?:[-.a-zA-Z]+\s?)+)[/. ]((?:(?:DBN)|[xi0-9._-])+)` # Strip blacklisted names from name_version_pairs blacklist = %w:.NET Mozilla Trident RTC InfoPath AppleWebKit chromeframe FlipboardProxy Gecko Media\ Center\ PC AskTbORJ Windows-Media-Player MSOffice WinTSI Carrier MSN MAXTHON plugins Qwest: name_version_pairs = name_version_pairs.reject do |name, version| blacklist.any? {|n| name =~ /#{n}/i} end , = self.(name_version_pairs) @browser_name, @browser_version = self.get_browser_data(name_version_pairs) @mobile_device_name, @mobile_device_version = self.get_mobile_data(name_version_pairs) @bot_name, @bot_version = self.get_bot_data(name_version_pairs) return true end |
Instance Attribute Details
#bot_name ⇒ Object (readonly)
Returns the value of attribute bot_name.
7 8 9 |
# File 'lib/agent_user.rb', line 7 def bot_name @bot_name end |
#bot_version ⇒ Object (readonly)
Returns the value of attribute bot_version.
7 8 9 |
# File 'lib/agent_user.rb', line 7 def bot_version @bot_version end |
#browser_name ⇒ Object (readonly)
Returns the value of attribute browser_name.
4 5 6 |
# File 'lib/agent_user.rb', line 4 def browser_name @browser_name end |
#browser_version ⇒ Object (readonly)
Returns the value of attribute browser_version.
4 5 6 |
# File 'lib/agent_user.rb', line 4 def browser_version @browser_version end |
#mobile_device_name ⇒ Object (readonly)
Returns the value of attribute mobile_device_name.
6 7 8 |
# File 'lib/agent_user.rb', line 6 def mobile_device_name @mobile_device_name end |
#mobile_device_version ⇒ Object (readonly)
Returns the value of attribute mobile_device_version.
6 7 8 |
# File 'lib/agent_user.rb', line 6 def mobile_device_version @mobile_device_version end |
#operating_system_name ⇒ Object (readonly)
Returns the value of attribute operating_system_name.
5 6 7 |
# File 'lib/agent_user.rb', line 5 def end |
#operating_system_version ⇒ Object (readonly)
Returns the value of attribute operating_system_version.
5 6 7 |
# File 'lib/agent_user.rb', line 5 def end |
Instance Method Details
#get_bot_data(name_version_pairs) ⇒ Object
Parse out bot name and version
139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 |
# File 'lib/agent_user.rb', line 139 def get_bot_data(name_version_pairs) # Bot name and version bot_names = %w:bot google bing yahoo: bot_name, bot_version = name_version_pairs.find do |name, version| bot_names.any? {|bot_name| name =~ /#{bot_name}/i} end # Change underscores to periods in m_version bot_version = bot_version.gsub(?_,?.) if bot_version return bot_name, bot_version end |
#get_browser_data(name_version_pairs) ⇒ Object
Parse out operating system name and version
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 |
# File 'lib/agent_user.rb', line 44 def get_browser_data(name_version_pairs) # Filter our browser name and version based on user agent browser names b_names = %w:Safari Firefox MSIE IEMobile Opera Internet\ Explorer Chrome: b_name, b_version = name_version_pairs.find do |name, version| b_names.any? {|b_name| name =~ /#{b_name}/} end # Change MSIE to Internet Explorer b_name = 'Internet Explorer' if b_name == 'MSIE' # Change underscores to periods in b_version b_version = b_version.gsub(?_,?.) if b_version # The version number for Safari or Mobile Safari is located in an"m " # place in the user agent. This will solve that by finding that version number version_arr = name_version_pairs.find {|name,version| name == "Version"} if b_name =~ /Safari/ && version_arr b_version = version_arr[1] end return b_name, b_version end |
#get_mobile_data(name_version_pairs) ⇒ Object
Parse out mobile device name and version
117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 |
# File 'lib/agent_user.rb', line 117 def get_mobile_data(name_version_pairs) # Mobile device name and version m_names = %w:Android Tablet Blackberry iPhone\ OS Zune CPU\ OS Windows\ Phone\ OS: m_name, m_version = name_version_pairs.find do |name, version| m_names.any? {|m_name| name =~ /#{m_name}/} end # Change underscores to periods in m_version m_version = m_version.gsub(?_,?.) if m_version m_name = 'iPad' if m_name == 'CPU OS' m_name = 'iPhone' if m_name == 'CPU iPhone OS' return m_name, m_version end |
#get_operating_system_data(name_version_pairs) ⇒ Object
Parse out operating system name and version
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 |
# File 'lib/agent_user.rb', line 71 def (name_version_pairs) # NOTE: Reverse order of name_version_pairs to that Android and Ubuntu are # found before Linux is if they exist in the name_version_pairs array. os_names = %w:Windows Android Ubuntu Linux CPU\ iPhone\ OS CPU\ OS Mac\ OS: os_name, os_version = name_version_pairs.reverse.find do |name, version| os_names.any? {|os_name| name =~ /#{os_name}/} end # Change underscores to periods in os_version os_version = os_version.gsub(?_,?.) if os_version # Change name from user agent to iOS if from an iPad or iPhone os_name = 'iOS' if ['CPU OS','CPU iPhone OS'].include?(os_name) os_name = 'Windows' if os_name == 'Windows NT' # Handle Windows version numbers/names if os_name == 'Windows' os_version = case os_version when '6.2' '8' when '6.1' '7' when '6.0' 'Vista' when '5.2' 'XP x64' when '5.1' 'XP' when '5.0' '2000' when '4.1' '98' when '4.9' 'ME' when '4.0' '95' end end return os_name, os_version end |
#is_bot? ⇒ Boolean
Check if AgentUser detects a bot
163 164 165 |
# File 'lib/agent_user.rb', line 163 def is_bot? !!self.bot_name end |
#is_mobile? ⇒ Boolean
Check if AgentUser detects a mobile device
158 159 160 |
# File 'lib/agent_user.rb', line 158 def is_mobile? !!self.mobile_device_name end |
#to_s ⇒ Object
32 33 34 35 36 37 38 39 |
# File 'lib/agent_user.rb', line 32 def to_s user_agent_data = [] user_agent_data << "OS: #{self.operating_system_name} #{self.operating_system_version}" if self. user_agent_data << "Browser: #{self.browser_name} #{self.browser_version}" if self.browser_name user_agent_data << "Mobile: #{self.mobile_device_name} #{self.mobile_device_version}" if self.mobile_device_name user_agent_data << "Bot: #{self.bot_name} #{self.bot_version}" if self.bot_name puts user_agent_data.join(", ") end |