class Brauser::Browser
This class represents a detection of the current user browser.
@attribute agent
@return [String] The raw User-Agent HTTP header.
@attribute accept_language
@return [String] The raw Accept-Language HTTP header.
@attribute [r] name
@return [Symbol] The current browser name.
@attribute [r] human_name
@return [String] The human-readable current browser name.
@attribute [r] version
@return [Versionomy::Value] The current browser version.
@attribute [r] platform
@return [Symbol] The current browser platform.
@attribute [r] human_platform
@return [String] The human-readable current browser platform.
@attribute [r] languages
@return [Hash] The accepted languages.
@attribute [r] human_languages
@return [Hash] The human-readable list of accepted languages.
Attributes
Public Class Methods
Creates a new browser.
@param agent [String] The User-Agent HTTP header. @param accept_language
[String] The Accept-Language HTTP header.
# File lib/brauser/browser.rb, line 37 def initialize(agent = "", accept_language = "") @agent = agent @accept_language = accept_language parse end
Public Instance Methods
Checks if the browser accepts a specific language or languages.
@param languages [Array] The list of languages. @return [Boolean] `true` if at least one of requested languages is accepted, `false` otherwise.
# File lib/brauser/browser.rb, line 69 def accepts?(*languages) languages = normalize_query_arguments(languages) (@languages.keys & languages).present? end
Returns an array of information about the browser. Information are strings which are suitable to use as CSS classes.
For version, it will be included a class for every token of the version. For example, version `7.0.1.2` will return this:
“`ruby
- “version-7”, “version-7_0”, “version-7_0_1”, “version-7_0_1_2”
-
“`
If you provide a block (with accepts name, version and platform as arguments), it will be used for translating the name.
@param join [String|NilClass] If non falsy, the separator to use to join information. If falsy, informations will be returned as array. @param name [Boolean] If non falsy, the string to prepend to the name. If falsy, the name information will not be included. @param version [String|NilClass] If non falsy, the string to prepend to the version. If falsy, the version information will not be included. @param platform [String|NilClass] If non falsy, the string to prepend to the platform. If falsy, the platform information will not be included. @return [String|Array] CSS ready information of the current browser.
# File lib/brauser/browser.rb, line 58 def classes(join = " ", name: true, version: true, platform: true) rv = [name_to_str(name), version_to_str(version), platform_to_str(platform)].compact.uniq.flatten join ? rv.join(join) : rv end
Checks if the browser matches a specific query.
@param name [Symbol|Array|NilClass] The list of names to check. Also, this meta-name is supported: `:tablet`. @param engine [Symbol|Array|NilClass] Alias for `name`, **which has precedence over this.** @param version [String|NilClass] The query to match the version.
It must be a query in the form is `OPERATOR VALUE && ..`, where `OPERATOR` is one of `["<", "<=", "=", "==", ">=", ">"]`. You can also pass the value "capable", which will return true for Webkit browsers, IE 10 or above, Firefox 28 and above and Opera 15 and above.
@param platform [Symbol|Array|NilClass] The list of platforms to check. @param languages [Symbol|Array|NilClass] The list of languages to check. @return [Boolean] `true` if browser match the query, `false` otherwise.
# File lib/brauser/browser.rb, line 84 def is?(name: nil, engine: nil, version: nil, platform: nil, languages: nil) name ||= engine rv = name ? (@name == apply_aliases(normalize_query_arguments(name))) : true rv &&= query_version(version) if version rv &&= @platform == normalize_query_arguments(platform) if platform rv &&= accepts?(normalize_query_arguments(languages)) if languages rv end
Check if the browser is a specific one
@param method The browser engine to check. @param args [Array] unused. @param block [Proc] unused. @return [Boolean] `true` if browser match the engine, `false` otherwise.
# File lib/brauser/browser.rb, line 109 def method_missing(method, *args, &block) method.to_s =~ /(.+)\?$/ ? is?(name: Regexp.last_match[1]) : super(method, *args, &block) end
Check if the browser is supported.
@param browsers [Hash|String] A map of name and minimum supported major version, or a path to YAML file containing the map. @return [Boolean] `true` if current browser is supported, `false` otherwise. If the name is not found in the map, `false` is returned.
# File lib/brauser/browser.rb, line 97 def supported?(browsers = {}) browsers = YAML.load_file(browsers.to_s).symbolize_keys unless browsers.is_a?(Hash) minimum_version = browsers.with_indifferent_access[name.value] minimum_version ? is?(version: ">= #{minimum_version}") : false end