browserino 2.5.2 → 2.5.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 9a9d892c5ad880cdc418ac696b69dd465ec03820
4
- data.tar.gz: 9addf30f035f5c74ada82cf47151c6ec9816667e
3
+ metadata.gz: 3625bb25aba01e627a8bd14d19971350f039c2f2
4
+ data.tar.gz: 15d14f30031beec4d0ae944b38a37600c19d57e9
5
5
  SHA512:
6
- metadata.gz: e9276a510795d45e68ae8e717c07d340258c2f956e2b778fb4d2c632ba45dccc53e256f020cc8eb2be888f515ae2fbc23d70d00ef7d74f91505c38fb2383ee69
7
- data.tar.gz: ff0ca377b67f2a492d3abce13b2c864a85f02077af6c8e49b5d021652e7238a0b8c0e38f32adef4b01cc7b6f5f1249b6aff1fc0f3b52871cc48a11ad29383750
6
+ metadata.gz: 7bded8f6898e7a644bb021d78716cdf26becec40c6171887ef2ae55bd9da8898b65773636604f958cf3c559f57c9513b92ff19c59bbc2d302eba8b4cd6ea4df5
7
+ data.tar.gz: 844f0b75b0ebbdaaeee98e6bff3aa9e09bd2634789d3fc266b942c28bf0435fcd23f29c1d684420942e576a1e3f06c3783f3568bbd0bd50335ce09ab77071f68
data/CHANGELOG.md CHANGED
@@ -1,6 +1,14 @@
1
1
  ## CHANGELOG
2
2
  _dates are in dd-mm-yyyy format_
3
3
 
4
+ #### 12-01-2016 VERSION 2.5.0
5
+
6
+ - Added support for the Vivaldi browser
7
+ - New method `#vivaldi?`
8
+ - Added support for the bsd family of operating systems
9
+ - New method `#bsd?`
10
+ - Fixed using symbols for system version identification (e.g. `:vista` or `:el_capitan`) without a version number
11
+
4
12
  #### 11-01-2016 VERSION 2.4.1(.1)
5
13
 
6
14
  - Caching the agent object in Rails
@@ -42,7 +50,7 @@ _dates are in dd-mm-yyyy format_
42
50
  - Added more tests
43
51
  - Added more browsers to check: *(bolt, opera mini and ucbrowser)*
44
52
  - Added `#known?` method to check if the agent is known
45
- - Added a `#ua` method to return the User Agent string as given to `Browserino::parse()`
53
+ - Added a `#ua` method to return the User Agent string as given to `Browserino.parse()`
46
54
  - Added `#x64?` and `#x32?` convenience methods to check system architecture
47
55
  - Added `#mobile?` to check wether or not a user agent is mobile
