uuid-ncname 0.2.5 → 0.2.6

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 0eaed682c9e65dd5d15a576fcc62b0f234cc70481017aaac64eef84afe3bb6ea
4
- data.tar.gz: f55eadabe095625c795d7cefda9e2cedc9643d383d0e1ecd17b375f3897b99ac
3
+ metadata.gz: f82febb1e1498f1d602bd2225b730b7e9128b7ea7b811472465001ae9925e83d
4
+ data.tar.gz: 8f403882f98f9509c074e3b53c1aebe58ff0b0adbe9f248baf4df9ded59b7b25
5
5
  SHA512:
6
- metadata.gz: 9f060166823eb018f830787177ad331d2e530d5036341802e8ae5b49c6f4a2e661ee8e8a273573bca44ab24904a33b754f7efcbe196e174c998a9a97958514fa
7
- data.tar.gz: e9fbb9c79c496bf639eef5d500e07f9b8f7207739a04c36aea1d6035fb56ddbd5581d8f33b63058a074f4fa35e5d23a9982315f2b14d3533b939f2f9fee89d5c
6
+ metadata.gz: b0c1948dd81504cd7241f2a5ad78e50f661b4d9b0868402fd3e393760a5ddf6d30be82f39b51d700e0ded7681efa65134fb0e37ffa27f2108c238ccb8d2cbdfe
7
+ data.tar.gz: 2c86dfc2b5ee4b1c1be7c1e8343e844bfa9df0a94ebf20b487fd5988b53df4e138ae05e44d5134b00c4e1092140d24042c3e1c5d90ad9238a87fea65ab165f3c
@@ -116,8 +116,9 @@ module UUID::NCName
116
116
  ],
117
117
  ]
118
118
 
119
- def self.encode_version version
120
- ((version & 15) + 65).chr
119
+ def self.encode_version version, radix
120
+ offset = radix == 32 ? 97 : 65
121
+ ((version & 15) + offset).chr
121
122
  end
122
123
 
123
124
  def self.decode_version version
@@ -137,6 +138,27 @@ module UUID::NCName
137
138
 
138
139
  public
139
140
 
141
+ # This error gets thrown when a UUID-NCName token can't be
142
+ # positively determined to be one version or the other.
143
+ class AmbiguousToken < ArgumentError
144
+
145
+ # @return [String] The ambiguous token
146
+ attr_reader :token
147
+ # @return [String] The UUID when decoded using version 0
148
+ attr_reader :v0
149
+ # @return [String] The UUID when decoded using version 1
150
+ attr_reader :v1
151
+
152
+ # @param token [#to_s] The token in question
153
+ # @param v0 [#to_s] UUID decoded with decoding scheme version 0
154
+ # @param v1 [#to_s] UUID decoded with decoding scheme version 1
155
+
156
+ def initialize token, v0: nil, v1: nil
157
+ @v0 = v0 || from_ncname(token, version: 0)
158
+ @v1 = v1 || from_ncname(token, version: 1)
159
+ end
160
+ end
161
+
140
162
  # Converts a UUID (or object that when converted to a string looks
141
163
  # like a UUID) to an NCName. By default it produces the Base64
142
164
  # variant.
@@ -148,7 +170,7 @@ module UUID::NCName
148
170
  # @param radix [32, 64] either the number 32 or the number 64.
149
171
  #
150
172
  # @param version [0, 1] An optional formatting version, where 0 is
151
- # the naïve original version and 1 moves the `variant` nybble out
173
+ # the naïve original version and 1 moves the +variant+ nybble out
152
174
  # to the end of the identifier. You will be warned for the time
153
175
  # being if you do not set this parameter explicitly. The default
154
176
  # version is 1.
@@ -160,9 +182,10 @@ module UUID::NCName
160
182
  # Base64, the overhang is only ever 4 bits. This means that when
161
183
  # the terminating character is aligned, it will always be in the
162
184
  # range of the letters A through P in (the RFC 3548/4648
163
- # representations of) both Base32 and Base64. When `version` is 1
185
+ # representations of) both Base32 and Base64. When +version+ is 1
164
186
  # and the terminating character is aligned, RFC4122-compliant UUIDs
