Skip to content

Commit

Permalink
Use "unknown" for everything we don't recognize (e.g. browser, platfo…
Browse files Browse the repository at this point in the history
…rm, device).
  • Loading branch information
fnando committed May 31, 2020
1 parent f5f9391 commit b9e65f3
Show file tree
Hide file tree
Showing 15 changed files with 133 additions and 79 deletions.
9 changes: 9 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
# Changelog

## Unreleased

- Rename `Browser::Platform#other?` to `Browser::Platform#unknown?`
- Unknown platforms now return `:unknown_platform` as the id
- Unknown devices now return `:unknown_device` as the id
- Unknown browsers now return `:unknown_browser` as the id
- All the changes above affect how `browser.meta` is composed
- Add method `Browser::Base#unknown?`

## 4.2.0

- Fix Chrome Lighthouse detection.
Expand Down
127 changes: 80 additions & 47 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ browser.ie?
browser.ie?(6) # detect specific IE version
browser.ie?([">8", "<10"]) # detect specific IE (IE9).
browser.known? # has the browser been successfully detected?
browser.unknown? # the browser wasn't detected.
browser.meta # an array with several attributes
browser.name # readable browser name
browser.nokia?
Expand Down Expand Up @@ -66,6 +67,7 @@ Browser::Bot.why?(ua)
browser.device
browser.device.id
browser.device.name
browser.device.unknown?
browser.device.blackberry_playbook?
browser.device.console?
browser.device.ipad?
Expand Down Expand Up @@ -111,7 +113,7 @@ browser.platform.ios_app? # detect webview in an iOS app
browser.platform.ios_webview? # alias for ios_app?
browser.platform.linux?
browser.platform.mac?
browser.platform.other?
browser.platform.unknown?
browser.platform.windows10?
browser.platform.windows7?
browser.platform.windows8?
Expand All @@ -130,7 +132,9 @@ browser.platform.windows_xp?

### Aliases

To add aliases like `mobile?` and `tablet?` to the base object (e.g `browser.mobile?`), require the `browser/aliases` file and extend the Browser::Base object like the following:
To add aliases like `mobile?` and `tablet?` to the base object (e.g
`browser.mobile?`), require the `browser/aliases` file and extend the
Browser::Base object like the following:

```ruby
require "browser/aliases"
Expand All @@ -142,13 +146,18 @@ browser.mobile? #=> false

### What's being detected?

- For a list of platform detections, check [lib/browser/platform.rb](https://github.com/fnando/browser/blob/master/lib/browser/platform.rb)
- For a list of device detections, check [lib/browser/device.rb](https://github.com/fnando/browser/blob/master/lib/browser/device.rb)
- For a list of bot detections, check [bots.yml](https://github.com/fnando/browser/blob/master/bots.yml)
- For a list of platform detections, check
[lib/browser/platform.rb](https://github.com/fnando/browser/blob/master/lib/browser/platform.rb)
- For a list of device detections, check
[lib/browser/device.rb](https://github.com/fnando/browser/blob/master/lib/browser/device.rb)
- For a list of bot detections, check
[bots.yml](https://github.com/fnando/browser/blob/master/bots.yml)

### Detecting modern browsers

To detect whether a browser can be considered as modern or not, create a method that abstracts your versioning constraints. The following example will consider any of the following browsers as a modern:
To detect whether a browser can be considered as modern or not, create a method
that abstracts your versioning constraints. The following example will consider
any of the following browsers as a modern:

```ruby
# Expects an Browser instance,
Expand Down Expand Up @@ -176,23 +185,26 @@ Just add it to the Gemfile.
gem "browser"
```

This adds a helper method called `browser`, that inspects your current user agent.
This adds a helper method called `browser`, that inspects your current user
agent.

```erb
<% if browser.ie?(6) %>
<p class="disclaimer">You're running an older IE version. Please update it!</p>
<% end %>
```

If you want to use Browser on your Rails app but don't want to taint your controller, use the following line on your Gemfile:
If you want to use Browser on your Rails app but don't want to taint your
controller, use the following line on your Gemfile:

```ruby
gem "browser", require: "browser/browser"
```

### Accept Language

Parses the accept-language header from an HTTP request and produces an array of language objects sorted by quality.
Parses the accept-language header from an HTTP request and produces an array of
language objects sorted by quality.

```ruby
browser = Browser.new("Some User Agent", accept_language: "en-us")
Expand All @@ -218,16 +230,22 @@ language.name
#=> "English/United States"
```

Result is always sorted in quality order from highest to lowest. As per the HTTP spec:
Result is always sorted in quality order from highest to lowest. As per the HTTP
spec:

- omitting the quality value implies 1.0.
- quality value equal to zero means that is not accepted by the client.

### Internet Explorer

Internet Explorer has a compatibility view mode that allows newer versions (IE8+) to run as an older version. Browser will always return the navigator version, ignoring the compatibility view version, when defined. If you need to get the engine's version, you have to use `Browser#msie_version` and `Browser#msie_full_version`.
Internet Explorer has a compatibility view mode that allows newer versions
(IE8+) to run as an older version. Browser will always return the navigator
version, ignoring the compatibility view version, when defined. If you need to
get the engine's version, you have to use `Browser#msie_version` and
`Browser#msie_full_version`.

