user_agent_parser 2.7.0 → 2.16.0
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 +4 -4
- data/lib/user_agent_parser/parser.rb +56 -43
- data/lib/user_agent_parser/version.rb +8 -3
- data/lib/user_agent_parser.rb +2 -2
- data/vendor/uap-core/regexes.yaml +925 -649
- metadata +11 -11
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: a48095209e88dd1c8b49954029334bddecbbb1facb44cbf3b2ed5e700b259541
|
4
|
+
data.tar.gz: 06b14639160f7cdc6f3b0cc0ffc88147cee9cd1b92dc8a10ad61a420edb4a5fb
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 94d1c6ad450918654bf5c6b9e9aaffd6ba3e28a01d17741a4443dc63110c30d310b81654de91297e9ce79ec31de3da40d8088d792cb100aca91d486b5410cf67
|
7
|
+
data.tar.gz: be73315646276e7ab2db8ce86cc54e5802fcd6cff84c8176c2af9db935911f853606edada097ac1ab471c5b9b4557363b27099412388599279790edf10882565
|
@@ -4,6 +4,8 @@ require 'yaml'
|
|
4
4
|
|
5
5
|
module UserAgentParser
|
6
6
|
class Parser
|
7
|
+
extend Gem::Deprecate
|
8
|
+
|
7
9
|
FAMILY_REPLACEMENT_KEYS = %w[
|
8
10
|
family_replacement
|
9
11
|
v1_replacement
|
@@ -22,11 +24,13 @@ module UserAgentParser
|
|
22
24
|
|
23
25
|
private_constant :FAMILY_REPLACEMENT_KEYS, :OS_REPLACEMENT_KEYS
|
24
26
|
|
25
|
-
attr_reader :
|
27
|
+
attr_reader :patterns_paths
|
28
|
+
|
29
|
+
def initialize(patterns_path: nil, patterns_paths: [])
|
30
|
+
@patterns_paths = [patterns_path, *patterns_paths].compact
|
31
|
+
@patterns_paths = [UserAgentParser::DefaultPatternsPath] if @patterns_paths.empty?
|
26
32
|
|
27
|
-
|
28
|
-
@patterns_path = options[:patterns_path] || UserAgentParser::DefaultPatternsPath
|
29
|
-
@ua_patterns, @os_patterns, @device_patterns = load_patterns(patterns_path)
|
33
|
+
@ua_patterns, @os_patterns, @device_patterns = load_patterns(@patterns_paths)
|
30
34
|
end
|
31
35
|
|
32
36
|
def parse(user_agent)
|
@@ -35,19 +39,24 @@ module UserAgentParser
|
|
35
39
|
parse_ua(user_agent, os, device)
|
36
40
|
end
|
37
41
|
|
38
|
-
|
39
|
-
|
40
|
-
def load_patterns(path)
|
41
|
-
yml = YAML.load_file(path)
|
42
|
+
def parse_os(user_agent)
|
43
|
+
pattern, match = first_pattern_match(@os_patterns, user_agent)
|
42
44
|
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
end
|
45
|
+
if match
|
46
|
+
os_from_pattern_match(pattern, match)
|
47
|
+
else
|
48
|
+
OperatingSystem.new
|
48
49
|
end
|
50
|
+
end
|
51
|
+
|
52
|
+
def parse_device(user_agent)
|
53
|
+
pattern, match = first_pattern_match(@device_patterns, user_agent)
|
49
54
|
|
50
|
-
|
55
|
+
if match
|
56
|
+
device_from_pattern_match(pattern, match)
|
57
|
+
else
|
58
|
+
Device.new
|
59
|
+
end
|
51
60
|
end
|
52
61
|
|
53
62
|
def parse_ua(user_agent, os = nil, device = nil)
|
@@ -60,44 +69,48 @@ module UserAgentParser
|
|
60
69
|
end
|
61
70
|
end
|
62
71
|
|
63
|
-
def
|
64
|
-
|
72
|
+
def patterns_path
|
73
|
+
patterns_paths.first
|
74
|
+
end
|
75
|
+
deprecate :patterns_path, :patterns_paths, 2022, 12
|
65
76
|
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
77
|
+
private
|
78
|
+
|
79
|
+
def load_patterns(patterns_paths)
|
80
|
+
patterns_paths.each_with_object([[], [], []]) do |path, patterns|
|
81
|
+
ua_patterns, os_patterns, device_patterns = load_patterns_file(path)
|
82
|
+
patterns[0] += ua_patterns
|
83
|
+
patterns[1] += os_patterns
|
84
|
+
patterns[2] += device_patterns
|
70
85
|
end
|
71
86
|
end
|
72
87
|
|
73
|
-
def
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
else
|
79
|
-
Device.new
|
88
|
+
def load_patterns_file(path)
|
89
|
+
yml = begin
|
90
|
+
YAML.load_file(path, freeze: true)
|
91
|
+
rescue ArgumentError
|
92
|
+
YAML.load_file(path)
|
80
93
|
end
|
94
|
+
[
|
95
|
+
parse_pattern(yml['user_agent_parsers']),
|
96
|
+
parse_pattern(yml['os_parsers']),
|
97
|
+
parse_pattern(yml['device_parsers']),
|
98
|
+
]
|
81
99
|
end
|
82
100
|
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
end
|
89
|
-
end
|
90
|
-
nil
|
101
|
+
def parse_pattern(patterns)
|
102
|
+
patterns.map do |pattern|
|
103
|
+
pattern = pattern.dup
|
104
|
+
pattern[:regex] = Regexp.new(pattern.delete('regex'), pattern.delete('regex_flag') == 'i')
|
105
|
+
pattern
|
91
106
|
end
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
end
|
98
|
-
end
|
99
|
-
nil
|
107
|
+
end
|
108
|
+
|
109
|
+
def first_pattern_match(patterns, value)
|
110
|
+
patterns.each do |pattern|
|
111
|
+
return [pattern, pattern[:regex].match(value)] if pattern[:regex].match?(value)
|
100
112
|
end
|
113
|
+
nil
|
101
114
|
end
|
102
115
|
|
103
116
|
def user_agent_from_pattern_match(pattern, match, os = nil, device = nil)
|
@@ -1,10 +1,14 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
+
require 'rubygems/version'
|
4
|
+
|
3
5
|
module UserAgentParser
|
4
6
|
class Version
|
7
|
+
include Comparable
|
8
|
+
|
5
9
|
# Private: Regex used to split string version string into major, minor,
|
6
10
|
# patch, and patch_minor.
|
7
|
-
SEGMENTS_REGEX = /\d+\-\d+|\d+[a-zA-Z]+$|\d+|[A-Za-z][0-9A-Za-z-]
|
11
|
+
SEGMENTS_REGEX = /\d+\-\d+|\d+[a-zA-Z]+$|\d+|[A-Za-z][0-9A-Za-z-]*$/.freeze
|
8
12
|
|
9
13
|
attr_reader :version
|
10
14
|
alias to_s version
|
@@ -45,9 +49,10 @@ module UserAgentParser
|
|
45
49
|
version == other.version
|
46
50
|
end
|
47
51
|
|
48
|
-
|
52
|
+
def <=>(other)
|
53
|
+
Gem::Version.new(version).<=>(Gem::Version.new(other.to_s))
|
54
|
+
end
|
49
55
|
|
50
|
-
# Private
|
51
56
|
def segments
|
52
57
|
@segments ||= version.scan(SEGMENTS_REGEX)
|
53
58
|
end
|
data/lib/user_agent_parser.rb
CHANGED
@@ -10,7 +10,7 @@ module UserAgentParser
|
|
10
10
|
DefaultPatternsPath = File.join(File.dirname(__FILE__), '../vendor/uap-core/regexes.yaml')
|
11
11
|
|
12
12
|
# Parse the given +user_agent_string+, returning a +UserAgent+
|
13
|
-
def self.parse(user_agent_string,
|
14
|
-
Parser.new(
|
13
|
+
def self.parse(user_agent_string, **args)
|
14
|
+
Parser.new(**args).parse(user_agent_string)
|
15
15
|
end
|
16
16
|
end
|