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.
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 +44 -6
  4. metadata +1 -1
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 72fad83584f69d2982732c3a0578e6a19a740b4aab3892b5d80e0a73f97b3e9e
4
- data.tar.gz: 94e56f2e88436acb04627be6d8b5b87f3a357dd0c1828ef3569ccdda21541812
3
+ metadata.gz: 971b72993168c64b86e07c62460ba32c22f3d2849f1b2bdcabf9d455b9ab320a
4
+ data.tar.gz: 9e90ac190a4ef2a956d24d9f90d2b460cca4b2af18abc7b695ca0bf5ea80f371
5
5
  SHA512:
6
- metadata.gz: 1d00ea6cb3b0b5dadac6097871743c4f4e709f884093dafd488c8e2a3d0bc2beae7e48c6bf54db509380137a671f0705abf464e18f41a43bbc7cd9500fe7f640
7
- data.tar.gz: bd0774e708e1ec4f5b570a1770bb12363520282e802705ce4b76b2235d186e089c42773374388f56f71c5d6c1ae8958fe95e63a2435410d7191c37dd401b2a24
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.3"
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
@@ -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
- return compute(digest) if digest.is_a? Digest::Instance
202
- return digest if digest.is_a? URI::NI # noop
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
- # make sure we're indeed dealing with a string
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}"
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.3
4
+ version: 0.2.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Dorian Taylor