So, let's say an user activates compatibility view in a IE11 browser. This is what you'll get:
So, let's say an user activates compatibility view in a IE11 browser. This is
what you'll get:

```ruby
browser.version
Expand All @@ -246,11 +264,14 @@ browser.compatibility_view?
#=> true
```

This behavior changed in `v1.0.0`; previously there wasn't a way of getting the real browser version.
This behavior changed in `v1.0.0`; previously there wasn't a way of getting the
real browser version.

### Safari

iOS webviews and web apps aren't detected as Safari anymore, so be aware of that if that's your case. You can use a combination of platform and webkit detection to do whatever you want.
iOS webviews and web apps aren't detected as Safari anymore, so be aware of that
if that's your case. You can use a combination of platform and webkit detection
to do whatever you want.

```ruby
# iPad's Safari running as web app mode.
Expand All @@ -268,19 +289,25 @@ browser.platform.ios?

### Bots

The bot detection is quite aggressive. Anything that matches at least one of the following requirements will be considered a bot.
The bot detection is quite aggressive. Anything that matches at least one of the
following requirements will be considered a bot.

- Empty user agent string
- User agent that matches `/crawl|fetch|search|monitoring|spider|bot/`
- Any known bot listed under [bots.yml](https://github.com/fnando/browser/blob/master/bots.yml)
- Any known bot listed under
[bots.yml](https://github.com/fnando/browser/blob/master/bots.yml)

To add custom matchers, you can add a callable object to `Browser::Bot.matchers`. The following example matches everything that has a `externalhit` substring on it. The bot name will always be `General Bot`.
To add custom matchers, you can add a callable object to
`Browser::Bot.matchers`. The following example matches everything that has a
`externalhit` substring on it. The bot name will always be `General Bot`.

```ruby
Browser::Bot.matchers << ->(ua, _browser) { ua =~ /externalhit/i }
```

To clear all matchers, including the ones that are bundled, use `Browser::Bot.matchers.clear`. You can re-add built-in matchers by doing the following:
To clear all matchers, including the ones that are bundled, use
`Browser::Bot.matchers.clear`. You can re-add built-in matchers by doing the
following:

```ruby
Browser::Bot.matchers += Browser::Bot.default_matchers
Expand All @@ -303,15 +330,17 @@ use Browser::Middleware do
end
```

If you're using Rails, you can use the route helper methods. Just add something like the following to a initializer file (`config/initializers/browser.rb`).
If you're using Rails, you can use the route helper methods. Just add something
like the following to a initializer file (`config/initializers/browser.rb`).

```ruby
Rails.configuration.middleware.use Browser::Middleware do
redirect_to upgrade_path if browser.ie?
end
```

If you need access to the `Rack::Request` object (e.g. to exclude a path), you can do so with `request`.
If you need access to the `Rack::Request` object (e.g. to exclude a path), you
can do so with `request`.

```ruby
Rails.configuration.middleware.use Browser::Middleware do
Expand All @@ -335,25 +364,28 @@ Once you've made your great commits (include tests, please):
4. Create a pull request
5. That's it!