165
- # will always terminate with I, J, K, or L. Defaults to `true`.
187
+ # will always terminate with +I+, +J+, +K+, or +L+. Defaults to
188
+ # +true+.
166
189
  #
167
190
  # @return [String] The NCName-formatted UUID.
168
191
 
@@ -170,7 +193,7 @@ module UUID::NCName
170
193
  raise 'Radix must be either 32 or 64' unless [32, 64].include? radix
171
194
  raise 'UUID must be something stringable' if uuid.nil? or
172
195
  not uuid.respond_to? :to_s
173
- raise 'Align must be true or false' unless [true, false].include? align
196
+ align = !!align # coerce to a boolean
174
197
 
175
198
  # XXX remove this when appropriate
176
199
  version = warn_version(version)
@@ -195,9 +218,9 @@ module UUID::NCName
195
218
  raise 'Binary representation of UUID is shorter than 16 bytes' if
196
219
  bin.length < 16
197
220
 
198
- uuidver, content = TRANSFORM[version][0].call bin[0, 16]
221
+ uuidver, content = TRANSFORM[version].first.call bin[0, 16]
199
222
 
200
- encode_version(uuidver) + ENCODE[radix].call(content, align)
223
+ encode_version(uuidver, radix) + ENCODE[radix].call(content, align)
201
224
  end
202
225
 
203
226
  # Converts an NCName-encoded UUID back to its canonical
@@ -210,16 +233,16 @@ module UUID::NCName
210
233
  # @param radix [nil, 32, 64] Optional radix; will use heuristic if omitted.
211
234
  #
212
235
  # @param format [:str, :hex, :b64, :bin] An optional formatting
213
- # parameter; defaults to `:str`, the canonical string representation.
236
+ # parameter; defaults to +:str+, the canonical string representation.
214
237
  #
215
238
  # @param version [0, 1] See ::to_ncname. Defaults to 1.
216
239
  #
217
240
  # @param align [nil, true, false] See ::to_ncname for details.
218
- # Setting this parameter to `nil`, the default, will cause the
241
+ # Setting this parameter to +nil+, the default, will cause the
219
242
  # decoder to detect the alignment state from the identifier.
220
243
  #
221
- # @param validate [false, true] Check that the ninth octet is
222
- # correctly masked _after_ decoding.
244
+ # @param validate [false, true] Check that the ninth (the variant)
245
+ # octet is correctly masked _after_ decoding.
223
246
  #
224
247
  # @return [String, nil] The corresponding UUID or nil if the input
225
248
  # is malformed.
@@ -337,17 +360,19 @@ module UUID::NCName
337
360
  # version. This method can positively identify a token as a UUID
338
361
  # NCName, but there is a small subset of UUIDs which will produce
339
362
  # tokens which are valid in both versions. The method returns
340
- # `false` if the token is invalid, otherwise it returns `0` or `1`
363
+ # +false+ if the token is invalid, otherwise it returns +0+ or +1+
341
364
  # for the guessed version.
342
365
  #
343
- # @note Version 1 tokens always end with I, J, K, or L (with base32
344
- # being case-insensitive), so tokens that end in something else will
345
- # be version 0.
366
+ # @note Version 1 tokens always end with +I+, +J+, +K+, or +L+ (with
367
+ # base32 being case-insensitive), so tokens that end in something
368
+ # else will always be version 0.
346
369
  #
347
370
  # @param token [#to_s] The token to test
348
371
  #
372
+ # @param strict [false, true]
373
+ #
349
374
  # @return [false, 0, 1]
350
- def self.valid? token
375
+ def self.valid? token, strict: false
351
376
  token = token.to_s
352
377
  if /^[A-Pa-p](?:[0-9A-Za-z_-]{21}|[2-7A-Za-z]{25})$/.match token
353
378
  # false is definitely version zero but true is only maybe version 1
@@ -356,14 +381,18 @@ module UUID::NCName
356
381
  # try decoding with validation on
357
382
  uu = from_ncname token, version: version, validate: true
358
383
 
