device_detector 1.0.1 → 1.0.5
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 +5 -5
- data/.rubocop.yml +49 -0
- data/.ruby-version +1 -0
- data/.travis.yml +5 -10
- data/CHANGELOG.md +14 -1
- data/README.md +6 -6
- data/Rakefile +20 -13
- data/device_detector.gemspec +1 -0
- data/lib/device_detector.rb +30 -26
- data/lib/device_detector/bot.rb +2 -2
- data/lib/device_detector/client.rb +3 -2
- data/lib/device_detector/device.rb +46 -20
- data/lib/device_detector/memory_cache.rb +26 -19
- data/lib/device_detector/metadata_extractor.rb +7 -8
- data/lib/device_detector/model_extractor.rb +3 -3
- data/lib/device_detector/name_extractor.rb +2 -2
- data/lib/device_detector/os.rb +121 -111
- data/lib/device_detector/parser.rb +22 -9
- data/lib/device_detector/version.rb +3 -1
- data/lib/device_detector/version_extractor.rb +2 -3
- data/regexes/bots.yml +442 -19
- data/regexes/client/browser_engine.yml +7 -1
- data/regexes/client/browsers.yml +773 -103
- data/regexes/client/feed_readers.yml +14 -8
- data/regexes/client/libraries.yml +43 -2
- data/regexes/client/mediaplayers.yml +21 -5
- data/regexes/client/mobile_apps.yml +131 -1
- data/regexes/client/pim.yml +6 -1
- data/regexes/device/cameras.yml +1 -1
- data/regexes/device/car_browsers.yml +7 -3
- data/regexes/device/consoles.yml +3 -3
- data/regexes/device/mobiles.yml +11365 -791
- data/regexes/device/notebooks.yml +114 -0
- data/regexes/device/portable_media_player.yml +2 -2
- data/regexes/device/televisions.yml +17 -3
- data/regexes/oss.yml +115 -47
- data/regexes/vendorfragments.yml +6 -2
- data/spec/device_detector/concrete_user_agent_spec.rb +16 -17
- data/spec/device_detector/detector_fixtures_spec.rb +30 -35
- data/spec/device_detector/device_spec.rb +28 -48
- data/spec/device_detector/memory_cache_spec.rb +60 -28
- data/spec/device_detector/model_extractor_spec.rb +3 -3
- data/spec/device_detector/version_extractor_spec.rb +5 -6
- data/spec/device_detector_spec.rb +49 -78
- data/spec/fixtures/client/browser.yml +1521 -406
- data/spec/fixtures/client/feed_reader.yml +39 -51
- data/spec/fixtures/client/library.yml +72 -11
- data/spec/fixtures/client/mediaplayer.yml +29 -40
- data/spec/fixtures/client/mobile_app.yml +172 -32
- data/spec/fixtures/client/pim.yml +32 -19
- data/spec/fixtures/detector/bots.yml +854 -19
- data/spec/fixtures/detector/camera.yml +22 -2
- data/spec/fixtures/detector/car_browser.yml +60 -0
- data/spec/fixtures/detector/console.yml +43 -3
- data/spec/fixtures/detector/desktop.yml +2860 -1527
- data/spec/fixtures/detector/feature_phone.yml +69 -1
- data/spec/fixtures/detector/feed_reader.yml +158 -130
- data/spec/fixtures/detector/mediaplayer.yml +113 -39
- data/spec/fixtures/detector/mobile_apps.yml +262 -89
- data/spec/fixtures/detector/phablet.yml +3444 -663
- data/spec/fixtures/detector/portable_media_player.yml +57 -0
- data/spec/fixtures/detector/smart_speaker.yml +55 -0
- data/spec/fixtures/detector/smartphone-1.yml +4739 -4765
- data/spec/fixtures/detector/smartphone-10.yml +9973 -0
- data/spec/fixtures/detector/smartphone-11.yml +10015 -0
- data/spec/fixtures/detector/smartphone-12.yml +9897 -0
- data/spec/fixtures/detector/smartphone-13.yml +9912 -0
- data/spec/fixtures/detector/smartphone-14.yml +9935 -0
- data/spec/fixtures/detector/smartphone-15.yml +6595 -0
- data/spec/fixtures/detector/smartphone-16.yml +10021 -0
- data/spec/fixtures/detector/smartphone-17.yml +9408 -0
- data/spec/fixtures/detector/smartphone-2.yml +4265 -4238
- data/spec/fixtures/detector/smartphone-3.yml +4487 -4391
- data/spec/fixtures/detector/smartphone-4.yml +4210 -4179
- data/spec/fixtures/detector/smartphone-5.yml +5794 -2901
- data/spec/fixtures/detector/smartphone-6.yml +10114 -0
- data/spec/fixtures/detector/smartphone-7.yml +9975 -0
- data/spec/fixtures/detector/smartphone-8.yml +9897 -0
- data/spec/fixtures/detector/smartphone-9.yml +9880 -0
- data/spec/fixtures/detector/smartphone.yml +4152 -4048
- data/spec/fixtures/detector/tablet-1.yml +3997 -3991
- data/spec/fixtures/detector/tablet-2.yml +6820 -1935
- data/spec/fixtures/detector/tablet-3.yml +9968 -0
- data/spec/fixtures/detector/tablet-4.yml +7113 -0
- data/spec/fixtures/detector/tablet.yml +3789 -3804
- data/spec/fixtures/detector/tv.yml +3889 -1495
- data/spec/fixtures/detector/unknown.yml +45 -179
- data/spec/fixtures/detector/wearable.yml +61 -0
- data/spec/fixtures/device/camera.yml +4 -3
- data/spec/fixtures/device/car_browser.yml +9 -2
- data/spec/fixtures/device/console.yml +15 -14
- data/spec/fixtures/device/notebook.yml +7 -0
- data/spec/fixtures/parser/oss.yml +177 -0
- data/spec/fixtures/parser/vendorfragments.yml +6 -0
- metadata +57 -7
data/regexes/vendorfragments.yml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
###############
|
2
2
|
# Device Detector - The Universal Device Detection library for parsing User Agents
|
3
3
|
#
|
4
|
-
# @link
|
4
|
+
# @link https://matomo.org
|
5
5
|
# @license http://www.gnu.org/licenses/lgpl.html LGPL v3 or later
|
6
6
|
###############
|
7
7
|
|
@@ -20,7 +20,7 @@ Sony:
|
|
20
20
|
|
21
21
|
Asus:
|
22
22
|
- 'MAAU'
|
23
|
-
- 'NP0[
|
23
|
+
- 'NP0[26789]'
|
24
24
|
- 'ASJB'
|
25
25
|
- 'ASU2(JS)?'
|
26
26
|
|
@@ -69,3 +69,7 @@ HP:
|
|
69
69
|
|
70
70
|
Hyrican:
|
71
71
|
- 'MANM(JS)?'
|
72
|
+
|
73
|
+
Ordissimo:
|
74
|
+
- 'Ordissimo'
|
75
|
+
- 'webissimo3'
|
@@ -13,7 +13,7 @@ describe DeviceDetector do
|
|
13
13
|
describe '#device_name' do
|
14
14
|
|
15
15
|
it 'returns device name' do
|
16
|
-
client.device_name.must_equal 'iPhone 5S'
|
16
|
+
value(client.device_name).must_equal 'iPhone 5S'
|
17
17
|
end
|
18
18
|
|
19
19
|
end
|
@@ -21,7 +21,7 @@ describe DeviceDetector do
|
|
21
21
|
describe '#device_type' do
|
22
22
|
|
23
23
|
it 'returns the device type' do
|
24
|
-
client.device_type.must_equal 'smartphone'
|
24
|
+
value(client.device_type).must_equal 'smartphone'
|
25
25
|
end
|
26
26
|
|
27
27
|
end
|
@@ -35,7 +35,7 @@ describe DeviceDetector do
|
|
35
35
|
describe '#os_name' do
|
36
36
|
|
37
37
|
it 'returns the OS name' do
|
38
|
-
client.os_name.must_equal 'Ubuntu'
|
38
|
+
value(client.os_name).must_equal 'Ubuntu'
|
39
39
|
end
|
40
40
|
|
41
41
|
end
|
@@ -49,7 +49,7 @@ describe DeviceDetector do
|
|
49
49
|
describe '#full_version' do
|
50
50
|
|
51
51
|
it 'returns the correct OS version' do
|
52
|
-
client.os_full_version.must_equal '10.10.1'
|
52
|
+
value(client.os_full_version).must_equal '10.10.1'
|
53
53
|
end
|
54
54
|
|
55
55
|
end
|
@@ -63,15 +63,15 @@ describe DeviceDetector do
|
|
63
63
|
let(:user_agent) { 'Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/37.0.2062.103 Safari/537.36' }
|
64
64
|
|
65
65
|
it 'returns the correct client name' do
|
66
|
-
client.name.must_equal 'Chrome'
|
66
|
+
value(client.name).must_equal 'Chrome'
|
67
67
|
end
|
68
68
|
|
69
69
|
it 'recognizes the device name' do
|
70
|
-
client.device_name.must_be_nil
|
70
|
+
value(client.device_name).must_be_nil
|
71
71
|
end
|
72
72
|
|
73
73
|
it 'recognizes the device type' do
|
74
|
-
client.device_type.must_equal "desktop"
|
74
|
+
value(client.device_type).must_equal "desktop"
|
75
75
|
end
|
76
76
|
|
77
77
|
end
|
@@ -81,15 +81,15 @@ describe DeviceDetector do
|
|
81
81
|
let(:user_agent) { 'Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2228.0 Safari/537.36' }
|
82
82
|
|
83
83
|
it 'returns the correct client name' do
|
84
|
-
client.name.must_equal 'Chrome'
|
84
|
+
value(client.name).must_equal 'Chrome'
|
85
85
|
end
|
86
86
|
|
87
87
|
it 'recognizes the device name' do
|
88
|
-
client.device_name.must_be_nil
|
88
|
+
value(client.device_name).must_be_nil
|
89
89
|
end
|
90
90
|
|
91
91
|
it 'recognizes the device type' do
|
92
|
-
client.device_type.must_equal "desktop"
|
92
|
+
value(client.device_type).must_equal "desktop"
|
93
93
|
end
|
94
94
|
|
95
95
|
end
|
@@ -101,15 +101,15 @@ describe DeviceDetector do
|
|
101
101
|
let(:user_agent) { 'sprd-Galaxy-S5/1.0 Linux/2.6.35.7 Android/4.4.4 Release/11.29.2014 Browser/AppleWebKit533.1 (KHTML, like Gecko) Mozilla/5.0 Mobile' }
|
102
102
|
|
103
103
|
it 'returns the correct client name' do
|
104
|
-
client.name.must_equal "Android Browser"
|
104
|
+
value(client.name).must_equal "Android Browser"
|
105
105
|
end
|
106
106
|
|
107
107
|
it 'recognizes the device name' do
|
108
|
-
client.device_name.must_equal "GALAXY S5"
|
108
|
+
value(client.device_name).must_equal "GALAXY S5"
|
109
109
|
end
|
110
110
|
|
111
111
|
it 'recognizes the device type' do
|
112
|
-
client.device_type.must_equal "smartphone"
|
112
|
+
value(client.device_type).must_equal "smartphone"
|
113
113
|
end
|
114
114
|
|
115
115
|
end
|
@@ -119,18 +119,17 @@ describe DeviceDetector do
|
|
119
119
|
let(:user_agent) { 'Lenovo-A398t+_TD/S100 Linux/3.4.5 Android/4.1.2 Release/09.10.2013 Browser/AppleWebKit534.30 Mobile Safari/534.30' }
|
120
120
|
|
121
121
|
it 'returns the correct client name' do
|
122
|
-
client.name.must_equal "Android Browser"
|
122
|
+
value(client.name).must_equal "Android Browser"
|
123
123
|
end
|
124
124
|
|
125
125
|
it 'recognizes the device name' do
|
126
|
-
client.device_name.must_equal "A398t+"
|
126
|
+
value(client.device_name).must_equal "A398t+"
|
127
127
|
end
|
128
128
|
|
129
129
|
it 'recognizes the device type' do
|
130
|
-
client.device_type.must_equal "smartphone"
|
130
|
+
value(client.device_type).must_equal "smartphone"
|
131
131
|
end
|
132
132
|
|
133
133
|
end
|
134
134
|
|
135
135
|
end
|
136
|
-
|
@@ -1,71 +1,69 @@
|
|
1
1
|
require_relative '../spec_helper'
|
2
2
|
|
3
3
|
describe DeviceDetector do
|
4
|
-
|
5
|
-
fixture_dir = File.expand_path('../../fixtures/detector', __FILE__)
|
4
|
+
fixture_dir = File.expand_path('../fixtures/detector', __dir__)
|
6
5
|
fixture_files = Dir["#{fixture_dir}/*.yml"]
|
7
6
|
fixture_files.each do |fixture_file|
|
8
|
-
|
9
7
|
describe File.basename(fixture_file) do
|
10
|
-
|
11
8
|
fixtures = nil
|
12
9
|
begin
|
13
10
|
fixtures = YAML.load(File.read(fixture_file))
|
14
11
|
rescue Psych::SyntaxError => e
|
15
|
-
|
12
|
+
raise "Failed to parse #{fixture_file}, reason: #{e}"
|
16
13
|
end
|
17
14
|
|
18
15
|
def str_or_nil(string)
|
16
|
+
return nil if string.nil?
|
19
17
|
return nil if string == ''
|
20
|
-
|
18
|
+
|
19
|
+
string.to_s
|
21
20
|
end
|
22
21
|
|
23
22
|
fixtures.each do |f|
|
24
|
-
|
25
|
-
user_agent = f["user_agent"]
|
23
|
+
user_agent = f['user_agent']
|
26
24
|
detector = DeviceDetector.new(user_agent)
|
27
25
|
os = detector.send(:os)
|
28
26
|
|
29
27
|
describe user_agent do
|
30
|
-
it
|
28
|
+
it 'should be detected' do
|
31
29
|
if detector.bot?
|
32
|
-
assert_equal str_or_nil(f[
|
30
|
+
assert_equal str_or_nil(f['bot']['name']), detector.bot_name, 'failed bot name detection'
|
33
31
|
else
|
34
|
-
if f[
|
35
|
-
assert_equal str_or_nil(f[
|
32
|
+
if f['client']
|
33
|
+
assert_equal str_or_nil(f['client']['name']), detector.name, 'failed client name detection'
|
36
34
|
end
|
37
35
|
|
38
|
-
os_family = str_or_nil(f[
|
39
|
-
if os_family !=
|
36
|
+
os_family = str_or_nil(f['os_family'])
|
37
|
+
if os_family != 'Unknown'
|
40
38
|
if os_family.nil?
|
41
|
-
assert_nil os.family,
|
39
|
+
assert_nil os.family, 'failed os family detection'
|
42
40
|
else
|
43
|
-
assert_equal os_family, os.family,
|
41
|
+
assert_equal os_family, os.family, 'failed os family detection'
|
44
42
|
end
|
45
43
|
|
46
|
-
name = str_or_nil(f[
|
44
|
+
name = str_or_nil(f['os']['name'])
|
47
45
|
if name.nil?
|
48
|
-
assert_nil os.name,
|
46
|
+
assert_nil os.name, 'failed os name detection'
|
49
47
|
else
|
50
|
-
assert_equal name, os.name,
|
48
|
+
assert_equal name, os.name, 'failed os name detection'
|
51
49
|
end
|
52
50
|
|
53
|
-
short_name = str_or_nil(f[
|
51
|
+
short_name = str_or_nil(f['os']['short_name'])
|
54
52
|
if short_name.nil?
|
55
|
-
assert_nil os.short_name,
|
53
|
+
assert_nil os.short_name, 'failed os short name detection'
|
56
54
|
else
|
57
|
-
assert_equal short_name, os.short_name,
|
55
|
+
assert_equal short_name, os.short_name, 'failed os short name detection'
|
58
56
|
end
|
59
57
|
|
60
|
-
os_version = str_or_nil(f[
|
58
|
+
os_version = str_or_nil(f['os']['version'])
|
61
59
|
if os_version.nil?
|
62
|
-
assert_nil os.full_version,
|
60
|
+
assert_nil os.full_version, 'failed os version detection'
|
63
61
|
else
|
64
|
-
assert_equal os_version, os.full_version,
|
62
|
+
assert_equal os_version, os.full_version, 'failed os version detection'
|
65
63
|
end
|
66
64
|
end
|
67
|
-
if f[
|
68
|
-
expected_type = str_or_nil(f[
|
65
|
+
if f['device']
|
66
|
+
expected_type = str_or_nil(f['device']['type'])
|
69
67
|
actual_type = detector.device_type
|
70
68
|
|
71
69
|
if expected_type != actual_type
|
@@ -74,27 +72,24 @@ describe DeviceDetector do
|
|
74
72
|
# detector.device_type
|
75
73
|
end
|
76
74
|
if expected_type.nil?
|
77
|
-
assert_nil actual_type,
|
75
|
+
assert_nil actual_type, 'failed device type detection'
|
78
76
|
else
|
79
|
-
assert_equal expected_type, actual_type,
|
77
|
+
assert_equal expected_type, actual_type, 'failed device type detection'
|
80
78
|
end
|
81
79
|
|
82
|
-
model = str_or_nil(f[
|
80
|
+
model = str_or_nil(f['device']['model'])
|
83
81
|
model = model.to_s unless model.nil?
|
84
82
|
|
85
83
|
if model.nil?
|
86
|
-
assert_nil detector.device_name,
|
84
|
+
assert_nil detector.device_name, 'failed device name detection'
|
87
85
|
else
|
88
|
-
assert_equal model, detector.device_name,
|
86
|
+
assert_equal model, detector.device_name, 'failed device name detection'
|
89
87
|
end
|
90
88
|
end
|
91
89
|
end
|
92
90
|
end
|
93
91
|
end
|
94
92
|
end
|
95
|
-
|
96
93
|
end
|
97
|
-
|
98
94
|
end
|
99
|
-
|
100
95
|
end
|
@@ -1,18 +1,18 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
require_relative '../spec_helper'
|
2
4
|
|
3
5
|
describe DeviceDetector::Device do
|
4
|
-
|
5
6
|
subject { DeviceDetector::Device.new(user_agent) }
|
6
7
|
|
7
|
-
|
8
|
+
alias_method :device, :subject
|
8
9
|
|
9
10
|
describe '#name' do
|
10
|
-
|
11
11
|
describe 'when models are nested' do
|
12
12
|
let(:user_agent) { 'Mozilla/5.0 (iPhone; CPU iPhone OS 8_1_3 like Mac OS X) AppleWebKit/600.1.4 (KHTML, like Gecko) Mobile/12B466 [FBDV/iPhone7,2]' }
|
13
13
|
|
14
14
|
it 'finds an Apple iPhone 6' do
|
15
|
-
device.name.must_equal 'iPhone 6'
|
15
|
+
value(device.name).must_equal 'iPhone 6'
|
16
16
|
end
|
17
17
|
end
|
18
18
|
|
@@ -20,7 +20,7 @@ describe DeviceDetector::Device do
|
|
20
20
|
let(:user_agent) { 'AIRNESS-AIR99/REV 2.2.1/Teleca Q03B1' }
|
21
21
|
|
22
22
|
it 'finds an Airness AIR99' do
|
23
|
-
device.name.must_equal 'AIR99'
|
23
|
+
value(device.name).must_equal 'AIR99'
|
24
24
|
end
|
25
25
|
end
|
26
26
|
|
@@ -28,19 +28,17 @@ describe DeviceDetector::Device do
|
|
28
28
|
let(:user_agent) { 'UNKNOWN MODEL NAME' }
|
29
29
|
|
30
30
|
it 'returns nil' do
|
31
|
-
device.name.must_be_nil
|
31
|
+
value(device.name).must_be_nil
|
32
32
|
end
|
33
33
|
end
|
34
|
-
|
35
34
|
end
|
36
35
|
|
37
36
|
describe '#type' do
|
38
|
-
|
39
37
|
describe 'when models are nested' do
|
40
38
|
let(:user_agent) { 'Mozilla/5.0 (iPhone; CPU iPhone OS 8_1_3 like Mac OS X) AppleWebKit/600.1.4 (KHTML, like Gecko) Mobile/12B466 [FBDV/iPhone7,2]' }
|
41
39
|
|
42
40
|
it 'finds device of Apple iPhone 6' do
|
43
|
-
device.type.must_equal 'smartphone'
|
41
|
+
value(device.type).must_equal 'smartphone'
|
44
42
|
end
|
45
43
|
end
|
46
44
|
|
@@ -48,7 +46,7 @@ describe DeviceDetector::Device do
|
|
48
46
|
let(:user_agent) { 'AIRNESS-AIR99/REV 2.2.1/Teleca Q03B1' }
|
49
47
|
|
50
48
|
it 'finds the device of Airness AIR99' do
|
51
|
-
device.type.must_equal 'feature phone'
|
49
|
+
value(device.type).must_equal 'feature phone'
|
52
50
|
end
|
53
51
|
end
|
54
52
|
|
@@ -56,96 +54,78 @@ describe DeviceDetector::Device do
|
|
56
54
|
let(:user_agent) { 'UNKNOWN MODEL TYPE' }
|
57
55
|
|
58
56
|
it 'returns nil' do
|
59
|
-
device.type.must_be_nil
|
57
|
+
value(device.type).must_be_nil
|
60
58
|
end
|
61
|
-
|
62
59
|
end
|
63
60
|
|
64
61
|
describe 'device not specified in nested block' do
|
65
|
-
|
66
62
|
let(:user_agent) { 'Mozilla/5.0 (Linux; Android 4.4.2; es-us; SAMSUNG SM-G900F Build/KOT49H) AppleWebKit/537.36 (KHTML, like Gecko)' }
|
67
63
|
|
68
64
|
it 'falls back to top-level device' do
|
69
|
-
device.type.must_equal 'smartphone'
|
65
|
+
value(device.type).must_equal 'smartphone'
|
70
66
|
end
|
71
|
-
|
72
67
|
end
|
73
|
-
|
74
68
|
end
|
75
69
|
|
76
70
|
describe 'concrete device types' do
|
77
|
-
|
78
71
|
describe 'mobiles' do
|
79
|
-
|
80
72
|
let(:user_agent) { 'Mozilla/5.0 (Linux; Android 4.4.2; es-us; SAMSUNG SM-G900F Build/KOT49H) AppleWebKit/537.36 (KHTML, like Gecko)' }
|
81
73
|
|
82
74
|
it 'identifies the device' do
|
83
|
-
device.name.must_equal 'GALAXY S5'
|
84
|
-
device.type.must_equal 'smartphone'
|
85
|
-
device.brand.must_equal 'Samsung'
|
75
|
+
value(device.name).must_equal 'GALAXY S5'
|
76
|
+
value(device.type).must_equal 'smartphone'
|
77
|
+
value(device.brand).must_equal 'Samsung'
|
86
78
|
end
|
87
|
-
|
88
79
|
end
|
89
80
|
|
90
81
|
describe 'cameras' do
|
91
|
-
|
92
82
|
let(:user_agent) { 'Mozilla/5.0 (Linux; U; Android 4.0; xx-xx; EK-GC100 Build/IMM76D) AppleWebKit/534.30 (KHTML, like Gecko) Version/4.0 Mobile Safari/534.30' }
|
93
83
|
|
94
84
|
it 'identifies the device' do
|
95
|
-
device.name.must_equal 'GALAXY Camera'
|
96
|
-
device.type.must_equal 'camera'
|
97
|
-
device.brand.must_equal 'Samsung'
|
85
|
+
value(device.name).must_equal 'GALAXY Camera'
|
86
|
+
value(device.type).must_equal 'camera'
|
87
|
+
value(device.brand).must_equal 'Samsung'
|
98
88
|
end
|
99
|
-
|
100
89
|
end
|
101
90
|
|
102
91
|
describe 'car browsers' do
|
103
|
-
|
104
92
|
let(:user_agent) { 'Mozilla/5.0 (X11; Linux) AppleWebKit/534.34 (KHTML, like Gecko) QtCarBrowser Safari/534.34' }
|
105
93
|
|
106
94
|
it 'identifies the device' do
|
107
|
-
device.name.must_equal 'Model S'
|
108
|
-
device.type.must_equal 'car browser'
|
109
|
-
device.brand.
|
95
|
+
value(device.name).must_equal 'Model S'
|
96
|
+
value(device.type).must_equal 'car browser'
|
97
|
+
value(device.brand).must_equal 'Tesla'
|
110
98
|
end
|
111
|
-
|
112
99
|
end
|
113
100
|
|
114
101
|
describe '(gaming) consoles' do
|
115
|
-
|
116
102
|
let(:user_agent) { 'Opera/9.30 (Nintendo Wii; U; ; 2047-7;en)' }
|
117
103
|
|
118
104
|
it 'identifies the device' do
|
119
|
-
device.name.must_equal 'Wii'
|
120
|
-
device.type.must_equal 'console'
|
121
|
-
device.brand.must_be_nil
|
105
|
+
value(device.name).must_equal 'Wii'
|
106
|
+
value(device.type).must_equal 'console'
|
107
|
+
value(device.brand).must_be_nil
|
122
108
|
end
|
123
|
-
|
124
109
|
end
|
125
110
|
|
126
111
|
describe 'portable media players' do
|
127
|
-
|
128
112
|
let(:user_agent) { 'Mozilla/5.0 (iPod touch; CPU iPhone OS 7_0_6 like Mac OS X) AppleWebKit/537.51.1 (KHTML, like Gecko) Version/7.0 Mobile/11B651 Safari/9537.53' }
|
129
113
|
|
130
114
|
it 'identifies the device' do
|
131
|
-
device.name.must_equal 'iPod Touch'
|
132
|
-
device.type.must_equal 'portable media player'
|
133
|
-
device.brand.must_equal 'Apple'
|
115
|
+
value(device.name).must_equal 'iPod Touch'
|
116
|
+
value(device.type).must_equal 'portable media player'
|
117
|
+
value(device.brand).must_equal 'Apple'
|
134
118
|
end
|
135
|
-
|
136
119
|
end
|
137
120
|
|
138
121
|
describe 'televisions' do
|
139
|
-
|
140
122
|
let(:user_agent) { 'Mozilla/5.0 (Unknown; Linux armv7l) AppleWebKit/537.1+ (KHTML, like Gecko) Safari/537.1+ HbbTV/1.1.1 ( ;LGE ;NetCast 4.0 ;03.10.81 ;1.0M ;)' }
|
141
123
|
|
142
124
|
it 'identifies the device' do
|
143
|
-
device.name.must_equal 'NetCast 4.0'
|
144
|
-
device.type.must_equal 'tv'
|
145
|
-
device.brand.must_equal 'LG'
|
125
|
+
value(device.name).must_equal 'NetCast 4.0'
|
126
|
+
value(device.type).must_equal 'tv'
|
127
|
+
value(device.brand).must_equal 'LG'
|
146
128
|
end
|
147
|
-
|
148
129
|
end
|
149
130
|
end
|
150
|
-
|
151
131
|
end
|
@@ -1,73 +1,103 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
require_relative '../spec_helper'
|
2
4
|
|
3
5
|
describe DeviceDetector::MemoryCache do
|
4
|
-
|
5
6
|
let(:subject) { DeviceDetector::MemoryCache.new(config) }
|
6
7
|
|
7
8
|
let(:config) { {} }
|
8
9
|
|
9
10
|
describe '#set' do
|
10
|
-
|
11
11
|
describe 'string key' do
|
12
|
-
|
13
12
|
let(:key) { 'string' }
|
14
13
|
|
15
14
|
it 'sets the value under the key' do
|
16
15
|
subject.set(key, 'value')
|
17
16
|
|
18
|
-
subject.data[key].must_equal 'value'
|
17
|
+
value(subject.data[key]).must_equal 'value'
|
19
18
|
end
|
20
19
|
|
20
|
+
it 'returns the value' do
|
21
|
+
value(subject.set(key, 'value')).must_equal 'value'
|
22
|
+
value(subject.set(key, false)).must_equal false
|
23
|
+
assert_nil subject.set(key, nil)
|
24
|
+
end
|
21
25
|
end
|
22
26
|
|
23
27
|
describe 'array key' do
|
24
|
-
|
25
|
-
let(:key) { ['string1', 'string2'] }
|
28
|
+
let(:key) { %w[string1 string2] }
|
26
29
|
|
27
30
|
it 'sets the value under the key' do
|
28
31
|
subject.set(key, 'value')
|
29
32
|
|
30
|
-
subject.data[String(key)].must_equal 'value'
|
33
|
+
value(subject.data[String(key)]).must_equal 'value'
|
31
34
|
end
|
35
|
+
end
|
36
|
+
|
37
|
+
describe 'nil value' do
|
38
|
+
let(:key) { 'string' }
|
39
|
+
let(:internal_value) { DeviceDetector::MemoryCache::STORES_NIL_VALUE }
|
40
|
+
|
41
|
+
it 'sets the value under the key' do
|
42
|
+
subject.set(key, nil)
|
32
43
|
|
44
|
+
value(subject.data[String(key)]).must_equal internal_value
|
45
|
+
assert_nil subject.get(key)
|
46
|
+
end
|
47
|
+
|
48
|
+
it 'sets the value under the key' do
|
49
|
+
subject.get_or_set(key, nil)
|
50
|
+
|
51
|
+
value(subject.data[String(key)]).must_equal internal_value
|
52
|
+
assert_nil subject.get(key)
|
53
|
+
end
|
33
54
|
end
|
34
55
|
|
56
|
+
describe 'false value' do
|
57
|
+
let(:key) { 'string' }
|
58
|
+
|
59
|
+
it 'sets the value under the key' do
|
60
|
+
subject.set(key, false)
|
61
|
+
|
62
|
+
value(subject.data[String(key)]).must_equal false
|
63
|
+
value(subject.get(key)).must_equal false
|
64
|
+
end
|
65
|
+
|
66
|
+
it 'sets the value under the key' do
|
67
|
+
subject.get_or_set(key, false)
|
68
|
+
|
69
|
+
value(subject.data[String(key)]).must_equal false
|
70
|
+
value(subject.get(key)).must_equal false
|
71
|
+
end
|
72
|
+
end
|
35
73
|
end
|
36
74
|
|
37
75
|
describe '#get' do
|
38
|
-
|
39
76
|
describe 'string key' do
|
40
|
-
|
41
77
|
let(:key) { 'string' }
|
42
78
|
|
43
79
|
it 'gets the value for the key' do
|
44
80
|
subject.data[key] = 'value'
|
45
81
|
|
46
|
-
subject.get(key).must_equal 'value'
|
82
|
+
value(subject.get(key)).must_equal 'value'
|
47
83
|
end
|
48
|
-
|
49
84
|
end
|
50
85
|
|
51
86
|
describe 'array key' do
|
52
|
-
|
53
|
-
let(:key) { ['string1', 'string2'] }
|
87
|
+
let(:key) { %w[string1 string2] }
|
54
88
|
|
55
89
|
it 'gets the value for the key' do
|
56
90
|
subject.data[String(key)] = 'value'
|
57
91
|
|
58
|
-
subject.get(key).must_equal 'value'
|
92
|
+
value(subject.get(key)).must_equal 'value'
|
59
93
|
end
|
60
|
-
|
61
94
|
end
|
62
|
-
|
63
95
|
end
|
64
96
|
|
65
97
|
describe '#get_or_set' do
|
66
|
-
|
67
98
|
let(:key) { 'string' }
|
68
99
|
|
69
100
|
describe 'value already present' do
|
70
|
-
|
71
101
|
it 'gets the value for the key from cache' do
|
72
102
|
subject.data[key] = 'value'
|
73
103
|
|
@@ -76,30 +106,34 @@ describe DeviceDetector::MemoryCache do
|
|
76
106
|
block_called = true
|
77
107
|
end
|
78
108
|
|
79
|
-
value.must_equal 'value'
|
80
|
-
block_called.must_equal false
|
109
|
+
value(value).must_equal 'value'
|
110
|
+
value(block_called).must_equal false
|
81
111
|
end
|
82
112
|
|
113
|
+
it 'returns the value' do
|
114
|
+
subject.data[key] = 'value2'
|
115
|
+
value(subject.get_or_set(key, 'value')).must_equal 'value2'
|
116
|
+
end
|
83
117
|
end
|
84
118
|
|
85
119
|
describe 'value not yet present' do
|
86
|
-
|
87
120
|
it 'evaluates the block and sets the result' do
|
88
121
|
block_called = false
|
89
122
|
subject.get_or_set(key) do
|
90
123
|
block_called = true
|
91
124
|
end
|
92
125
|
|
93
|
-
block_called.must_equal true
|
94
|
-
subject.data[key].must_equal true
|
126
|
+
value(block_called).must_equal true
|
127
|
+
value(subject.data[key]).must_equal true
|
95
128
|
end
|
96
129
|
|
130
|
+
it 'returns the value' do
|
131
|
+
value(subject.get_or_set(key, 'value')).must_equal 'value'
|
132
|
+
end
|
97
133
|
end
|
98
|
-
|
99
134
|
end
|
100
135
|
|
101
136
|
describe 'cache purging' do
|
102
|
-
|
103
137
|
let(:config) { { max_cache_keys: 3 } }
|
104
138
|
|
105
139
|
it 'purges the cache when key size arrives at max' do
|
@@ -108,9 +142,7 @@ describe DeviceDetector::MemoryCache do
|
|
108
142
|
subject.set('3', 'baz')
|
109
143
|
subject.set('4', 'boz')
|
110
144
|
|
111
|
-
subject.data.keys.size.must_equal 3
|
145
|
+
value(subject.data.keys.size).must_equal 3
|
112
146
|
end
|
113
|
-
|
114
147
|
end
|
115
|
-
|
116
148
|
end
|