Please respect the indentation rules and code style.
And use 2 spaces, not tabs. And don't touch the version thing.
Please respect the indentation rules and code style. And use 2 spaces, not tabs.
And don't touch the version thing.

## Configuring environment

To configure your environment, you must have Ruby and bundler installed. Then run `bundle install` to install all dependencies.
To configure your environment, you must have Ruby and bundler installed. Then
run `bundle install` to install all dependencies.

To run tests, execute `./bin/rake`.

### Adding new features

Before using your time to code a new feature, open a ticket asking if it makes sense and if it's on this project's scope.
Before using your time to code a new feature, open a ticket asking if it makes
sense and if it's on this project's scope.

Don't forget to add a new entry to `CHANGELOG.md`.

#### Adding a new bot

1. Add the user agent to `test/ua_bots.yml`.
2. Add the readable name to `bots.yml`. The key must be something that matches the user agent, in lowercased text.
2. Add the readable name to `bots.yml`. The key must be something that matches
the user agent, in lowercased text.
3. Run tests.

Don't forget to add a new entry to `CHANGELOG.md`.
Expand All @@ -362,42 +394,43 @@ Don't forget to add a new entry to `CHANGELOG.md`.

1. Add the user agent to `test/ua_search_engines.yml`.
2. Add the same user agent to `test/ua_bots.yml`.
3. Add the readable name to `search_engines.yml`. The key must be something that matches the user agent, in lowercased text.
3. Add the readable name to `search_engines.yml`. The key must be something that
matches the user agent, in lowercased text.
4. Run tests.

Don't forget to add a new entry to `CHANGELOG.md`.

#### Wrong browser/platform/device detection

If you know how to fix it, follow the "Writing code" above. Open an issue otherwise; make sure you fill in the issue template with all the required information.
If you know how to fix it, follow the "Writing code" above. Open an issue
otherwise; make sure you fill in the issue template with all the required
information.

## Maintainer

* Nando Vieira - http://nandovieira.com
- Nando Vieira - http://nandovieira.com

## Contributors

* https://github.com/fnando/browser/contributors
- https://github.com/fnando/browser/contributors

## License

(The MIT License)

Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
'Software'), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:

The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the 'Software'), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
the Software, and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
6 changes: 5 additions & 1 deletion lib/browser/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,11 @@ def webkit_full_version
end

def known?
id != :generic
!unknown?
end

def unknown?
id == :unknown_browser
end

# Detect if browser is a proxy browser.
Expand Down
4 changes: 2 additions & 2 deletions lib/browser/browser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
require_relative "edge"
require_relative "opera"
require_relative "blackberry"
require_relative "generic"
require_relative "unknown"
require_relative "phantom_js"
require_relative "uc_browser"
require_relative "nokia"
Expand Down Expand Up @@ -77,7 +77,7 @@ def self.matchers
SamsungBrowser, # must be placed before Chrome and Safari
Chrome,
Safari,
Generic
Unknown
]
end

Expand Down
2 changes: 1 addition & 1 deletion lib/browser/device.rb
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ def ipad?
end

def unknown?
id == :unknown
id == :unknown_device
end

def ipod_touch?
Expand Down
2 changes: 1 addition & 1 deletion lib/browser/device/unknown.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ module Browser
class Device
class Unknown < Base
def id
:unknown
:unknown_device
end

def name
Expand Down
8 changes: 4 additions & 4 deletions lib/browser/platform.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
require_relative "platform/firefox_os"
require_relative "platform/blackberry"
require_relative "platform/android"
require_relative "platform/other"
require_relative "platform/unknown"
require_relative "platform/chrome_os"
require_relative "platform/adobe_air"

Expand All @@ -35,7 +35,7 @@ def self.matchers
FirefoxOS,
Windows,
Linux,
Other
Unknown
]
end

Expand All @@ -61,8 +61,8 @@ def android?(expected_version = nil)
id == :android && detect_version?(version, expected_version)
end

def other?
id == :other
def unknown?
id == :unknown_platform
end

def linux?
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,17 @@

module Browser
class Platform
class Other < Base
class Unknown < Base
def version
"0"
end

def name
"Other"
"Unknown"
end

def id
:other
:unknown_platform
end

def match?
Expand Down
Loading

0 comments on commit b9e65f3

Please sign in to comment.