359
- if version == 1 and !uu
360
- # try version zero
361
- uu = from_ncname token, version: 0, validate: true
362
- # either zero or invalid
363
- uu ? 0 : false
364
- else
365
- version
384
+ # note that version 1 will always return something because the
385
+ # method of detecting it is a version 1 also happens to be the
386
+ # method of determining whether or not it is valid.
387
+ return false unless uu
388
+
389
+ if version == 1 and strict
390
+ # but we can also check if the input is a valid version 0
391
+ u0 = from_ncname token, version: 0, validate: true
392
+ raise AmbiguousToken.new(token, v0: u0, v1: uu) if u0
366
393
  end
394
+
395
+ version
367
396
  else
368
397
  false
369
398
  end
@@ -6,5 +6,5 @@ unless Module.const_defined? 'UUID'
6
6
  end
7
7
 
8
8
  module UUID::NCName
9
- VERSION = "0.2.5"
9
+ VERSION = "0.2.6"
10
10
  end
@@ -33,9 +33,9 @@ DESC
33
33
  # spec.add_runtime_dependency 'uuidtools', '~> 2.1.5'
34
34
  spec.add_runtime_dependency 'base32', '~> 0.3.2'
35
35
 
36
- spec.add_development_dependency 'bundler', '~> 1.16'
37
- spec.add_development_dependency 'rake', '~> 10.0'
38
- spec.add_development_dependency 'rspec', '~> 3.0'
36
+ spec.add_development_dependency 'bundler', '~> 2.1'
37
+ spec.add_development_dependency 'rake', '~> 13.0'
38
+ spec.add_development_dependency 'rspec', '~> 3.9'
39
39
 
40
40
  # only need it for testing, who knew
41
41
  # spec.add_development_dependency 'uuidtools', '~> 2.1.5'
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: uuid-ncname
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.5
4
+ version: 0.2.6
5
5
  platform: ruby
6
6
  authors:
7
7
  - Dorian Taylor
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2019-11-19 00:00:00.000000000 Z
11
+ date: 2020-07-28 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: base32
@@ -30,42 +30,42 @@ dependencies:
30
30
  requirements:
31
31
  - - "~>"
32
32
  - !ruby/object:Gem::Version
33
- version: '1.16'
33
+ version: '2.1'
34
34
  type: :development
35
35
  prerelease: false
36
36
  version_requirements: !ruby/object:Gem::Requirement
37
37
  requirements:
38
38
  - - "~>"
39
39
  - !ruby/object:Gem::Version
40
- version: '1.16'
40
+ version: '2.1'
41
41
  - !ruby/object:Gem::Dependency
42
42
  name: rake
43
43
  requirement: !ruby/object:Gem::Requirement
44
44
  requirements:
45
45
  - - "~>"
46
46
  - !ruby/object:Gem::Version
47
- version: '10.0'
47
+ version: '13.0'
48
48
  type: :development
49
49
  prerelease: false
50
50
  version_requirements: !ruby/object:Gem::Requirement
51
51
  requirements:
52
52
  - - "~>"
53
53
  - !ruby/object:Gem::Version
54
- version: '10.0'
54
+ version: '13.0'
55
55
  - !ruby/object:Gem::Dependency
56
56
  name: rspec
57
57
  requirement: !ruby/object:Gem::Requirement
58
58
  requirements:
59
59
  - - "~>"
60
60
  - !ruby/object:Gem::Version
61
- version: '3.0'
61
+ version: '3.9'
62
62
  type: :development
63
63
  prerelease: false
64
64
  version_requirements: !ruby/object:Gem::Requirement
65
65
  requirements:
66
66
  - - "~>"
67
67
  - !ruby/object:Gem::Version
68
- version: '3.0'
68
+ version: '3.9'
69
69
  description: |
70
70
  This module creates an isomorphic representation of a UUID which is
71
71
  guaranteed to fit into the grammar of the XML NCName construct, which
@@ -110,7 +110,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
110
110
  - !ruby/object:Gem::Version
111
111
  version: '0'
112
112
  requirements: []
113
- rubygems_version: 3.0.3
113
+ rubygems_version: 3.1.2
114
114
  signing_key:
115
115
  specification_version: 4
116
116
  summary: Format a UUID as a valid NCName.