berkeley_library-util 0.1.3 → 0.1.4

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: c52542412eba63da568f64ede565f97e941321d108f42e89920341df02a268b2
4
- data.tar.gz: 3a56f6a5dd154c44d3d94e601f3093a8c4aca81e05fb3f7e18cd70ca6389f930
3
+ metadata.gz: 57ff7fc89570ad61496f1587934e40dc18752df9cd99d766f591c4fe03fcde51
4
+ data.tar.gz: e0bc0403c067c04801683e12de79a569721332ac23c770a05a0a0fde41842214
5
5
  SHA512:
6
- metadata.gz: 0ff8d42319d6faa217bb540d7276a7dd48499a9660242a3c875496594105a16a6bd63190ea234781b8950d269548d4400cabcf87292a161eb16cf8f5a3d205b8
7
- data.tar.gz: ebfbe371422bbbb59587d583409200968742511013aa49df1f2922df5c9880102c3014ebe7622a7be663cd327a5a742f69c622e8369a912929edad1cc4a0b4fe
6
+ metadata.gz: 6f7786f78fd64266dab9e424f61c03a95dc5a7eb294990bfef1d124699d5e0f6db7b53eaa2d3ca84c77eea1e096b11a5d227c679f5994699973b6274e3d6afe0
7
+ data.tar.gz: a8a61db572a00d9d0ca6d41f23d2c76490196c14a6fdd8202b88d1f924fd5f9a32ebbea0e7811a4fba2a49a6fcec4ac40242cd79dfbaec8f37deaff046650436
data/CHANGES.md CHANGED
@@ -1,3 +1,10 @@
1
+ # 0.1.4 (2022-07-20)
2
+
3
+ - Adds `URIs#safe_parse_uri`, which returns `nil` for invalid URLs (unlike `URIs#uri_or_nil`, which
4
+ raises `URI::InvalidURIError` for non-nil, non-parseable URLs)
5
+ - Updates documentation for `URIs#uri_or_nil` and `Validator#uri_or_nil` to clarify that
6
+ `nil` is an acceptable argument.
7
+
1
8
  # 0.1.3 (2022-05-11)
2
9
 
3
10
  - Adds `URIs#get_response`, which returns a complete `RestClient::Response` rather than just
@@ -7,7 +7,7 @@ module BerkeleyLibrary
7
7
  SUMMARY = 'Miscellaneous Ruby utilities for the UC Berkeley Library'.freeze
8
8
  DESCRIPTION = 'A collection of miscellaneous Ruby routines for the UC Berkeley Library.'.freeze
9
9
  LICENSE = 'MIT'.freeze
10
- VERSION = '0.1.3'.freeze
10
+ VERSION = '0.1.4'.freeze
11
11
  HOMEPAGE = 'https://github.com/BerkeleyLibrary/util'.freeze
12
12
  end
13
13
  end
@@ -6,10 +6,11 @@ module BerkeleyLibrary
6
6
  module Validator
7
7
  class << self
8
8
 
9
- # Returns the specified URL as a URI.
10
- # @param url [String, URI] the URL.
11
- # @return [URI] the URI.
12
- # @raise [URI::InvalidURIError] if `url` cannot be parsed as a URI.
9
+ # Returns the specified URL as a URI, or `nil` if the URI is `nil`.
10
+ # @param url [String, URI, nil] the URL.
11
+ # @return [URI] the URI, or `nil`.
12
+ # @raise [URI::InvalidURIError] if `url` is not `nil` and cannot be
13
+ # parsed as a URI.
13
14
  def uri_or_nil(url)
14
15
  return unless url
15
16
 
@@ -1,3 +1,4 @@
1
+ require 'berkeley_library/logging'
1
2
  require 'berkeley_library/util/uris/appender'
2
3
  require 'berkeley_library/util/uris/requester'
3
4
  require 'berkeley_library/util/uris/validator'
@@ -5,6 +6,8 @@ require 'berkeley_library/util/uris/validator'
5
6
  module BerkeleyLibrary
6
7
  module Util
7
8
  module URIs
9
+ include BerkeleyLibrary::Logging
10
+
8
11
  class << self
9
12
  include URIs
10
13
  end
@@ -42,13 +45,26 @@ module BerkeleyLibrary
42
45
  Requester.get_response(uri, params: params, headers: headers)
43
46
  end
44
47
 
45
- # Returns the specified URL as a URI.
46
- # @param url [String, URI] the URL.
47
- # @return [URI] the URI.
48
- # @raise [URI::InvalidURIError] if `url` cannot be parsed as a URI.
48
+ # Returns the specified URL as a URI, or `nil` if the URL is `nil`.
49
+ # @param url [String, URI, nil] the URL.
50
+ # @return [URI] the URI, or `nil`.
51
+ # @raise [URI::InvalidURIError] if `url` is not `nil` and cannot be
52
+ # parsed as a URI.
49
53
  def uri_or_nil(url)
50
54
  Validator.uri_or_nil(url)
51
55
  end
56
+
57
+ # Returns the specified URL as a URI, or `nil` if the URL cannot
58
+ # be parsed.
59
+ # @param url [Object, nil] the URL.
60
+ # @return [URI, nil] the URI, or `nil`.
61
+ def safe_parse_uri(url)
62
+ # noinspection RubyMismatchedArgumentType
63
+ uri_or_nil(url)
64
+ rescue URI::InvalidURIError => e
65
+ logger.warn("Error parsing URL #{url.inspect}", e)
66
+ nil
67
+ end
52
68
  end
53
69
  end
54
70
  end
@@ -178,5 +178,37 @@ module BerkeleyLibrary::Util
178
178
  end
179
179
  end
180
180
  end
181
+
182
+ describe :safe_parse_uri do
183
+ it 'returns a URI unchanged' do
184
+ uri = URI.parse('http://example.org/')
185
+ expect(URIs.safe_parse_uri(uri)).to be(uri)
186
+ end
187
+
188
+ it 'converts a string to a URI' do
189
+ url = 'http://example.org/'
190
+ expect(URIs.safe_parse_uri(url)).to eq(URI.parse(url))
191
+ end
192
+
193
+ it 'returns nil for nil' do
194
+ expect(URIs.safe_parse_uri(nil)).to be_nil
195
+ end
196
+
197
+ context 'invalid URL strings' do
198
+ it 'returns nil' do
199
+ bad_url = 'not a uri'
200
+ expect(URIs.safe_parse_uri(bad_url)).to be_nil
201
+ end
202
+
203
+ it 'logs a warning' do
204
+ logger = instance_double(Ougai::Logger)
205
+ allow(BerkeleyLibrary::Logging).to receive(:logger).and_return(logger)
206
+
207
+ bad_url = 'not a uri'
208
+ expect(logger).to receive(:warn).with(/#{bad_url}/, kind_of(URI::InvalidURIError))
209
+ expect(URIs.safe_parse_uri(bad_url)).to be_nil
210
+ end
211
+ end
212
+ end
181
213
  end
182
214
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: berkeley_library-util
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.3
4
+ version: 0.1.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - David Moles
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2022-05-11 00:00:00.000000000 Z
11
+ date: 2022-07-20 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: berkeley_library-logging