Class: Brauser::Browser
- Inherits:
-
Object
- Object
- Brauser::Browser
- Defined in:
- lib/brauser/browser.rb
Overview
This class represents a detection of the current user browser.
Constant Summary
Instance Attribute Summary (collapse)
-
- (String) accept_language
The raw Accept-Language HTTP header.
-
- (String) agent
The raw User-Agent HTTP header.
-
- (Hash) human_languages
readonly
The human-readable list of accepted languages.
-
- (String) human_name
readonly
The human-readable current browser name.
-
- (String) human_platform
readonly
The human-readable current browser platform.
-
- (Hash) languages
readonly
The accepted languages.
-
- (Symbol) name
readonly
The current browser name.
-
- (Symbol) platform
readonly
The current browser platform.
-
- (Versionomy::Value) version
readonly
The current browser version.
Instance Method Summary (collapse)
-
- (Boolean) accepts?(*languages)
Checks if the browser accepts a specific language or languages.
-
- (String|Array) classes(join = " ", name: true, version: true, platform: true)
(also: #meta, #to_s)
Returns an array of information about the browser.
-
- (Browser) initialize(agent = "", accept_language = "")
constructor
Creates a new browser.
-
- (Boolean) is?(name: nil, engine: nil, version: nil, platform: nil, languages: nil)
Checks if the browser matches a specific query.
-
- (Boolean) method_missing(method, *args, &block)
Check if the browser is a specific one.
Constructor Details
- (Browser) initialize(agent = "", accept_language = "")
Creates a new browser.
37 38 39 40 41 |
# File 'lib/brauser/browser.rb', line 37 def initialize(agent = "", accept_language = "") @agent = agent @accept_language = accept_language parse end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
- (Boolean) method_missing(method, *args, &block)
Check if the browser is a specific one
99 100 101 |
# File 'lib/brauser/browser.rb', line 99 def method_missing(method, *args, &block) method.to_s =~ /(.+)\?$/ ? is?(name: Regexp.last_match[1]) : super(method, *args, &block) end |
Instance Attribute Details
- (String) accept_language
Returns The raw Accept-Language HTTP header.
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 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 69 70 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 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 |
# File 'lib/brauser/browser.rb', line 28 class Browser attr_accessor :agent, :accept_language attr_reader :name, :human_name, :version, :platform, :human_platform, :languages, :human_languages alias_attribute :ua, :agent # Creates a new browser. # # @param agent [String] The User-Agent HTTP header. # @param accept_language [String] The Accept-Language HTTP header. def initialize(agent = "", accept_language = "") @agent = agent @accept_language = accept_language parse 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. 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 alias_method :meta, :classes alias_method :to_s, :classes # 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. def accepts?(*languages) languages = normalize_query_arguments(languages) (@languages.keys & languages).present? end # Checks if the browser matches a specific query. # # @param name [Symbol|Array] The list of names to check. Also, this meta-name is supported: `:tablet`. # @param engine [Symbol|Array] Alias for `name`, **which has precedence over this.** # @param version [String] 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] The list of platforms to check. # @param languages [Symbol|Array] The list of languages to check. # @return [Boolean] `true` if browser match the query, `false` otherwise. 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. def method_missing(method, *args, &block) method.to_s =~ /(.+)\?$/ ? is?(name: Regexp.last_match[1]) : super(method, *args, &block) end private # :nodoc: VERSION_TOKEN = /(?<operator>>=|<=|<|>|!=|(={1,2}))\s*(?<version>.+)/ # :nodoc: def parse parser = Brauser::Parser.new parse_agent(parser) parse_languages(parser) end # :nodoc: def parse_agent(parser) agent = parser.parse_agent(@agent) if agent @name, @version, @platform = *agent @human_name = Brauser::Definitions.browsers[@name].try(:name) || "Unknown Browser" @human_platform = Brauser::Definitions.platforms[@platform].try(:name) || "Unknown Platform" else @name = @platform = Brauser::Value.new(:unknown) @human_name = @human_platform = Brauser::Value.new("Unknown") @version = Brauser::Value.new(Versionomy.parse("0.0")) end end # :nodoc: def parse_languages(parser) languages = parser.parse_accept_language(@accept_language) @languages = languages @human_languages = languages.reduce({}) { |rv, (code, priority)| rv[Brauser::Definitions.languages[code].name] = priority rv } end # :nodoc: def name_to_str(name) if name name = "" if name.is_a?(TrueClass) rv = @name rv = [:msie_compatibility, :msie] if rv == :msie_compatibility rv.ensure_array(no_duplicates: true) { |n| "#{name}#{n}" } else nil end end # :nodoc: def version_to_str(version) if version version = "version-" if version.is_a?(TrueClass) version_str = @version.values_array.reduce([]) { |rv, current| rv << [rv.last, current].compact.join("_") rv } version_str.map { |v| "#{version}#{v}" } else nil end end # :nodoc: def platform_to_str(platform) if platform platform = "platform-" if platform.is_a?(TrueClass) "#{platform}#{@platform}" else nil end end # :nodoc: def normalize_query_arguments(arguments) sanitizer = ->(a) { a.ensure_string.downcase.gsub("_", "-").to_sym } arguments ? arguments.ensure_array(no_duplicates: true, compact: true, flatten: true, &sanitizer) : nil end # :nodoc: def apply_aliases(names) names << [:msie, :msie_compatibility] if (names & [:ie, :msie]).present? names << [:chromium] if names.include?(:chrome) names << [:ipad, :android, :kindle] if names.include?(:tablet) names.flatten.compact.uniq end # :nodoc: def query_version(version) version.ensure_string.strip.parameterize.to_sym == :capable ? check_capable_browser : check_version(version) end # :nodoc: def check_version(version) parser = StringScanner.new(version) rv = true until parser.eos? token = parser.scan_until(/(?=&&)|\Z/) parser.skip_until(/&&|\Z/) operator, version = parse_version_token(token) rv &&= @version.send(operator, Versionomy.parse(version)) break unless rv end rv end # :nodoc: def check_capable_browser check_capable_browser_engines || check_capable_browser_recents end # :nodoc: def check_capable_browser_engines chrome? || safari? || check_capable_browser_recents end # :nodoc: def check_capable_browser_recents (firefox? && @version >= "28") || (msie? && @version >= "10") || (opera? && @version >= "15") end # :nodoc: def parse_version_token(token) mo = VERSION_TOKEN.match(token) raise ArgumentError, "Invalid version check: #{token}." unless mo [mo["operator"], mo["version"]].map(&:strip) end end |
- (String) agent
Returns The raw User-Agent HTTP header.
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 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 69 70 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 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 |
# File 'lib/brauser/browser.rb', line 28 class Browser attr_accessor :agent, :accept_language attr_reader :name, :human_name, :version, :platform, :human_platform, :languages, :human_languages alias_attribute :ua, :agent # Creates a new browser. # # @param agent [String] The User-Agent HTTP header. # @param accept_language [String] The Accept-Language HTTP header. def initialize(agent = "", accept_language = "") @agent = agent @accept_language = accept_language parse 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. 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 alias_method :meta, :classes alias_method :to_s, :classes # 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. def accepts?(*languages) languages = normalize_query_arguments(languages) (@languages.keys & languages).present? end # Checks if the browser matches a specific query. # # @param name [Symbol|Array] The list of names to check. Also, this meta-name is supported: `:tablet`. # @param engine [Symbol|Array] Alias for `name`, **which has precedence over this.** # @param version [String] 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] The list of platforms to check. # @param languages [Symbol|Array] The list of languages to check. # @return [Boolean] `true` if browser match the query, `false` otherwise. 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. def method_missing(method, *args, &block) method.to_s =~ /(.+)\?$/ ? is?(name: Regexp.last_match[1]) : super(method, *args, &block) end private # :nodoc: VERSION_TOKEN = /(?<operator>>=|<=|<|>|!=|(={1,2}))\s*(?<version>.+)/ # :nodoc: def parse parser = Brauser::Parser.new parse_agent(parser) parse_languages(parser) end # :nodoc: def parse_agent(parser) agent = parser.parse_agent(@agent) if agent @name, @version, @platform = *agent @human_name = Brauser::Definitions.browsers[@name].try(:name) || "Unknown Browser" @human_platform = Brauser::Definitions.platforms[@platform].try(:name) || "Unknown Platform" else @name = @platform = Brauser::Value.new(:unknown) @human_name = @human_platform = Brauser::Value.new("Unknown") @version = Brauser::Value.new(Versionomy.parse("0.0")) end end # :nodoc: def parse_languages(parser) languages = parser.parse_accept_language(@accept_language) @languages = languages @human_languages = languages.reduce({}) { |rv, (code, priority)| rv[Brauser::Definitions.languages[code].name] = priority rv } end # :nodoc: def name_to_str(name) if name name = "" if name.is_a?(TrueClass) rv = @name rv = [:msie_compatibility, :msie] if rv == :msie_compatibility rv.ensure_array(no_duplicates: true) { |n| "#{name}#{n}" } else nil end end # :nodoc: def version_to_str(version) if version version = "version-" if version.is_a?(TrueClass) version_str = @version.values_array.reduce([]) { |rv, current| rv << [rv.last, current].compact.join("_") rv } version_str.map { |v| "#{version}#{v}" } else nil end end # :nodoc: def platform_to_str(platform) if platform platform = "platform-" if platform.is_a?(TrueClass) "#{platform}#{@platform}" else nil end end # :nodoc: def normalize_query_arguments(arguments) sanitizer = ->(a) { a.ensure_string.downcase.gsub("_", "-").to_sym } arguments ? arguments.ensure_array(no_duplicates: true, compact: true, flatten: true, &sanitizer) : nil end # :nodoc: def apply_aliases(names) names << [:msie, :msie_compatibility] if (names & [:ie, :msie]).present? names << [:chromium] if names.include?(:chrome) names << [:ipad, :android, :kindle] if names.include?(:tablet) names.flatten.compact.uniq end # :nodoc: def query_version(version) version.ensure_string.strip.parameterize.to_sym == :capable ? check_capable_browser : check_version(version) end # :nodoc: def check_version(version) parser = StringScanner.new(version) rv = true until parser.eos? token = parser.scan_until(/(?=&&)|\Z/) parser.skip_until(/&&|\Z/) operator, version = parse_version_token(token) rv &&= @version.send(operator, Versionomy.parse(version)) break unless rv end rv end # :nodoc: def check_capable_browser check_capable_browser_engines || check_capable_browser_recents end # :nodoc: def check_capable_browser_engines chrome? || safari? || check_capable_browser_recents end # :nodoc: def check_capable_browser_recents (firefox? && @version >= "28") || (msie? && @version >= "10") || (opera? && @version >= "15") end # :nodoc: def parse_version_token(token) mo = VERSION_TOKEN.match(token) raise ArgumentError, "Invalid version check: #{token}." unless mo [mo["operator"], mo["version"]].map(&:strip) end end |
- (Hash) human_languages (readonly)
Returns The human-readable list of accepted languages.
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 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 69 70 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 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 |
# File 'lib/brauser/browser.rb', line 28 class Browser attr_accessor :agent, :accept_language attr_reader :name, :human_name, :version, :platform, :human_platform, :languages, :human_languages alias_attribute :ua, :agent # Creates a new browser. # # @param agent [String] The User-Agent HTTP header. # @param accept_language [String] The Accept-Language HTTP header. def initialize(agent = "", accept_language = "") @agent = agent @accept_language = accept_language parse 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. 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 alias_method :meta, :classes alias_method :to_s, :classes # 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. def accepts?(*languages) languages = normalize_query_arguments(languages) (@languages.keys & languages).present? end # Checks if the browser matches a specific query. # # @param name [Symbol|Array] The list of names to check. Also, this meta-name is supported: `:tablet`. # @param engine [Symbol|Array] Alias for `name`, **which has precedence over this.** # @param version [String] 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] The list of platforms to check. # @param languages [Symbol|Array] The list of languages to check. # @return [Boolean] `true` if browser match the query, `false` otherwise. 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. def method_missing(method, *args, &block) method.to_s =~ /(.+)\?$/ ? is?(name: Regexp.last_match[1]) : super(method, *args, &block) end private # :nodoc: VERSION_TOKEN = /(?<operator>>=|<=|<|>|!=|(={1,2}))\s*(?<version>.+)/ # :nodoc: def parse parser = Brauser::Parser.new parse_agent(parser) parse_languages(parser) end # :nodoc: def parse_agent(parser) agent = parser.parse_agent(@agent) if agent @name, @version, @platform = *agent @human_name = Brauser::Definitions.browsers[@name].try(:name) || "Unknown Browser" @human_platform = Brauser::Definitions.platforms[@platform].try(:name) || "Unknown Platform" else @name = @platform = Brauser::Value.new(:unknown) @human_name = @human_platform = Brauser::Value.new("Unknown") @version = Brauser::Value.new(Versionomy.parse("0.0")) end end # :nodoc: def parse_languages(parser) languages = parser.parse_accept_language(@accept_language) @languages = languages @human_languages = languages.reduce({}) { |rv, (code, priority)| rv[Brauser::Definitions.languages[code].name] = priority rv } end # :nodoc: def name_to_str(name) if name name = "" if name.is_a?(TrueClass) rv = @name rv = [:msie_compatibility, :msie] if rv == :msie_compatibility rv.ensure_array(no_duplicates: true) { |n| "#{name}#{n}" } else nil end end # :nodoc: def version_to_str(version) if version version = "version-" if version.is_a?(TrueClass) version_str = @version.values_array.reduce([]) { |rv, current| rv << [rv.last, current].compact.join("_") rv } version_str.map { |v| "#{version}#{v}" } else nil end end # :nodoc: def platform_to_str(platform) if platform platform = "platform-" if platform.is_a?(TrueClass) "#{platform}#{@platform}" else nil end end # :nodoc: def normalize_query_arguments(arguments) sanitizer = ->(a) { a.ensure_string.downcase.gsub("_", "-").to_sym } arguments ? arguments.ensure_array(no_duplicates: true, compact: true, flatten: true, &sanitizer) : nil end # :nodoc: def apply_aliases(names) names << [:msie, :msie_compatibility] if (names & [:ie, :msie]).present? names << [:chromium] if names.include?(:chrome) names << [:ipad, :android, :kindle] if names.include?(:tablet) names.flatten.compact.uniq end # :nodoc: def query_version(version) version.ensure_string.strip.parameterize.to_sym == :capable ? check_capable_browser : check_version(version) end # :nodoc: def check_version(version) parser = StringScanner.new(version) rv = true until parser.eos? token = parser.scan_until(/(?=&&)|\Z/) parser.skip_until(/&&|\Z/) operator, version = parse_version_token(token) rv &&= @version.send(operator, Versionomy.parse(version)) break unless rv end rv end # :nodoc: def check_capable_browser check_capable_browser_engines || check_capable_browser_recents end # :nodoc: def check_capable_browser_engines chrome? || safari? || check_capable_browser_recents end # :nodoc: def check_capable_browser_recents (firefox? && @version >= "28") || (msie? && @version >= "10") || (opera? && @version >= "15") end # :nodoc: def parse_version_token(token) mo = VERSION_TOKEN.match(token) raise ArgumentError, "Invalid version check: #{token}." unless mo [mo["operator"], mo["version"]].map(&:strip) end end |
- (String) human_name (readonly)
Returns The human-readable current browser name.
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 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 69 70 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 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 |
# File 'lib/brauser/browser.rb', line 28 class Browser attr_accessor :agent, :accept_language attr_reader :name, :human_name, :version, :platform, :human_platform, :languages, :human_languages alias_attribute :ua, :agent # Creates a new browser. # # @param agent [String] The User-Agent HTTP header. # @param accept_language [String] The Accept-Language HTTP header. def initialize(agent = "", accept_language = "") @agent = agent @accept_language = accept_language parse 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. 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 alias_method :meta, :classes alias_method :to_s, :classes # 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. def accepts?(*languages) languages = normalize_query_arguments(languages) (@languages.keys & languages).present? end # Checks if the browser matches a specific query. # # @param name [Symbol|Array] The list of names to check. Also, this meta-name is supported: `:tablet`. # @param engine [Symbol|Array] Alias for `name`, **which has precedence over this.** # @param version [String] 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] The list of platforms to check. # @param languages [Symbol|Array] The list of languages to check. # @return [Boolean] `true` if browser match the query, `false` otherwise. 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. def method_missing(method, *args, &block) method.to_s =~ /(.+)\?$/ ? is?(name: Regexp.last_match[1]) : super(method, *args, &block) end private # :nodoc: VERSION_TOKEN = /(?<operator>>=|<=|<|>|!=|(={1,2}))\s*(?<version>.+)/ # :nodoc: def parse parser = Brauser::Parser.new parse_agent(parser) parse_languages(parser) end # :nodoc: def parse_agent(parser) agent = parser.parse_agent(@agent) if agent @name, @version, @platform = *agent @human_name = Brauser::Definitions.browsers[@name].try(:name) || "Unknown Browser" @human_platform = Brauser::Definitions.platforms[@platform].try(:name) || "Unknown Platform" else @name = @platform = Brauser::Value.new(:unknown) @human_name = @human_platform = Brauser::Value.new("Unknown") @version = Brauser::Value.new(Versionomy.parse("0.0")) end end # :nodoc: def parse_languages(parser) languages = parser.parse_accept_language(@accept_language) @languages = languages @human_languages = languages.reduce({}) { |rv, (code, priority)| rv[Brauser::Definitions.languages[code].name] = priority rv } end # :nodoc: def name_to_str(name) if name name = "" if name.is_a?(TrueClass) rv = @name rv = [:msie_compatibility, :msie] if rv == :msie_compatibility rv.ensure_array(no_duplicates: true) { |n| "#{name}#{n}" } else nil end end # :nodoc: def version_to_str(version) if version version = "version-" if version.is_a?(TrueClass) version_str = @version.values_array.reduce([]) { |rv, current| rv << [rv.last, current].compact.join("_") rv } version_str.map { |v| "#{version}#{v}" } else nil end end # :nodoc: def platform_to_str(platform) if platform platform = "platform-" if platform.is_a?(TrueClass) "#{platform}#{@platform}" else nil end end # :nodoc: def normalize_query_arguments(arguments) sanitizer = ->(a) { a.ensure_string.downcase.gsub("_", "-").to_sym } arguments ? arguments.ensure_array(no_duplicates: true, compact: true, flatten: true, &sanitizer) : nil end # :nodoc: def apply_aliases(names) names << [:msie, :msie_compatibility] if (names & [:ie, :msie]).present? names << [:chromium] if names.include?(:chrome) names << [:ipad, :android, :kindle] if names.include?(:tablet) names.flatten.compact.uniq end # :nodoc: def query_version(version) version.ensure_string.strip.parameterize.to_sym == :capable ? check_capable_browser : check_version(version) end # :nodoc: def check_version(version) parser = StringScanner.new(version) rv = true until parser.eos? token = parser.scan_until(/(?=&&)|\Z/) parser.skip_until(/&&|\Z/) operator, version = parse_version_token(token) rv &&= @version.send(operator, Versionomy.parse(version)) break unless rv end rv end # :nodoc: def check_capable_browser check_capable_browser_engines || check_capable_browser_recents end # :nodoc: def check_capable_browser_engines chrome? || safari? || check_capable_browser_recents end # :nodoc: def check_capable_browser_recents (firefox? && @version >= "28") || (msie? && @version >= "10") || (opera? && @version >= "15") end # :nodoc: def parse_version_token(token) mo = VERSION_TOKEN.match(token) raise ArgumentError, "Invalid version check: #{token}." unless mo [mo["operator"], mo["version"]].map(&:strip) end end |
- (String) human_platform (readonly)
Returns The human-readable current browser platform.
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 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 69 70 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 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 |
# File 'lib/brauser/browser.rb', line 28 class Browser attr_accessor :agent, :accept_language attr_reader :name, :human_name, :version, :platform, :human_platform, :languages, :human_languages alias_attribute :ua, :agent # Creates a new browser. # # @param agent [String] The User-Agent HTTP header. # @param accept_language [String] The Accept-Language HTTP header. def initialize(agent = "", accept_language = "") @agent = agent @accept_language = accept_language parse 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. 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 alias_method :meta, :classes alias_method :to_s, :classes # 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. def accepts?(*languages) languages = normalize_query_arguments(languages) (@languages.keys & languages).present? end # Checks if the browser matches a specific query. # # @param name [Symbol|Array] The list of names to check. Also, this meta-name is supported: `:tablet`. # @param engine [Symbol|Array] Alias for `name`, **which has precedence over this.** # @param version [String] 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] The list of platforms to check. # @param languages [Symbol|Array] The list of languages to check. # @return [Boolean] `true` if browser match the query, `false` otherwise. 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. def method_missing(method, *args, &block) method.to_s =~ /(.+)\?$/ ? is?(name: Regexp.last_match[1]) : super(method, *args, &block) end private # :nodoc: VERSION_TOKEN = /(?<operator>>=|<=|<|>|!=|(={1,2}))\s*(?<version>.+)/ # :nodoc: def parse parser = Brauser::Parser.new parse_agent(parser) parse_languages(parser) end # :nodoc: def parse_agent(parser) agent = parser.parse_agent(@agent) if agent @name, @version, @platform = *agent @human_name = Brauser::Definitions.browsers[@name].try(:name) || "Unknown Browser" @human_platform = Brauser::Definitions.platforms[@platform].try(:name) || "Unknown Platform" else @name = @platform = Brauser::Value.new(:unknown) @human_name = @human_platform = Brauser::Value.new("Unknown") @version = Brauser::Value.new(Versionomy.parse("0.0")) end end # :nodoc: def parse_languages(parser) languages = parser.parse_accept_language(@accept_language) @languages = languages @human_languages = languages.reduce({}) { |rv, (code, priority)| rv[Brauser::Definitions.languages[code].name] = priority rv } end # :nodoc: def name_to_str(name) if name name = "" if name.is_a?(TrueClass) rv = @name rv = [:msie_compatibility, :msie] if rv == :msie_compatibility rv.ensure_array(no_duplicates: true) { |n| "#{name}#{n}" } else nil end end # :nodoc: def version_to_str(version) if version version = "version-" if version.is_a?(TrueClass) version_str = @version.values_array.reduce([]) { |rv, current| rv << [rv.last, current].compact.join("_") rv } version_str.map { |v| "#{version}#{v}" } else nil end end # :nodoc: def platform_to_str(platform) if platform platform = "platform-" if platform.is_a?(TrueClass) "#{platform}#{@platform}" else nil end end # :nodoc: def normalize_query_arguments(arguments) sanitizer = ->(a) { a.ensure_string.downcase.gsub("_", "-").to_sym } arguments ? arguments.ensure_array(no_duplicates: true, compact: true, flatten: true, &sanitizer) : nil end # :nodoc: def apply_aliases(names) names << [:msie, :msie_compatibility] if (names & [:ie, :msie]).present? names << [:chromium] if names.include?(:chrome) names << [:ipad, :android, :kindle] if names.include?(:tablet) names.flatten.compact.uniq end # :nodoc: def query_version(version) version.ensure_string.strip.parameterize.to_sym == :capable ? check_capable_browser : check_version(version) end # :nodoc: def check_version(version) parser = StringScanner.new(version) rv = true until parser.eos? token = parser.scan_until(/(?=&&)|\Z/) parser.skip_until(/&&|\Z/) operator, version = parse_version_token(token) rv &&= @version.send(operator, Versionomy.parse(version)) break unless rv end rv end # :nodoc: def check_capable_browser check_capable_browser_engines || check_capable_browser_recents end # :nodoc: def check_capable_browser_engines chrome? || safari? || check_capable_browser_recents end # :nodoc: def check_capable_browser_recents (firefox? && @version >= "28") || (msie? && @version >= "10") || (opera? && @version >= "15") end # :nodoc: def parse_version_token(token) mo = VERSION_TOKEN.match(token) raise ArgumentError, "Invalid version check: #{token}." unless mo [mo["operator"], mo["version"]].map(&:strip) end end |
- (Hash) languages (readonly)
Returns The accepted languages.
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 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 69 70 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 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 |
# File 'lib/brauser/browser.rb', line 28 class Browser attr_accessor :agent, :accept_language attr_reader :name, :human_name, :version, :platform, :human_platform, :languages, :human_languages alias_attribute :ua, :agent # Creates a new browser. # # @param agent [String] The User-Agent HTTP header. # @param accept_language [String] The Accept-Language HTTP header. def initialize(agent = "", accept_language = "") @agent = agent @accept_language = accept_language parse 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. 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 alias_method :meta, :classes alias_method :to_s, :classes # 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. def accepts?(*languages) languages = normalize_query_arguments(languages) (@languages.keys & languages).present? end # Checks if the browser matches a specific query. # # @param name [Symbol|Array] The list of names to check. Also, this meta-name is supported: `:tablet`. # @param engine [Symbol|Array] Alias for `name`, **which has precedence over this.** # @param version [String] 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] The list of platforms to check. # @param languages [Symbol|Array] The list of languages to check. # @return [Boolean] `true` if browser match the query, `false` otherwise. 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. def method_missing(method, *args, &block) method.to_s =~ /(.+)\?$/ ? is?(name: Regexp.last_match[1]) : super(method, *args, &block) end private # :nodoc: VERSION_TOKEN = /(?<operator>>=|<=|<|>|!=|(={1,2}))\s*(?<version>.+)/ # :nodoc: def parse parser = Brauser::Parser.new parse_agent(parser) parse_languages(parser) end # :nodoc: def parse_agent(parser) agent = parser.parse_agent(@agent) if agent @name, @version, @platform = *agent @human_name = Brauser::Definitions.browsers[@name].try(:name) || "Unknown Browser" @human_platform = Brauser::Definitions.platforms[@platform].try(:name) || "Unknown Platform" else @name = @platform = Brauser::Value.new(:unknown) @human_name = @human_platform = Brauser::Value.new("Unknown") @version = Brauser::Value.new(Versionomy.parse("0.0")) end end # :nodoc: def parse_languages(parser) languages = parser.parse_accept_language(@accept_language) @languages = languages @human_languages = languages.reduce({}) { |rv, (code, priority)| rv[Brauser::Definitions.languages[code].name] = priority rv } end # :nodoc: def name_to_str(name) if name name = "" if name.is_a?(TrueClass) rv = @name rv = [:msie_compatibility, :msie] if rv == :msie_compatibility rv.ensure_array(no_duplicates: true) { |n| "#{name}#{n}" } else nil end end # :nodoc: def version_to_str(version) if version version = "version-" if version.is_a?(TrueClass) version_str = @version.values_array.reduce([]) { |rv, current| rv << [rv.last, current].compact.join("_") rv } version_str.map { |v| "#{version}#{v}" } else nil end end # :nodoc: def platform_to_str(platform) if platform platform = "platform-" if platform.is_a?(TrueClass) "#{platform}#{@platform}" else nil end end # :nodoc: def normalize_query_arguments(arguments) sanitizer = ->(a) { a.ensure_string.downcase.gsub("_", "-").to_sym } arguments ? arguments.ensure_array(no_duplicates: true, compact: true, flatten: true, &sanitizer) : nil end # :nodoc: def apply_aliases(names) names << [:msie, :msie_compatibility] if (names & [:ie, :msie]).present? names << [:chromium] if names.include?(:chrome) names << [:ipad, :android, :kindle] if names.include?(:tablet) names.flatten.compact.uniq end # :nodoc: def query_version(version) version.ensure_string.strip.parameterize.to_sym == :capable ? check_capable_browser : check_version(version) end # :nodoc: def check_version(version) parser = StringScanner.new(version) rv = true until parser.eos? token = parser.scan_until(/(?=&&)|\Z/) parser.skip_until(/&&|\Z/) operator, version = parse_version_token(token) rv &&= @version.send(operator, Versionomy.parse(version)) break unless rv end rv end # :nodoc: def check_capable_browser check_capable_browser_engines || check_capable_browser_recents end # :nodoc: def check_capable_browser_engines chrome? || safari? || check_capable_browser_recents end # :nodoc: def check_capable_browser_recents (firefox? && @version >= "28") || (msie? && @version >= "10") || (opera? && @version >= "15") end # :nodoc: def parse_version_token(token) mo = VERSION_TOKEN.match(token) raise ArgumentError, "Invalid version check: #{token}." unless mo [mo["operator"], mo["version"]].map(&:strip) end end |
- (Symbol) name (readonly)
Returns The current browser name.
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 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 69 70 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 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 |
# File 'lib/brauser/browser.rb', line 28 class Browser attr_accessor :agent, :accept_language attr_reader :name, :human_name, :version, :platform, :human_platform, :languages, :human_languages alias_attribute :ua, :agent # Creates a new browser. # # @param agent [String] The User-Agent HTTP header. # @param accept_language [String] The Accept-Language HTTP header. def initialize(agent = "", accept_language = "") @agent = agent @accept_language = accept_language parse 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. 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 alias_method :meta, :classes alias_method :to_s, :classes # 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. def accepts?(*languages) languages = normalize_query_arguments(languages) (@languages.keys & languages).present? end # Checks if the browser matches a specific query. # # @param name [Symbol|Array] The list of names to check. Also, this meta-name is supported: `:tablet`. # @param engine [Symbol|Array] Alias for `name`, **which has precedence over this.** # @param version [String] 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] The list of platforms to check. # @param languages [Symbol|Array] The list of languages to check. # @return [Boolean] `true` if browser match the query, `false` otherwise. 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. def method_missing(method, *args, &block) method.to_s =~ /(.+)\?$/ ? is?(name: Regexp.last_match[1]) : super(method, *args, &block) end private # :nodoc: VERSION_TOKEN = /(?<operator>>=|<=|<|>|!=|(={1,2}))\s*(?<version>.+)/ # :nodoc: def parse parser = Brauser::Parser.new parse_agent(parser) parse_languages(parser) end # :nodoc: def parse_agent(parser) agent = parser.parse_agent(@agent) if agent @name, @version, @platform = *agent @human_name = Brauser::Definitions.browsers[@name].try(:name) || "Unknown Browser" @human_platform = Brauser::Definitions.platforms[@platform].try(:name) || "Unknown Platform" else @name = @platform = Brauser::Value.new(:unknown) @human_name = @human_platform = Brauser::Value.new("Unknown") @version = Brauser::Value.new(Versionomy.parse("0.0")) end end # :nodoc: def parse_languages(parser) languages = parser.parse_accept_language(@accept_language) @languages = languages @human_languages = languages.reduce({}) { |rv, (code, priority)| rv[Brauser::Definitions.languages[code].name] = priority rv } end # :nodoc: def name_to_str(name) if name name = "" if name.is_a?(TrueClass) rv = @name rv = [:msie_compatibility, :msie] if rv == :msie_compatibility rv.ensure_array(no_duplicates: true) { |n| "#{name}#{n}" } else nil end end # :nodoc: def version_to_str(version) if version version = "version-" if version.is_a?(TrueClass) version_str = @version.values_array.reduce([]) { |rv, current| rv << [rv.last, current].compact.join("_") rv } version_str.map { |v| "#{version}#{v}" } else nil end end # :nodoc: def platform_to_str(platform) if platform platform = "platform-" if platform.is_a?(TrueClass) "#{platform}#{@platform}" else nil end end # :nodoc: def normalize_query_arguments(arguments) sanitizer = ->(a) { a.ensure_string.downcase.gsub("_", "-").to_sym } arguments ? arguments.ensure_array(no_duplicates: true, compact: true, flatten: true, &sanitizer) : nil end # :nodoc: def apply_aliases(names) names << [:msie, :msie_compatibility] if (names & [:ie, :msie]).present? names << [:chromium] if names.include?(:chrome) names << [:ipad, :android, :kindle] if names.include?(:tablet) names.flatten.compact.uniq end # :nodoc: def query_version(version) version.ensure_string.strip.parameterize.to_sym == :capable ? check_capable_browser : check_version(version) end # :nodoc: def check_version(version) parser = StringScanner.new(version) rv = true until parser.eos? token = parser.scan_until(/(?=&&)|\Z/) parser.skip_until(/&&|\Z/) operator, version = parse_version_token(token) rv &&= @version.send(operator, Versionomy.parse(version)) break unless rv end rv end # :nodoc: def check_capable_browser check_capable_browser_engines || check_capable_browser_recents end # :nodoc: def check_capable_browser_engines chrome? || safari? || check_capable_browser_recents end # :nodoc: def check_capable_browser_recents (firefox? && @version >= "28") || (msie? && @version >= "10") || (opera? && @version >= "15") end # :nodoc: def parse_version_token(token) mo = VERSION_TOKEN.match(token) raise ArgumentError, "Invalid version check: #{token}." unless mo [mo["operator"], mo["version"]].map(&:strip) end end |
- (Symbol) platform (readonly)
Returns The current browser platform.
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 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 69 70 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 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 |
# File 'lib/brauser/browser.rb', line 28 class Browser attr_accessor :agent, :accept_language attr_reader :name, :human_name, :version, :platform, :human_platform, :languages, :human_languages alias_attribute :ua, :agent # Creates a new browser. # # @param agent [String] The User-Agent HTTP header. # @param accept_language [String] The Accept-Language HTTP header. def initialize(agent = "", accept_language = "") @agent = agent @accept_language = accept_language parse 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. 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 alias_method :meta, :classes alias_method :to_s, :classes # 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. def accepts?(*languages) languages = normalize_query_arguments(languages) (@languages.keys & languages).present? end # Checks if the browser matches a specific query. # # @param name [Symbol|Array] The list of names to check. Also, this meta-name is supported: `:tablet`. # @param engine [Symbol|Array] Alias for `name`, **which has precedence over this.** # @param version [String] 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] The list of platforms to check. # @param languages [Symbol|Array] The list of languages to check. # @return [Boolean] `true` if browser match the query, `false` otherwise. 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. def method_missing(method, *args, &block) method.to_s =~ /(.+)\?$/ ? is?(name: Regexp.last_match[1]) : super(method, *args, &block) end private # :nodoc: VERSION_TOKEN = /(?<operator>>=|<=|<|>|!=|(={1,2}))\s*(?<version>.+)/ # :nodoc: def parse parser = Brauser::Parser.new parse_agent(parser) parse_languages(parser) end # :nodoc: def parse_agent(parser) agent = parser.parse_agent(@agent) if agent @name, @version, @platform = *agent @human_name = Brauser::Definitions.browsers[@name].try(:name) || "Unknown Browser" @human_platform = Brauser::Definitions.platforms[@platform].try(:name) || "Unknown Platform" else @name = @platform = Brauser::Value.new(:unknown) @human_name = @human_platform = Brauser::Value.new("Unknown") @version = Brauser::Value.new(Versionomy.parse("0.0")) end end # :nodoc: def parse_languages(parser) languages = parser.parse_accept_language(@accept_language) @languages = languages @human_languages = languages.reduce({}) { |rv, (code, priority)| rv[Brauser::Definitions.languages[code].name] = priority rv } end # :nodoc: def name_to_str(name) if name name = "" if name.is_a?(TrueClass) rv = @name rv = [:msie_compatibility, :msie] if rv == :msie_compatibility rv.ensure_array(no_duplicates: true) { |n| "#{name}#{n}" } else nil end end # :nodoc: def version_to_str(version) if version version = "version-" if version.is_a?(TrueClass) version_str = @version.values_array.reduce([]) { |rv, current| rv << [rv.last, current].compact.join("_") rv } version_str.map { |v| "#{version}#{v}" } else nil end end # :nodoc: def platform_to_str(platform) if platform platform = "platform-" if platform.is_a?(TrueClass) "#{platform}#{@platform}" else nil end end # :nodoc: def normalize_query_arguments(arguments) sanitizer = ->(a) { a.ensure_string.downcase.gsub("_", "-").to_sym } arguments ? arguments.ensure_array(no_duplicates: true, compact: true, flatten: true, &sanitizer) : nil end # :nodoc: def apply_aliases(names) names << [:msie, :msie_compatibility] if (names & [:ie, :msie]).present? names << [:chromium] if names.include?(:chrome) names << [:ipad, :android, :kindle] if names.include?(:tablet) names.flatten.compact.uniq end # :nodoc: def query_version(version) version.ensure_string.strip.parameterize.to_sym == :capable ? check_capable_browser : check_version(version) end # :nodoc: def check_version(version) parser = StringScanner.new(version) rv = true until parser.eos? token = parser.scan_until(/(?=&&)|\Z/) parser.skip_until(/&&|\Z/) operator, version = parse_version_token(token) rv &&= @version.send(operator, Versionomy.parse(version)) break unless rv end rv end # :nodoc: def check_capable_browser check_capable_browser_engines || check_capable_browser_recents end # :nodoc: def check_capable_browser_engines chrome? || safari? || check_capable_browser_recents end # :nodoc: def check_capable_browser_recents (firefox? && @version >= "28") || (msie? && @version >= "10") || (opera? && @version >= "15") end # :nodoc: def parse_version_token(token) mo = VERSION_TOKEN.match(token) raise ArgumentError, "Invalid version check: #{token}." unless mo [mo["operator"], mo["version"]].map(&:strip) end end |
- (Versionomy::Value) version (readonly)
Returns The current browser version.
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 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 69 70 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 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 |
# File 'lib/brauser/browser.rb', line 28 class Browser attr_accessor :agent, :accept_language attr_reader :name, :human_name, :version, :platform, :human_platform, :languages, :human_languages alias_attribute :ua, :agent # Creates a new browser. # # @param agent [String] The User-Agent HTTP header. # @param accept_language [String] The Accept-Language HTTP header. def initialize(agent = "", accept_language = "") @agent = agent @accept_language = accept_language parse 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. 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 alias_method :meta, :classes alias_method :to_s, :classes # 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. def accepts?(*languages) languages = normalize_query_arguments(languages) (@languages.keys & languages).present? end # Checks if the browser matches a specific query. # # @param name [Symbol|Array] The list of names to check. Also, this meta-name is supported: `:tablet`. # @param engine [Symbol|Array] Alias for `name`, **which has precedence over this.** # @param version [String] 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] The list of platforms to check. # @param languages [Symbol|Array] The list of languages to check. # @return [Boolean] `true` if browser match the query, `false` otherwise. 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. def method_missing(method, *args, &block) method.to_s =~ /(.+)\?$/ ? is?(name: Regexp.last_match[1]) : super(method, *args, &block) end private # :nodoc: VERSION_TOKEN = /(?<operator>>=|<=|<|>|!=|(={1,2}))\s*(?<version>.+)/ # :nodoc: def parse parser = Brauser::Parser.new parse_agent(parser) parse_languages(parser) end # :nodoc: def parse_agent(parser) agent = parser.parse_agent(@agent) if agent @name, @version, @platform = *agent @human_name = Brauser::Definitions.browsers[@name].try(:name) || "Unknown Browser" @human_platform = Brauser::Definitions.platforms[@platform].try(:name) || "Unknown Platform" else @name = @platform = Brauser::Value.new(:unknown) @human_name = @human_platform = Brauser::Value.new("Unknown") @version = Brauser::Value.new(Versionomy.parse("0.0")) end end # :nodoc: def parse_languages(parser) languages = parser.parse_accept_language(@accept_language) @languages = languages @human_languages = languages.reduce({}) { |rv, (code, priority)| rv[Brauser::Definitions.languages[code].name] = priority rv } end # :nodoc: def name_to_str(name) if name name = "" if name.is_a?(TrueClass) rv = @name rv = [:msie_compatibility, :msie] if rv == :msie_compatibility rv.ensure_array(no_duplicates: true) { |n| "#{name}#{n}" } else nil end end # :nodoc: def version_to_str(version) if version version = "version-" if version.is_a?(TrueClass) version_str = @version.values_array.reduce([]) { |rv, current| rv << [rv.last, current].compact.join("_") rv } version_str.map { |v| "#{version}#{v}" } else nil end end # :nodoc: def platform_to_str(platform) if platform platform = "platform-" if platform.is_a?(TrueClass) "#{platform}#{@platform}" else nil end end # :nodoc: def normalize_query_arguments(arguments) sanitizer = ->(a) { a.ensure_string.downcase.gsub("_", "-").to_sym } arguments ? arguments.ensure_array(no_duplicates: true, compact: true, flatten: true, &sanitizer) : nil end # :nodoc: def apply_aliases(names) names << [:msie, :msie_compatibility] if (names & [:ie, :msie]).present? names << [:chromium] if names.include?(:chrome) names << [:ipad, :android, :kindle] if names.include?(:tablet) names.flatten.compact.uniq end # :nodoc: def query_version(version) version.ensure_string.strip.parameterize.to_sym == :capable ? check_capable_browser : check_version(version) end # :nodoc: def check_version(version) parser = StringScanner.new(version) rv = true until parser.eos? token = parser.scan_until(/(?=&&)|\Z/) parser.skip_until(/&&|\Z/) operator, version = parse_version_token(token) rv &&= @version.send(operator, Versionomy.parse(version)) break unless rv end rv end # :nodoc: def check_capable_browser check_capable_browser_engines || check_capable_browser_recents end # :nodoc: def check_capable_browser_engines chrome? || safari? || check_capable_browser_recents end # :nodoc: def check_capable_browser_recents (firefox? && @version >= "28") || (msie? && @version >= "10") || (opera? && @version >= "15") end # :nodoc: def parse_version_token(token) mo = VERSION_TOKEN.match(token) raise ArgumentError, "Invalid version check: #{token}." unless mo [mo["operator"], mo["version"]].map(&:strip) end end |
Instance Method Details
- (Boolean) accepts?(*languages)
Checks if the browser accepts a specific language or languages.
69 70 71 72 |
# File 'lib/brauser/browser.rb', line 69 def accepts?(*languages) languages = normalize_query_arguments(languages) (@languages.keys & languages).present? end |
- (String|Array) classes(join = " ", name: true, version: true, platform: true) Also known as: meta, to_s
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.
58 59 60 61 |
# 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 |
- (Boolean) is?(name: nil, engine: nil, version: nil, platform: nil, languages: nil)
Checks if the browser matches a specific query.
84 85 86 87 88 89 90 91 |
# 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 |