uri-ni 0.2.3 → 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.
- checksums.yaml +4 -4
- data/lib/uri/ni/version.rb +1 -1
- data/lib/uri/ni.rb +44 -6
- metadata +1 -1
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 971b72993168c64b86e07c62460ba32c22f3d2849f1b2bdcabf9d455b9ab320a
|
|
4
|
+
data.tar.gz: 9e90ac190a4ef2a956d24d9f90d2b460cca4b2af18abc7b695ca0bf5ea80f371
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 8499f77ffdeb429a6b3d07021cfd4d825b492761bb780a2dff37fbee2c2955f2d7552aa3e11013bda54a04315bf3d8301b128abce4d3d8ea2048dc6b78b2ecfd
|
|
7
|
+
data.tar.gz: '08399a8e69433a93246327d336776f879fddb2fc6bdfd38e97d125aa974c4fa92297097c1e48389a7a7c17076874f2ad607eed1861de01889951e3bfcb894566'
|
data/lib/uri/ni/version.rb
CHANGED
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
|
|
@@ -198,15 +231,20 @@ class URI::NI < URI::Generic
|
|
|
198
231
|
# @return [URI::NI] the transformed identifier
|
|
199
232
|
#
|
|
200
233
|
def self.ingest algorithm = nil, digest
|
|
201
|
-
|
|
202
|
-
|
|
234
|
+
if digest.is_a? Digest::Instance
|
|
235
|
+
# this will complain
|
|
236
|
+
algo_for digest, algorithm
|
|
237
|
+
return compute(digest)
|
|
238
|
+
elsif tmp = try_uri(digest)
|
|
239
|
+
raise ArgumentError,
|
|
240
|
+
"digest algorithm #{tmp.algorithm} does not match #{algorithm}" if
|
|
241
|
+
algorithm && algorithm != tmp.algorithm
|
|
242
|
+
return tmp
|
|
243
|
+
end
|
|
203
244
|
|
|
204
|
-
#
|
|
245
|
+
# okay now we should have a string that's just the hash
|
|
205
246
|
digest = digest.to_s
|
|
206
247
|
|
|
207
|
-
# just parse if it's already a ni: URI string
|
|
208
|
-
return URI(digest) if /^ni:/i.match? digest
|
|
209
|
-
|
|
210
248
|
# get the expected length of the digest for the algorithm
|
|
211
249
|
len = LENGTHS[algorithm.to_s.downcase.to_sym] or raise ArgumentError,
|
|
212
250
|
"unsupported algorithm #{algorithm}"
|