al 0.1.0 → 0.1.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/al.rb +57 -78
- data/test/parse_test.rb +11 -0
- data/test/pick_test.rb +5 -0
- data/test/strict_parse_test.rb +11 -0
- data/test/strict_pick_test.rb +6 -1
- metadata +3 -4
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 916f2172f8334bd4022beba1200fbadc5ef02f99
|
4
|
+
data.tar.gz: 9960379dacc8cb84b26c05da2a5b3e46ea6e5b0e
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 99b589489d1089ae744d4ef4d8b455323958a1810a4a738f6b3ab48599c373d9c5ba7b613ae375485ca5fe44fd9f47b12142f2621c714eca29b6d212156a0f5c
|
7
|
+
data.tar.gz: 133b27b1395a6fb3d3de09fa142b3739b6ee2d52fe630045a801038c083fb4c11633ede447c348aa9f38b42a8006bfd27ed6c934491f1f4bf688515bd0d897ce
|
data/lib/al.rb
CHANGED
@@ -1,21 +1,25 @@
|
|
1
1
|
class Al
|
2
|
-
def initialize(tag = nil,
|
3
|
-
|
2
|
+
def initialize(tag = nil, value = nil)
|
3
|
+
unless tag.nil?
|
4
|
+
tag = tag.downcase
|
5
|
+
end
|
6
|
+
|
7
|
+
@languages = { "*" => [tag, value] }
|
4
8
|
|
5
|
-
if tag &&
|
6
|
-
self[tag] =
|
9
|
+
if tag && value
|
10
|
+
self[tag] = value
|
7
11
|
end
|
8
12
|
end
|
9
13
|
|
10
|
-
def []=(tag,
|
14
|
+
def []=(tag, value)
|
11
15
|
tag = preprocess(tag)
|
12
16
|
|
13
|
-
tag.split(
|
14
|
-
key
|
15
|
-
|
17
|
+
tag.split("-").inject do |key, bit|
|
18
|
+
@languages[key] ||= [tag, value]
|
19
|
+
"#{key}-#{bit}"
|
16
20
|
end
|
17
21
|
|
18
|
-
@languages[tag] = [tag,
|
22
|
+
@languages[tag] = [tag, value] # ensure xx overwrites a previous xx-yy
|
19
23
|
end
|
20
24
|
|
21
25
|
def [](tag)
|
@@ -23,97 +27,72 @@ class Al
|
|
23
27
|
end
|
24
28
|
|
25
29
|
def pick(header)
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
high = 0.0
|
31
|
-
best = nil
|
32
|
-
|
33
|
-
preprocess(header).split(COMMA).each do |tag|
|
34
|
-
len = tag.size
|
35
|
-
sem = tag.index(SEMICOLON) || len
|
36
|
-
pos = sem
|
30
|
+
find(header) do |tag|
|
31
|
+
length = tag.length
|
32
|
+
dash_index = length
|
37
33
|
|
38
|
-
while
|
39
|
-
|
40
|
-
result = @languages[attempt]
|
34
|
+
while dash_index
|
35
|
+
result = @languages[tag[0, dash_index]]
|
41
36
|
|
42
37
|
if result
|
43
|
-
|
44
|
-
|
45
|
-
if quality > high
|
46
|
-
high = quality
|
47
|
-
best = result
|
48
|
-
|
49
|
-
if high >= 1.0
|
50
|
-
return best
|
51
|
-
end
|
52
|
-
|
53
|
-
break
|
54
|
-
end
|
38
|
+
break result
|
55
39
|
end
|
56
40
|
|
57
|
-
|
41
|
+
dash_index = tag.rindex("-".freeze, dash_index - length - 1)
|
58
42
|
end
|
59
43
|
end
|
60
|
-
|
61
|
-
return best || @languages[DEFAULT]
|
62
44
|
end
|
63
45
|
|
64
46
|
def strict_pick(header)
|
65
|
-
|
66
|
-
|
67
|
-
|
47
|
+
find(header) { |tag| @languages[tag] }
|
48
|
+
end
|
49
|
+
|
50
|
+
private
|
51
|
+
|
52
|
+
def find(header, &block)
|
53
|
+
best_quality = 0.0
|
54
|
+
best_result = nil
|
55
|
+
|
56
|
+
preprocess(header.to_s).split(",".freeze).each do |language_range|
|
57
|
+
tag, q = language_range.split(";".freeze, 2)
|
68
58
|
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
end
|
59
|
+
unless tag
|
60
|
+
next
|
61
|
+
end
|
62
|
+
|
63
|
+
result = yield(tag)
|
64
|
+
|
65
|
+
unless result
|
66
|
+
next
|
67
|
+
end
|
68
|
+
|
69
|
+
quality = quality(q)
|
70
|
+
|
71
|
+
if quality > best_quality
|
72
|
+
best_quality = quality
|
73
|
+
best_result = result
|
74
|
+
|
75
|
+
if quality >= 1.0
|
76
|
+
break result
|
88
77
|
end
|
89
78
|
end
|
90
79
|
end
|
91
80
|
|
92
|
-
return
|
81
|
+
return best_result || @languages["*".freeze]
|
93
82
|
end
|
94
83
|
|
95
|
-
|
96
|
-
|
97
|
-
SPACE = " ".freeze
|
98
|
-
EMPTY = "".freeze
|
99
|
-
|
100
|
-
def preprocess(s)
|
101
|
-
result = s.tr(SPACE, EMPTY)
|
84
|
+
def preprocess(header)
|
85
|
+
result = header.delete(" ".freeze)
|
102
86
|
result.downcase!
|
103
87
|
|
104
88
|
return result
|
105
89
|
end
|
106
90
|
|
107
|
-
def quality(
|
108
|
-
q
|
91
|
+
def quality(q)
|
92
|
+
if q.nil? || q.size < 2
|
93
|
+
return 1.0
|
94
|
+
end
|
109
95
|
|
110
|
-
return q
|
111
|
-
rescue ArgumentError
|
112
|
-
0.0
|
96
|
+
return q[2, 10].to_f
|
113
97
|
end
|
114
|
-
|
115
|
-
DEFAULT = "*".freeze
|
116
|
-
COMMA = ",".freeze
|
117
|
-
SEMICOLON = ";".freeze
|
118
|
-
DASH = "-".freeze
|
119
98
|
end
|
data/test/parse_test.rb
CHANGED
@@ -1,5 +1,6 @@
|
|
1
1
|
require "al"
|
2
2
|
require "cutest"
|
3
|
+
require "securerandom"
|
3
4
|
|
4
5
|
test "no input" do
|
5
6
|
al = Al.new("en-gb", "british english")
|
@@ -60,3 +61,13 @@ test "invalid quality values" do
|
|
60
61
|
assert_equal al.pick("en-gb;q=.9, en-us;q=0.8"), ["en-gb", "british english"]
|
61
62
|
assert_equal al.pick("en-gb;q=aaa, en-us;q=0.8"), ["en-us", "american english"]
|
62
63
|
end
|
64
|
+
|
65
|
+
test "gibberish" do
|
66
|
+
al = Al.new("en-gb", "british english")
|
67
|
+
|
68
|
+
assert 1000.times.all? {
|
69
|
+
s = SecureRandom.random_bytes rand(1000)
|
70
|
+
|
71
|
+
al.pick(s) || raise("failed on: #{s}")
|
72
|
+
}
|
73
|
+
end
|
data/test/pick_test.rb
CHANGED
data/test/strict_parse_test.rb
CHANGED
@@ -1,5 +1,6 @@
|
|
1
1
|
require "al"
|
2
2
|
require "cutest"
|
3
|
+
require "securerandom"
|
3
4
|
|
4
5
|
test "no input" do
|
5
6
|
al = Al.new("en-gb", "british english")
|
@@ -60,3 +61,13 @@ test "invalid quality values" do
|
|
60
61
|
assert_equal al.strict_pick("en-gb;q=.9, en-us;q=0.8"), ["en-gb", "british english"]
|
61
62
|
assert_equal al.strict_pick("en-gb;q=aaa, en-us;q=0.8"), ["en-us", "american english"]
|
62
63
|
end
|
64
|
+
|
65
|
+
test "gibberish" do
|
66
|
+
al = Al.new("en-gb", "british english")
|
67
|
+
|
68
|
+
assert 1000.times.all? {
|
69
|
+
s = SecureRandom.random_bytes rand(1000)
|
70
|
+
|
71
|
+
al.strict_pick(s) || raise("failed on: #{s}")
|
72
|
+
}
|
73
|
+
end
|
data/test/strict_pick_test.rb
CHANGED
@@ -46,5 +46,10 @@ test "default value" do
|
|
46
46
|
al = Al.new("en", "english")
|
47
47
|
al["en-GB"] = "british english"
|
48
48
|
|
49
|
-
assert_equal al.
|
49
|
+
assert_equal al.strict_pick("en-us"), ["en", "english"]
|
50
|
+
|
51
|
+
al = Al.new("EN", "english")
|
52
|
+
al["en-GB"] = "british english"
|
53
|
+
|
54
|
+
assert_equal al.strict_pick("en-us"), ["en", "english"]
|
50
55
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: al
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Jip van Reijsen
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2016-03-10 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: cutest
|
@@ -64,7 +64,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
64
64
|
version: '0'
|
65
65
|
requirements: []
|
66
66
|
rubyforge_project:
|
67
|
-
rubygems_version: 2.
|
67
|
+
rubygems_version: 2.5.1
|
68
68
|
signing_key:
|
69
69
|
specification_version: 4
|
70
70
|
summary: Al stands for Accept-Language
|
@@ -73,4 +73,3 @@ test_files:
|
|
73
73
|
- test/pick_test.rb
|
74
74
|
- test/strict_parse_test.rb
|
75
75
|
- test/strict_pick_test.rb
|
76
|
-
has_rdoc:
|