uuid-ncname 0.3.1 → 0.4.0

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: d17bfbafda1f25ed22fe1b314af7d9add89e66caa92a5f859fe399b81b4a1c63
4
- data.tar.gz: 38372398d6460a72212023f6904c2eef6b5785461169f8ff7e1ff4248aa08ea2
3
+ metadata.gz: 471f6d4d6e5124454b7bcc1a7ab46fc3f6fbf6951f4ba988c80a9b04f0051be1
4
+ data.tar.gz: 03265d39a6f8b4c38ad9d86ddab9753563216c4ac3421a5cff9f38f1e536d4e1
5
5
  SHA512:
6
- metadata.gz: c2f4d687ce200545784b6cd57417d182701faa805bb11e963184418284bc134b02e191a099026a63183aad8f089fd9eb140331fcd3915f33a1ea5e596e97836a
7
- data.tar.gz: dcbd3afa99943709d07892fff109659dc567fbb3c7ab285cd5b9281894cb0477d984fd9ca0de12b778bd4d7125643299ce9ac61047fbd3a22ce35a91bb774722
6
+ metadata.gz: 311e0f9856467d875782d37ac759ec2479d74276ad2ec427588954ab22335e233d69f2d32e6ceaf20a24e91eba8a2cd780721f89f5a829c7acf346ab78b20401
7
+ data.tar.gz: d9458bc9b998ac9fb0d5450e66a73d49bbc5bd546250d221045c4b17361cc9a02fa9a0c9b2dbf012686e4bc0c60336635b6946e7f531a8ba80900336e24affa9
data/README.md CHANGED
@@ -7,19 +7,17 @@ require 'uuidtools'
7
7
  uu = UUIDTools::UUID.random_create
8
8
  # => #<UUID:0x3fff0e597ef8 UUID:df521e0a-9d57-4f04-9a95-fc2888decc5a>
9
9
 
10
- # see below about this :version parameter
11
-
12
- nc64 = UUID::NCName.to_ncname uu, version: 1
10
+ nc64 = UUID::NCName.to_ncname uu
13
11
  # => "E31IeCp1X8EqV_CiI3sxaJ"
14
12
 
15
- nc32 = UUID::NCName.to_ncname_32 uu, version: 1
13
+ nc32 = UUID::NCName.to_ncname_32 uu
16
14
  # => "E35jb4cu5k7yevfp4fcen5tc2j"
17
15
 
18
- orig = UUID::NCName.from_ncname nc64, version: 1
16
+ orig = UUID::NCName.from_ncname nc64
19
17
  # => "df521e0a-9d57-4f04-9a95-fc2888decc5a"
20
18
 
21
- orig == UUID::NCName.from_ncname nc32, version: 1 # => true
22
- orig == uu.to_s # => true
19
+ orig == UUID::NCName.from_ncname nc32 # => true
20
+ orig == uu.to_s # => true
23
21
 
24
22
  # then you can turn it back into an object or whatever
25
23
  uu == UUIDTools::UUID.parse(orig) # => true
@@ -27,69 +25,12 @@ uu == UUIDTools::UUID.parse(orig) # => true
27
25
 
28
26
  ## Description
29
27
 
30
- The purpose of this module is to devise an alternative representation
28
+ The purpose of this module is to [devise an alternative
29
+ representation](https://datatracker.ietf.org/doc/html/draft-taylor-uuid-ncname)
31
30
  of the [UUID](http://tools.ietf.org/html/rfc4122) which conforms to
32
- the constraints of various other identifiers such as NCName, and create an
33
- [isomorphic](http://en.wikipedia.org/wiki/Isomorphism) mapping between
34
- them.
35
-
36
- ## _FORMAT DEPRECATION NOTICE_
37
-
38
- After careful consideration, I have decided to change the UUID-NCName
39
- format in a minor yet incompatible way. In particular, I have moved
40
- the nybble containing
41
- the [`variant`](https://tools.ietf.org/html/rfc4122#section-4.1.1) to
42
- the very end of the identifier, whereas it previously was mixed into
43
- the middle somewhere.
44
-
45
- This can be considered an application
46
- of [Postel's Law](https://en.wikipedia.org/wiki/Postel%27s_law), based
47
- on the assumption that these identifiers will be generated through
48
- other methods, and potentially naïvely. Like the `version` field, the
49
- `variant` field has a limited acceptable range of values. If, for
50
- example, one were to attempt to generate a conforming identifier by
51
- simply generating a random Base32 or Base64 string, it will be
52
- difficult to ensure that the `variant` field will indeed conform when
53
- the identifier is converted to a standard UUID. By moving the
54
- `variant` field out to the end of the identifier, everything between
55
- the `version` and `variant` bookends can be generated randomly without
56
- any further consideration, like so:
57
-
58
- ```ruby
59
- B64_ALPHA = ('A'..'Z').to_a + ('a'..'z').to_a + ('0'..'9').to_a + %w(- _)
60
-
61
- def make_cheapo_b64_uuid_ncname
62
- vals = (1..20).map { rand 64 } # generate the content
63
- vals.push(rand(4) + 8) # last digit is special
64
- 'E' + vals.map { |v| B64_ALPHA[v] }.join('') # 'E' for UUID v4
65
- end
66
-
67
- # voilà:
68
-
69
- cheap = make_cheapo_b64_uuid_ncname
70
- # => "EXSVv8ezPbSKWoKOkBNWKL"
71
-
72
- # now try changing it to a standard UUID:
73
-
74
- UUID::NCName.from_ncname cheap, version: 1
75
- # => "5d256ff1-eccf-46d2-b296-a0a3a404d58a"
76
- ```
77
-
78
- Furthermore, since the default behaviour is to align the bits of the
79
- last byte to the size of the encoding symbol, and since the `variant`
80
- bits are masked, a compliant RFC4122 UUID will _always_ end with `I`,
81
- `J`, `K`, or `L`, in _both_ Base32 (case-insensitive) and Base64
82
- variants.
83
-
84
- Since I have already released this gem prior to this format change, I
85
- have added a `:version` parameter to both `to_ncname` and
86
- `from_ncname`. The version, as of 0.2.4, now defaults to `1`, the
87
- current one, but will still issue a warning if not explicitly
88
- set. Later I will finally remove the warning. This should ensure that
89
- any code written during the transition produces the correct results.
90
-
91
- > Unless you have to support identifiers generated from version 0.1.3
92
- > or older, you should be running these methods with `version: 1`.
31
+ the constraints of various other identifiers such as NCName, and
32
+ create an [isomorphic](http://en.wikipedia.org/wiki/Isomorphism)
33
+ mapping between them.
93
34
 
94
35
  ## Rationale & Method
95
36
 
@@ -198,6 +139,62 @@ representation should be adequate for placeholder symbols in just
198
139
  about any programming language, save for those which do not permit
199
140
  identifiers as long as 26 characters (which are extremely scarce).
200
141
 
142
+ ## _FORMAT DEPRECATION NOTICE_
143
+
144
+ After careful consideration, I have decided to change the UUID-NCName
145
+ format in a minor yet incompatible way. In particular, I have moved
146
+ the nybble containing
147
+ the [`variant`](https://tools.ietf.org/html/rfc4122#section-4.1.1) to
148
+ the very end of the identifier, whereas it previously was mixed into
149
+ the middle somewhere.
150
+
151
+ This can be considered an application
152
+ of [Postel's Law](https://en.wikipedia.org/wiki/Postel%27s_law), based
153
+ on the assumption that these identifiers will be generated through
154
+ other methods, and potentially naïvely. Like the `version` field, the
155
+ `variant` field has a limited acceptable range of values. If, for
156
+ example, one were to attempt to generate a conforming identifier by
157
+ simply generating a random Base32 or Base64 string, it will be
158
+ difficult to ensure that the `variant` field will indeed conform when
159
+ the identifier is converted to a standard UUID. By moving the
160
+ `variant` field out to the end of the identifier, everything between
161
+ the `version` and `variant` bookends can be generated randomly without
162
+ any further consideration, like so:
163
+
164
+ ```ruby
165
+ B64_ALPHA = ('A'..'Z').to_a + ('a'..'z').to_a + ('0'..'9').to_a + %w(- _)
166
+
167
+ def make_cheapo_b64_uuid_ncname
168
+ vals = (1..20).map { rand 64 } # generate the content
169
+ vals.push(rand(4) + 8) # last digit is special
170
+ 'E' + vals.map { |v| B64_ALPHA[v] }.join('') # 'E' for UUID v4
171
+ end
172
+
173
+ # voilà:
174
+
175
+ cheap = make_cheapo_b64_uuid_ncname
176
+ # => "EXSVv8ezPbSKWoKOkBNWKL"
177
+
178
+ # now try changing it to a standard UUID:
179
+
180
+ UUID::NCName.from_ncname cheap, version: 1
181
+ # => "5d256ff1-eccf-46d2-b296-a0a3a404d58a"
182
+ ```
183
+
184
+ Furthermore, since the default behaviour is to align the bits of the
185
+ last byte to the size of the encoding symbol, and since the `variant`
186
+ bits are masked, a compliant RFC4122 UUID will _always_ end with `I`,
187
+ `J`, `K`, or `L`, in _both_ Base32 (case-insensitive) and Base64
188
+ variants.
189
+
190
+ Since I have already released this gem prior to this format change, I
191
+ have added a `:version` parameter to both `to_ncname` and
192
+ `from_ncname`. This parameter, which controls the compact UUID spec
193
+ behaviour, defaults to `1`, as of the _module_ version 0.2.4.
194
+
195
+ > Unless you have to support identifiers generated from version 0.1.3
196
+ > or older, you should be running these methods with `version: 1`.
197
+
201
198
  ## Documentation
202
199
 
203
200
  Generated and deposited
@@ -6,5 +6,5 @@ unless Module.const_defined? 'UUID'
6
6
  end
7
7
 
8
8
  module UUID::NCName
9
- VERSION = "0.3.1"
9
+ VERSION = "0.4.0"
10
10
  end
data/lib/uuid/ncname.rb CHANGED
@@ -61,8 +61,11 @@ module UUID::NCName
61
61
  },
62
62
  58 => -> (str, _) {
63
63
  variant = decode_version(str[-1]) << 4
64
+ # warn str
64
65
  str = str.chop.tr ?_, ''
65
- ::Base58.base58_to_binary(str, :bitcoin) + variant.chr
66
+ # warn str
67
+ # warn ::Base58.base58_to_binary(str, :bitcoin).length
68
+ ::Base58.base58_to_binary(str, :bitcoin) + variant.chr.b
66
69
  },
67
70
  64 => -> (str, align = true) {
68
71
  str = str[0, 21] + 'A=='
@@ -77,6 +80,7 @@ module UUID::NCName
77
80
 
78
81
  FORMAT = {
79
82
  str: -> bin { UUF % bin.unpack('C*') },
83
+ urn: -> bin { "urn:uuid:#{UUF % bin.unpack('C*')}" },
80
84
  hex: -> bin { bin.unpack 'H*' },
81
85
  b64: -> bin { ::Base64.strict_encode64 bin },
82
86
  bin: -> bin { bin },
@@ -125,7 +129,10 @@ module UUID::NCName
125
129
  -> (version, data) {
126
130
  version &= 0xf
127
131
 
132
+ # warn data.length
133
+
128
134
  list = data.unpack 'N4'
135
+ # warn list.inspect
129
136
  variant = (list[3] & 0xf0) << 24
130
137
  list[3] >>= 8
131
138
  list[3] |= ((list[2] & 0xff) << 24)
@@ -148,6 +155,16 @@ module UUID::NCName
148
155
  (version.upcase.ord - 65) % 16
149
156
  end
150
157
 
158
+ def self.assert_version version
159
+ version = 1 unless version
160
+ raise ArgumentError, "version #{version.inspect} is not an integer" unless
161
+ version.respond_to? :to_i
162
+ version = version.to_i
163
+ raise ArgumentError, "there is no version #{version}" unless
164
+ TRANSFORM[version]
165
+ version
166
+ end
167
+
151
168
  def self.warn_version version
152
169
  if version.nil?
153
170
  warn 'Set an explicit :version to remove this warning. See documentation.'
@@ -194,9 +211,7 @@ module UUID::NCName
194
211
  #
195
212
  # @param version [0, 1] An optional formatting version, where 0 is
196
213
  # the naïve original version and 1 moves the `variant` nybble out
197
- # to the end of the identifier. You will be warned for the time
198
- # being if you do not set this parameter explicitly. The default
199
- # version is 1.
214
+ # to the end of the identifier. The default version is 1.
200
215
  #
201
216
  # @param align [true, false] Optional directive to treat the
202
217
  # terminating character as aligned to the numerical base of the
@@ -220,7 +235,8 @@ module UUID::NCName
220
235
  align = !!align # coerce to a boolean
221
236
 
222
237
  # XXX remove this when appropriate
223
- version = warn_version(version)
238
+ # version = warn_version(version)
239
+ version = assert_version version
224
240
 
225
241
  uuid = uuid.to_s
226
242
  bin = nil
@@ -254,9 +270,10 @@ module UUID::NCName
254
270
  # @param ncname [#to_s] an NCName-encoded UUID, either a
255
271
  # 22-character (Base64) variant, or a 26-character (Base32) variant.
256
272
  #
257
- # @param radix [nil, 32, 64] Optional radix; will use heuristic if omitted.
273
+ # @param radix [nil, 32, 58, 64] Optional radix; will use a heuristic
274
+ # if omitted.
258
275
  #
259
- # @param format [:str, :hex, :b64, :bin] An optional formatting
276
+ # @param format [:str, :urn, :hex, :b64, :bin] An optional formatting
260
277
  # parameter; defaults to `:str`, the canonical string representation.
261
278
  #
262
279
  # @param version [0, 1] See ::to_ncname. Defaults to 1.
@@ -279,7 +296,8 @@ module UUID::NCName
279
296
  [true, false, nil].include? align
280
297
 
281
298
  # XXX remove this when appropriate
282
- version = warn_version version
299
+ # version = warn_version version
300
+ version = assert_version version
283
301
 
284
302
  return unless ncname and ncname.respond_to? :to_s
285
303
 
@@ -342,7 +360,8 @@ module UUID::NCName
342
360
  # is malformed.
343
361
  #
344
362
  def self.from_ncname_64 ncname, format: :str, version: nil, align: nil
345
- from_ncname ncname, radix: 64, format: format
363
+ from_ncname ncname,
364
+ radix: 64, format: format, version: version, align: align
346
365
  end
347
366
 
348
367
  # Shorthand for conversion to the Base58 version
@@ -373,7 +392,8 @@ module UUID::NCName
373
392
  # is malformed.
374
393
  #
375
394
  def self.from_ncname_58 ncname, format: :str, version: nil, align: nil
376
- from_ncname ncname, radix: 58, format: format
395
+ from_ncname ncname,
396
+ radix: 58, format: format, version: version, align: align
377
397
  end
378
398
 
379
399
  # Shorthand for conversion to the Base32 version
@@ -404,7 +424,8 @@ module UUID::NCName
404
424
  # is malformed.
405
425
  #
406
426
  def self.from_ncname_32 ncname, format: :str, version: nil, align: nil
407
- from_ncname ncname, radix: 32, format: format
427
+ from_ncname ncname,
428
+ radix: 32, format: format, version: version, align: align
408
429
  end
409
430
 
410
431
  # Test if the given token is a UUID NCName, with a hint to its
data/uuid-ncname.gemspec CHANGED
@@ -27,16 +27,16 @@ DESC
27
27
  spec.require_paths = ["lib"]
28
28
 
29
29
  # we use named parameters
30
- spec.required_ruby_version = '~> 2.0'
30
+ spec.required_ruby_version = '>= 2.0'
31
31
 
32
32
  # surprisingly do not need this
33
33
  # spec.add_runtime_dependency 'uuidtools', '~> 2.1.5'
34
34
  spec.add_runtime_dependency 'base32', '~> 0.3.2'
35
35
  spec.add_runtime_dependency 'base58', '~> 0.2.3'
36
36
 
37
- spec.add_development_dependency 'bundler', '~> 2.1'
37
+ spec.add_development_dependency 'bundler', '~> 2.2'
38
38
  spec.add_development_dependency 'rake', '~> 13.0'
39
- spec.add_development_dependency 'rspec', '~> 3.9'
39
+ spec.add_development_dependency 'rspec', '~> 3.10'
40
40
 
41
41
  # only need it for testing, who knew
42
42
  # 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.3.1
4
+ version: 0.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Dorian Taylor
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2021-01-16 00:00:00.000000000 Z
11
+ date: 2021-11-25 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: base32
@@ -44,14 +44,14 @@ dependencies:
44
44
  requirements:
45
45
  - - "~>"
46
46
  - !ruby/object:Gem::Version
47
- version: '2.1'
47
+ version: '2.2'
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: '2.1'
54
+ version: '2.2'
55
55
  - !ruby/object:Gem::Dependency
56
56
  name: rake
57
57
  requirement: !ruby/object:Gem::Requirement
@@ -72,14 +72,14 @@ dependencies:
72
72
  requirements:
73
73
  - - "~>"
74
74
  - !ruby/object:Gem::Version
75
- version: '3.9'
75
+ version: '3.10'
76
76
  type: :development
77
77
  prerelease: false
78
78
  version_requirements: !ruby/object:Gem::Requirement
79
79
  requirements:
80
80
  - - "~>"
81
81
  - !ruby/object:Gem::Version
82
- version: '3.9'
82
+ version: '3.10'
83
83
  description: |
84
84
  This module creates an isomorphic representation of a UUID which is
85
85
  guaranteed to fit into the grammar of the XML NCName construct, which
@@ -116,7 +116,7 @@ require_paths:
116
116
  - lib
117
117
  required_ruby_version: !ruby/object:Gem::Requirement
118
118
  requirements:
119
- - - "~>"
119
+ - - ">="
120
120
  - !ruby/object:Gem::Version
121
121
  version: '2.0'
122
122
  required_rubygems_version: !ruby/object:Gem::Requirement
@@ -125,7 +125,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
125
125
  - !ruby/object:Gem::Version
126
126
  version: '0'
127
127
  requirements: []
128
- rubygems_version: 3.1.4
128
+ rubygems_version: 3.2.22
129
129
  signing_key:
130
130
  specification_version: 4
131
131
  summary: Format a UUID as a valid NCName.