base32-url 0.6.0 → 0.7.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 6700c1c53464c4318f6361ebe22cf4276c925f24bf92cc6fc841e2075c502e6a
4
- data.tar.gz: 5b58a804226b9714abde687042e207d8d00a10ed0859c716ccefa1c350a7fd69
3
+ metadata.gz: 74ebd73eb20ec6ef25ff4fe3edd29f2346dcea1e8d8620a8151d415948b95194
4
+ data.tar.gz: 131fe2a1c3d678041228e223f4698b50e2b7181ff4c7c15d2e249add8b69f7ae
5
5
  SHA512:
6
- metadata.gz: fa905b9b15aafdeb4ef156da982952e613d397b0f4dda8d5a1145eaeda61b461deeba2410f850564d20246ad5902c84d02d330602d1a30d7b9f9f0ede5b785c4
7
- data.tar.gz: 1831d2538ff6892222341b9082db32ee93456e9385a0521db10a2549ef50eb7940e2cd57bcce3ae82c98518c010e6508c68994ce325699c6ab38293d45eb7780
6
+ metadata.gz: 9db5cdc495c36f92f8707e7b7391cfa2eb0acbe2e722cff72ee55f55bde91447d2e58d38a4dc43d6f255844f51dd912f8c640e1629d48e0de1f0d2fe6e659794
7
+ data.tar.gz: fec4321eb6603afea990c427e90bc421d381dda5188292c34e71049681cd4a9d47132e30530adb2c308038466faf1ff0b338301c1dc62c76164f30c8c49d0969
data/base32-url.gemspec CHANGED
@@ -16,6 +16,7 @@ Gem::Specification.new do |s|
16
16
  s.required_ruby_version = '>= 2.6.0'
17
17
 
18
18
  # Declary dependencies here, rather than in the Gemfile
19
+ s.add_dependency 'uuidtools', '~> 2.1', '>= 2.1.5'
19
20
  s.add_development_dependency 'bundler', '>= 1.11', "< 3"
20
21
  s.add_development_dependency 'rake', '>= 11.3', "< 14"
21
22
  s.add_development_dependency 'rubocop', '~> 1.36'
data/lib/base32/url.rb CHANGED
@@ -35,6 +35,8 @@ end
35
35
  # hyphens to assure symbol string correctness.
36
36
  #
37
37
  #
38
+ require 'uuidtools'
39
+
38
40
  class Base32::URL
39
41
  ENCODE_CHARS =
40
42
  %w[0 1 2 3 4 5 6 7 8 9 a b c d e f g h j k m n p q r s t v w x y z ?].freeze
@@ -81,6 +83,30 @@ class Base32::URL
81
83
  str
82
84
  end
83
85
 
86
+ # encodes a uuid into a string
87
+ #
88
+ # when +checksum+ is given, a checksum is added at the end of the the string,
89
+ # calculated as modulo 97-10 (ISO 7064)
90
+ #
91
+ # when +split+ is given a hyphen is inserted every <n> characters to improve
92
+ # readability
93
+ #
94
+ # when +length+ is given, the resulting string is zero-padded to be exactly
95
+ # this number of characters long (hyphens are ignored)
96
+ #
97
+ # Base32::URL.encode_uuid('0022b9ef-525a-4a79-81ad-13411697f58a') # => "16j"
98
+ # Base32::URL.encode_uuid('6179ad80-cc7f-4904-9260-0ecb3c3a90ba', :split=>5) # => "3g923-0vqvs"
99
+ #
100
+
101
+ def self.encode_uuid(uuid, opts = {})
102
+ # verify options
103
+ raise ArgumentError unless opts.keys - %i[length split checksum] == []
104
+
105
+ number = ::UUIDTools::UUID.parse(uuid).to_i
106
+
107
+ encode(number, opts)
108
+ end
109
+
84
110
  # decode a string to an integer using Douglas Crockfords Base32 Encoding
85
111
  #
86
112
  # the string is converted to uppercase and hyphens are stripped before
@@ -114,6 +140,26 @@ class Base32::URL
114
140
  number
115
141
  end
116
142
 
143
+ # decode a string to a uuid using Douglas Crockfords Base32 Encoding
144
+ #
145
+ # the string is converted to uppercase and hyphens are stripped before
146
+ # decoding
147
+ #
148
+ # I,i,l,L decodes to 1
149
+ # O,o decodes to 0
150
+ #
151
+ # Base32::URL.decode_uuid("4awyymjt99wr3b8k84b9fxca") # => '0022b9ef-525a-4a79-81ad-13411697f58a'
152
+ #
153
+ # returns +nil+ if the string contains invalid characters and can't be
154
+ # decoded, or if checksum option is used and checksum is incorrect
155
+
156
+ def self.decode_uuid(string, opts = {})
157
+ number = decode(string, opts)
158
+ return nil unless number
159
+
160
+ ::UUIDTools::UUID.parse_int(number).to_s
161
+ end
162
+
117
163
  # same as decode, but raises ArgumentError when the string can't be decoded
118
164
  def self.decode!(string, _opts = {})
119
165
  decode(string) or raise ArgumentError
@@ -2,6 +2,6 @@
2
2
 
3
3
  module Base32
4
4
  module Url
5
- VERSION = '0.6.0'
5
+ VERSION = '0.7.0'
6
6
  end
7
7
  end
@@ -24,6 +24,12 @@ class TestBase32Url < Test::Unit::TestCase
24
24
  assert_equal('16j', Base32::URL.encode(1234))
25
25
  end
26
26
 
27
+ def test_encoding_uuids
28
+ assert_equal('4awyymjt99wr3b8k84b9fxca', Base32::URL.encode_uuid('0022b9ef-525a-4a79-81ad-13411697f58a'))
29
+ assert_equal('31f6p-r1k3z94-294r0es-cy3n45t', Base32::URL.encode_uuid('6179ad80-cc7f-4904-9260-0ecb3c3a90ba', split: 7))
30
+ assert_equal('6315bn4-aqg82ja-4a9wxdt-29f7279', Base32::URL.encode_uuid('c3095752-2af0-40a4-a229-3ceb7424bce2', split: 7, checksum: true))
31
+ end
32
+
27
33
  def test_decoding_strings
28
34
  assert_equal(1234, Base32::URL.decode('16j'))
29
35
  end
@@ -43,6 +49,13 @@ class TestBase32Url < Test::Unit::TestCase
43
49
  assert_raises(ArgumentError) { Base32::URL.decode!("'+?") }
44
50
  end
45
51
 
52
+ def test_decoding_uuids
53
+ assert_equal('0022b9ef-525a-4a79-81ad-13411697f58a', Base32::URL.decode_uuid('4awyymjt99wr3b8k84b9fxca'))
54
+ assert_equal('6179ad80-cc7f-4904-9260-0ecb3c3a90ba', Base32::URL.decode_uuid('31f6p-r1k3z94-294r0es-cy3n45t'))
55
+ assert_equal('c3095752-2af0-40a4-a229-3ceb7424bce2', Base32::URL.decode_uuid('6315bn4-aqg82ja-4a9wxdt-29f7279', checksum: true))
56
+ assert_equal(nil, Base32::URL.decode_uuid('6315bn4-aqg82ja-4a9wxdt-29f7297', checksum: true))
57
+ end
58
+
46
59
  def test_decode_should_ignore_hyphens
47
60
  assert_equal 1234, Base32::URL.decode('1-6-j')
48
61
  end
metadata CHANGED
@@ -1,15 +1,35 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: base32-url
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.6.0
4
+ version: 0.7.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Martin Fenner
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2023-03-10 00:00:00.000000000 Z
11
+ date: 2023-06-15 00:00:00.000000000 Z
12
12
  dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: uuidtools
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '2.1'
20
+ - - ">="
21
+ - !ruby/object:Gem::Version
22
+ version: 2.1.5
23
+ type: :runtime
24
+ prerelease: false
25
+ version_requirements: !ruby/object:Gem::Requirement
26
+ requirements:
27
+ - - "~>"
28
+ - !ruby/object:Gem::Version
29
+ version: '2.1'
30
+ - - ">="
31
+ - !ruby/object:Gem::Version
32
+ version: 2.1.5
13
33
  - !ruby/object:Gem::Dependency
14
34
  name: bundler
15
35
  requirement: !ruby/object:Gem::Requirement
@@ -167,7 +187,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
167
187
  - !ruby/object:Gem::Version
168
188
  version: '0'
169
189
  requirements: []
170
- rubygems_version: 3.4.8
190
+ rubygems_version: 3.4.14
171
191
  signing_key:
172
192
  specification_version: 4
173
193
  summary: 32-symbol notation for expressing numbers in a form that can be conveniently