uri-ni 0.2.4 → 0.2.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.
Files changed (4) hide show
  1. checksums.yaml +4 -4
  2. data/lib/uri/ni/version.rb +1 -1
  3. data/lib/uri/ni.rb +38 -15
  4. metadata +1 -1
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 6f21e8e7bf5c19fc3eedf94e5a41c401c8bf0c49153b70abf1ec27d301811c9c
4
- data.tar.gz: 10db96336f2f2509b4079d295af3bac755ecfdc07d06c2fc43fbebb21ba95974
3
+ metadata.gz: 971b72993168c64b86e07c62460ba32c22f3d2849f1b2bdcabf9d455b9ab320a
4
+ data.tar.gz: 9e90ac190a4ef2a956d24d9f90d2b460cca4b2af18abc7b695ca0bf5ea80f371
5
5
  SHA512:
6
- metadata.gz: 803def5e52f0265a7cbb881bb874e3d306f352b79f24eee939981591692dad450bb3be5d5f88de050939a8c37154531935401e6f8abf62a7886583b2ed8527ac
7
- data.tar.gz: d9081a72a683c62a550c28bb19c0c713913a20addebfbb9e203bceb5a04262f982d841bd5d65b0e773722ab10c15ec99da12f88ce9b67734f0a25a27ab00b4fb
6
+ metadata.gz: 8499f77ffdeb429a6b3d07021cfd4d825b492761bb780a2dff37fbee2c2955f2d7552aa3e11013bda54a04315bf3d8301b128abce4d3d8ea2048dc6b78b2ecfd
7
+ data.tar.gz: '08399a8e69433a93246327d336776f879fddb2fc6bdfd38e97d125aa974c4fa92297097c1e48389a7a7c17076874f2ad607eed1861de01889951e3bfcb894566'
@@ -3,7 +3,7 @@ require 'uri/generic'
3
3
 
4
4
  module URI
5
5
  class NI < Generic
6
- VERSION = "0.2.4"
6
+ VERSION = "0.2.5"
7
7
  end
8
8
 
9
9
  # might as well put this here
data/lib/uri/ni.rb CHANGED
@@ -182,6 +182,39 @@ class URI::NI < URI::Generic
182
182
  @host = v.to_s
183
183
  end
184
184
 
185
+ # This will attempt to coerce the input into a {URI::NI} if it's possible.
186
+ #
187
+ # @param arg [#to_s, URI::Generic] something that might be a URI
188
+ #
189
+ # @raise [URI::Error] if the URI itself is malformed
190
+ # @raise [ArgumentError] if it is not a `ni:` URI or HTTP(S) with
191
+ # the `/.well-known/ni/…` construct
192
+ #
193
+ # @return [nil, URI::NI] the hash URI (maybe)
194
+ #
195
+ def self.try_uri arg
196
+ unless arg.is_a? URI
197
+ arg = arg.to_s
198
+ return unless %r{^(?i:ni|https?)://}.match? arg
199
+ arg = URI(arg)
200
+ end
201
+
202
+ # normalize the uri
203
+ arg = arg.normalize
204
+
205
+ # if HTTP(S) URI with `/.well-known/ni/…`, convert it
206
+ if m = %r{^/+\.well-known/+ni/+([^/]+)/+([0-9A-Za-z_-]+)$}.match(arg.path)
207
+ arg = URI('ni:///%s;%s' % m.captures)
208
+ end
209
+
210
+ raise ArgumentError,
211
+ "don't know what to do with a #{arg.scheme}: URI" unless
212
+ arg.is_a? URI::NI
213
+
214
+ # return it
215
+ arg.dup
216
+ end
217
+
185
218
  public
186
219
 
187
220
  # Transform a digest for a known algorithm into a {URI::NI}. Takes a
@@ -202,26 +235,16 @@ class URI::NI < URI::Generic
202
235
  # this will complain
203
236
  algo_for digest, algorithm
204
237
  return compute(digest)
205
- elsif digest.is_a? URI::NI
238
+ elsif tmp = try_uri(digest)
206
239
  raise ArgumentError,
207
- "digest algorithm #{digest.algorithm} does not match #{algorithm}" if
208
- algorithm && algorithm != digest.algorithm
209
- return digest
240
+ "digest algorithm #{tmp.algorithm} does not match #{algorithm}" if
241
+ algorithm && algorithm != tmp.algorithm
242
+ return tmp
210
243
  end
211
244
 
212
- # make sure we're indeed dealing with a string
245
+ # okay now we should have a string that's just the hash
213
246
  digest = digest.to_s
214
247
 
215
- # just parse if it's already a ni: URI string
216
- if /^ni:/i.match? digest
217
- digest = URI(digest)
218
- raise ArgumentError,
219
- "digest algorithm #{digest.algorithm} does not match #{algorithm}" if
220
- algorithm && algorithm != digest.algorithm
221
-
222
- return digest
223
- end
224
-
225
248
  # get the expected length of the digest for the algorithm
226
249
  len = LENGTHS[algorithm.to_s.downcase.to_sym] or raise ArgumentError,
227
250
  "unsupported algorithm #{algorithm}"
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: uri-ni
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.4
4
+ version: 0.2.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Dorian Taylor