48
56
  - Moved older changelogs to its own [CHANGELOG.md](https://github.com/SidOfc/browserino/blob/master/CHANGELOG.md) file
data/README.md CHANGED
@@ -10,9 +10,13 @@ This gem aims to provide information about the browser that your visitor is usin
10
10
  _dates are in dd-mm-yyyy format_
11
11
  _older changes can be found in the [CHANGELOG.md](https://github.com/SidOfc/browserino/blob/master/CHANGELOG.md)_
12
12
 
13
+ #### 19-01-2016 VERSION 2.5.3
14
+
15
+ - Minor refactoring of code
16
+
13
17
  #### 15-01-2016 VERSION 2.5.2
14
18
 
15
- - **DEPRECATE** Custom return values (passed through `Browserino::parse`) will no longer alter the output of the agent object
19
+ - **DEPRECATE** Custom return values (passed through `Browserino.parse`) will no longer alter the output of the agent object
16
20
  - Added support for windows phone detection
17
21
  - Added `windows_phone?` method
18
22
 
@@ -20,14 +24,6 @@ _older changes can be found in the [CHANGELOG.md](https://github.com/SidOfc/brow
20
24
 
21
25
  - Patched blackberry mapping, this used to be done by model number instead but is now corrected
22
26
 
23
- #### 12-01-2016 VERSION 2.5.0
24
-
25
- - Added support for the Vivaldi browser
26
- - New method `#vivaldi?`
27
- - Added support for the bsd family of operating systems
28
- - New method `#bsd?`
29
- - Fixed using symbols for system version identification (e.g. `:vista` or `:el_capitan`) without a version number
30
-
31
27
  ## Installation
32
28
 
33
29
  *supports ruby 1.9.3+*
@@ -56,7 +52,7 @@ require 'browserino'
56
52
  Afterwards you can simply call
57
53
 
58
54
  ```ruby
59
- Browserino::parse('<user agent>')
55
+ Browserino.parse('<user agent>')
60
56
  ```
61
57
 
62
58
  Or if you're using Rails *(>= 3.2.0)*, in your controllers you'll have access to an `agent` object
@@ -71,13 +67,13 @@ end
71
67
 
72
68
  The return value will always be `nil` if a value isn't found
73
69
 
74
- `Browserino::parse()` will return a `Browserino::Agent` object containing all the information parsed out of the supplied user agent.
70
+ `Browserino.parse()` will return a `Browserino::Agent` object containing all the information parsed out of the supplied user agent.
75
71
  On this object there are a few method calls you can do to retrieve information.
76
72
 
77
73
  ```ruby
78
74
  require 'browserino'
79
75
 
80
- agent = Browserino::parse('user-agent')
76
+ agent = Browserino.parse('user-agent')
81
77
 
82
78
  agent.browser_name
83
79
  # => 'safari'
data/bin/console CHANGED
@@ -3,13 +3,12 @@
3
3
  require "bundler/setup"
4
4
  require "browserino"
5
5
  require "pry"
6
- ua = 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/47.0.2526.80 Safari/537.36 Vivaldi/1.0.344.37'
7
- @agent = Browserino::parse(ua)
6
+ ua = 'Mozilla/5.0 (compatible; MSIE 9.0; Windows Phone OS 7.5; Trident/5.0; IEMobile/9.0)'
7
+ @agent = Browserino.parse(ua)
8
8
 
9
9
  puts "> @agent variable available parsed with: '#{ua}'\n\n"
10
10
  puts "> You can create a fresh object by using\n"
11
- puts "> Browserino::parse((string) ua, (any) unknown_alt = nil)\n\n"
12
- puts "> ua - The user agent you'd actually want to parse as a string"
13
- puts "> unknown_alt - A default value for unknown properties, default nil (recommended)\n\n"
11
+ puts "> Browserino.parse([string] ua)\n\n"
12
+ puts "> ua - The user agent you'd actually want to parse as a string"
14
13
 
15
14
  Pry.start
@@ -1,14 +1,17 @@
1
1
  module Browserino
2
2
  class Agent
3
- def initialize(ua, unknown = Browserino::UNKNOWN)
3
+ def initialize(ua)
4
4
  @ua = ua
5
5
  @not = false
6
6
 
7
- cleansed_ua = Browserino::strip_lies @ua
8
- name = Browserino::agent_id cleansed_ua
7
+ cleansed_ua = Browserino.strip_lies(@ua)
8
+ pat_name = Browser::name(cleansed_ua)
9
+ name = pat_name.to_s.downcase.gsub(/_/, ' ')
10
+ name = nil if name == ''
11
+
9
12
  info = {
10
13
  browser_name: name,
11
- browser_version: Browser::version(cleansed_ua, PATTERNS[:browser][name]),
14
+ browser_version: Browser::version(cleansed_ua, PATTERNS[:browser][pat_name]),
12
15
  engine_name: Engine::name(cleansed_ua),
13
16
  engine_version: Engine::version(cleansed_ua),
14
17
  system_name: OperatingSystem::name(cleansed_ua),
@@ -18,73 +21,58 @@ module Browserino
18
21
  bot_name: nil
19
22
  }
20
23
 
21
- if Browserino::PATTERNS[:bot].include? name
22
- info.merge!({browser_name: nil, browser_version: nil, bot_name: name})
24
+ if Browserino::PATTERNS[:bot].include? pat_name
25
+ info.merge!(browser_name: nil,
26
+ browser_version: nil,
27
+ bot_name: name)
23
28
  end
24
29
 
25
- @info = Browserino::check_for_aliases(info)
30
+ @info = Browserino.check_for_aliases(info)
26
31
  end
27
32
 
28
33
  def browser_name
29
- with_valid(@info[:browser_name]) do |v|
30
- v.to_s.downcase.gsub(/_/, ' ')
31
- end
34
+ @info[:browser_name]
32
35
  end
33
36
 
34
37
  def browser_version(opts = {})
35
38
  if ie? && engine_version && !opts[:compat]
36
39
  (engine_version.to_f + 4.0).to_s
37
40
  else
38
- with_valid(@info[:browser_version]) do |v|
39
- v.to_s.downcase.gsub('_', '.')
40
- end
41
+ @info[:browser_version]
41
42
  end
42
43
  end
43
44
 
44
45
  def engine_name
45
- with_valid(@info[:engine_name]) do |v|
46
- v.to_s.downcase
47
- end
46
+ @info[:engine_name]
48
47
  end
49
48
 
50
49
  def engine_version
51
- with_valid(@info[:engine_version]) do |v|
52
- v.to_s.downcase.gsub('_', '.')
53
- end
50
+ @info[:engine_version]
54
51
  end
55
52
 
56
53
  def system_name(opts = {})
57
54
  opts = {full: false}.merge(opts)
58
- name = with_valid(@info[:system_name]) do |v|
59
- v.to_s.downcase
60
- end
61
55
  if opts[:full]
62
- [name, fetch_system_version_name(name)]
56
+ [@info[:system_name], fetch_system_version_name(@info[:system_name])]
63
57
  else
64
- name
58
+ @info[:system_name]
65
59
  end
66
60
  end
67
61
 
68
62
  def system_version
69
- with_valid(@info[:system_version]) do |v|
70
- v.to_s.downcase.gsub('_', '.')
71
- end
63
+ @info[:system_version]
72
64
  end
73
65
 
74
66
  def system_architecture
75
- with_valid(@info[:system_architecture]) do |v|
76
- v.to_s.downcase
77
- end
67
+ @info[:system_architecture]
78
68
  end
79
69
 
80
70
  def locale
81
- with_valid(@info[:locale]) { |v| v.to_s.downcase }
71
+ @info[:locale]
82
72
  end
83
73
 
84
74
  def bot_name
85
- with_valid(@info[:bot_name]) do |v|
86
- v.to_s.downcase.gsub(/_/, ' ')
87
- end
75
+ @info[:bot_name]
88
76
  end
89
77
 
90
78
  def ua
@@ -96,7 +84,7 @@ module Browserino
96
84
  end
97
85
 
98
86
  def known?
99
- allow_inverted_return (browser_name != Browserino::UNKNOWN || bot_name != Browserino::UNKNOWN)
87
+ allow_inverted_return (!browser_name.nil? || !bot_name.nil?)
100
88
  end
101
89
 
102
90
  def mobile?
@@ -131,12 +119,11 @@ module Browserino
131
119
 
132
120
  def method_missing(method_sym, *args, &block)
133
121
  name = method_sym.to_s.gsub('?', '')
134
- res = case agent_or_system?(method_sym)
135
- when :system then correct_system?(name, *args)
136
- when :agent then correct_agent?(name, *args)
137
- else super
138
- end
139
- allow_inverted_return res
122
+ allow_inverted_return case agent_or_system?(method_sym)
123
+ when :system then correct_system?(name, *args)
124
+ when :agent then correct_agent?(name, *args)
125
+ else super
126
+ end
140
127
  end
141
128
 
142
129
  def respond_to?(method_sym)
@@ -229,20 +216,9 @@ module Browserino
229
216
  end
230
217
  end
231
218
 
232
- def with_valid(val)
233
- if val && (val != '' || val != false) && block_given?
234
- res = yield(val)
235
- return Browserino::UNKNOWN if res == ''
236
- res
237
- else
238
- Browserino::UNKNOWN
239
- end
240
- end
241
-
242
219
  def fetch_system_version_name(name)
243
220
  return Browserino::UNKNOWN if name.nil? || name == '' || !name
244
- const = name.upcase.gsub(/\s/, '_')
245
- name.downcase!
221
+ const = name.dup.upcase.gsub(/\s/, '_')
246
222
  version = if system_version == Browserino::UNKNOWN
247
223
  nil
248
224
  elsif name.match(/mac|ios|blackberry/i)
@@ -1,23 +1,15 @@
1
1
  module Browserino
2
2
  ALIAS = {
3
- browser_name: {
4
- 'ie' => ['msie']
5
- },
6
- browser_version: {},
7
- engine_name: {
8
- 'webkit' => ['applewebkit']
9
- },
10
- engine_version: {},
3
+ browser_name: { 'ie' => /msie/ },
4
+ engine_name: { 'webkit' => /applewebkit/ },
11
5
  system_name: {
12
- 'linux' => ['ubuntu', 'x11'],
13
- 'windows_phone' => ['windows phone os', 'windows phone']
6
+ 'linux' => /ubuntu|x11/,
7
+ 'windows_phone' => /windows\sphone/,
8
+ 'ios' => /ip(?:[ao]d|hone)/
14
9
  },
15
- system_version: {},
16
10
  system_architecture: {
17
- 'x64' => ['64', 'x86_64', 'amd64', 'wow64'],
18
- 'x32' => ['32', 'i686', 'i383', 'i386', 'x86_32']
19
- },
20
- bot_name: {},
21
- locale: {}
11
+ 'x64' => /(?:x86_|amd|wow)?64/,
12
+ 'x32' => /(?:(?:x86_)?32|i[36]8[36])/
13
+ }
22
14
  }
23
15
  end
@@ -1,8 +1,23 @@
1
1
  module Browserino
2
2
  module Browser
3
+ def self.name(ua)
4
+ name = nil
5
+ patterns = PATTERNS[:browser].merge(PATTERNS[:bot])
6
+ agents = patterns.keys
7
+
8
+ until agents.empty? || !name.nil?
9
+ tmp = agents.shift
10
+ name = tmp if (ua.match(patterns[tmp][:name]))
11
+ end
12
+
13
+ name
14
+ end
15
+
3
16
  def self.version(ua, patterns)
4
17
  if patterns
5
- Browserino::extract_match(ua.match(patterns[:version]), :version)
18
+ Browserino::extract_match(ua.match(patterns[:version]), :version) do |v|
19
+ v.gsub('_', '.')
20
+ end
6
21
  else
7
22
  UNKNOWN
8
23
  end
@@ -1,11 +1,13 @@
1
1
  module Browserino
2
2
  module Engine
3
3
  def self.name(ua)
4
- Browserino::extract_match(ua.match(PATTERNS[:engine][:name]), :name) || UNKNOWN
4
+ Browserino::extract_match(ua.match(PATTERNS[:engine][:name]), :name)
5
5
  end
6
6
 
7
7
  def self.version(ua)
8
- Browserino::extract_match(ua.match(PATTERNS[:engine][:version]), :version) || UNKNOWN
8
+ Browserino::extract_match(ua.match(PATTERNS[:engine][:version]), :version) do |v|
9
+ v.gsub('_', '.')
10
+ end
9
11
  end
10
12
  end
11
13
  end
@@ -4,7 +4,7 @@ module Browserino
4
4
  class ActionController
5
5
  module Base
6
6
  def agent
7
- @agent ||= Browserino::parse(request.headers['User-Agent'])
7
+ @agent ||= Browserino.parse(request.headers['User-Agent'])
8
8
  end
9
9
  end
10
10
  end
@@ -1,19 +1,21 @@
1
1
  module Browserino
2
2
  module OperatingSystem
3
3
  def self.name(ua)
4
- Browserino::extract_match(ua.match(PATTERNS[:operating_system][:name]), :name) || UNKNOWN
4
+ Browserino::extract_match(ua.match(PATTERNS[:operating_system][:name]), :name)
5
5
  end
6
6
 
7
7
  def self.version(ua)
8
- Browserino::extract_match(ua.match(PATTERNS[:operating_system][:version]), :version) || UNKNOWN
8
+ Browserino::extract_match(ua.match(PATTERNS[:operating_system][:version]), :version) do |v|
9
+ v.gsub('_', '.')
10
+ end
9
11
  end
10
12
 
11
13
  def self.architecture(ua)
12
- Browserino::extract_match(ua.match(PATTERNS[:operating_system][:architecture]), :architecture) || UNKNOWN
14
+ Browserino::extract_match(ua.match(PATTERNS[:operating_system][:architecture]), :architecture)
13
15
  end
14
16
 
15
17
  def self.locale(ua)
16
- Browserino::extract_match(ua.match(PATTERNS[:operating_system][:locale]), :locale) || UNKNOWN
18
+ Browserino::extract_match(ua.match(PATTERNS[:operating_system][:locale]), :locale)
17
19
  end
18
20
  end
19
21
  end
@@ -84,7 +84,7 @@ module Browserino
84
84
  },
85
85
 
86
86
  operating_system: {
87
- name: /(?<name>windows(?:\sphone(?:\sos)?)?|macintosh|android|ios|blackberry|linux|ubuntu|x11|bsd)/i,
87
+ name: /(?<name>windows(?:\sphone(?:\sos)?)?|macintosh|android|ip(?:[ao]d|hone)|blackberry|linux|ubuntu|x11|bsd)/i,
88
88
  version: /(?:windows(?:\sphone(?:\sos)?)?|nt|mac\sos\sx|android|(cpu\s|i)os|blackberry.*?version\/|bb)\s?(?<version>[\d\._]+)/i,
89
89
  architecture: /(?<architecture>((?:x|x86_|amd|wow)64)|i(3|6)86)/i,
90
90
  mobile: /bolt|nokia|samsung|mobi(?:le)?|android|i?p(?:[ao]d|hone)|bb\d+|blackberry|iemobile|fennec|bada|meego|vodafone|t\-mobile|opera\sm(?:ob|in)i/i,
@@ -1,3 +1,3 @@
1
1
  module Browserino
2
- VERSION = "2.5.2"
2
+ VERSION = "2.5.3"
3
3
  end
data/lib/browserino.rb CHANGED
@@ -24,20 +24,15 @@ require "browserino/operating_system"
24
24
  # require_relative "../spec/user_agents_browsers"
25
25
 
26
26
  module Browserino
27
- def self.parse(ua, unknown_alt = nil)
28
- if unknown_alt
29
- puts "The feature for using a custom return value is deprecated and will be removed in a future release. For now, your value will be ignored and nil will be the return value for unknown properties."
30
- end
31
- Agent.new(ua, UNKNOWN)
27
+ def self.parse(ua, _ = nil) # _ = nil maintains backwards compatibility
28
+ Agent.new ua
32
29
  end
33
30
 
34
31
  private
35
32
 
36
33
  def self.strip_lies(ua)
37
- #make iphone / ipad / ipod consistent
38
- ua = ua.gsub(/ip((a|o)d|hone)/i, 'ios')
39
34
  ua = ua.gsub(/(Mozilla\/[\d\.]+)/i, '')
40
- ua = ua.gsub(/9\.80/i, '')
35
+ ua = ua.gsub(/9\.80/, '') if /opera/i =~ ua
41
36
  ua = ua.gsub(/(?:apple)?webkit\/[\d\.]+/i, '') if /presto/i =~ ua
42
37
  ua = ua.gsub(/(?:ms)?ie/i, '') if /rv\:/i =~ ua
43
38
  ua = ua.gsub(/linux/i, '') if /android/i =~ ua
@@ -46,29 +41,22 @@ module Browserino
46
41
  ua
47
42
  end
48
43
 
49
- def self.check_for_aliases(hash)
50
- hash.inject({}) do |memo, kv|
51
- ls = ALIAS[kv.first].select { |k, m| true if m.include?(kv.last) }.keys.first || kv.last
52
- memo[kv.first] = ls
53
- memo
54
- end
55
- end
56
-
57
- def self.agent_id(ua)
58
- name = nil
59
- patterns = PATTERNS[:browser].merge(PATTERNS[:bot])
60
- browsers = patterns.keys
61
- until browsers.empty? || !name.nil?
62
- tmp = browsers.shift
63
- name = tmp if (ua.match(patterns[tmp][:name]))
44
+ def self.check_for_aliases(h)
45
+ ALIAS.each do |k, v|
46
+ h[k] = v.select { |n, re| n if h[k] =~ re }.keys.first || h[k]
64
47
  end
65
- name ||= UNKNOWN
48
+ h
66
49
  end
67
50
 
68
- def self.extract_match(match, sym, trim = true)
51
+ def self.extract_match(match, sym)
69
52
  if match && match.names.include?(sym.to_s)
70
- match[sym].strip! if trim
71
- match[sym].to_s.downcase
53
+ m = match[sym].to_s.downcase.strip
54
+ m = yield(m) if block_given?
55
+ if m && m.strip != ''
56
+ m
57
+ else
58
+ UNKNOWN
59
+ end
72
60
  else
73
61
  UNKNOWN
74
62
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: browserino
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.5.2
4
+ version: 2.5.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Sidney Liebrand
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2016-01-15 00:00:00.000000000 Z
11
+ date: 2016-01-19 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler