Module: BerkeleyLibrary::Util::URIs
- Extended by:
- URIs
- Includes:
- Logging
- Included in:
- URIs
- Defined in:
- lib/berkeley_library/util/uris.rb,
lib/berkeley_library/util/uris/appender.rb,
lib/berkeley_library/util/uris/requester.rb,
lib/berkeley_library/util/uris/validator.rb
Defined Under Namespace
Modules: Requester, Validator Classes: Appender
Constant Summary collapse
- UTF_8 =
Encoding::UTF_8
Instance Method Summary collapse
-
#append(uri, *elements) ⇒ URI
Appends the specified paths to the path of the specified URI, removing any extraneous slashes and merging additional query parameters, and returns a new URI with that path and the same scheme, host, query, fragment, etc.
-
#get(uri, params: {}, headers: {}) ⇒ String
Performs a GET request and returns the response body as a string.
-
#get_response(uri, params: {}, headers: {}) ⇒ RestClient::Response
Performs a GET request and returns the response, even in the event of a failed request.
-
#head(uri, params: {}, headers: {}) ⇒ Integer
Performs a HEAD request and returns the response status as an integer.
-
#head_response(uri, params: {}, headers: {}) ⇒ RestClient::Response
Performs a HEAD request and returns the response, even in the event of a failed request.
-
#path_escape(s) ⇒ Object
Escapes the specified string so that it can be used as a URL path segment, replacing disallowed characters (including /) with percent-encodings as needed.
-
#safe_parse_uri(url) ⇒ URI?
Returns the specified URL as a URI, or
nilif the URL cannot be parsed. -
#uri_or_nil(url) ⇒ URI
Returns the specified URL as a URI, or
nilif the URL isnil.
Instance Method Details
#append(uri, *elements) ⇒ URI
Appends the specified paths to the path of the specified URI, removing any extraneous slashes and merging additional query parameters, and returns a new URI with that path and the same scheme, host, query, fragment, etc. as the original.
25 26 27 |
# File 'lib/berkeley_library/util/uris.rb', line 25 def append(uri, *elements) Appender.new(uri, *elements).to_uri end |
#get(uri, params: {}, headers: {}) ⇒ String
Performs a GET request and returns the response body as a string.
36 37 38 |
# File 'lib/berkeley_library/util/uris.rb', line 36 def get(uri, params: {}, headers: {}) Requester.get(uri, params: params, headers: headers) end |
#get_response(uri, params: {}, headers: {}) ⇒ RestClient::Response
Performs a GET request and returns the response, even in the event of a failed request.
59 60 61 |
# File 'lib/berkeley_library/util/uris.rb', line 59 def get_response(uri, params: {}, headers: {}) Requester.get_response(uri, params: params, headers: headers) end |
#head(uri, params: {}, headers: {}) ⇒ Integer
Performs a HEAD request and returns the response status as an integer. Note that unlike BerkeleyLibrary::Util::URIs::Requester.get, this does not raise an error in the event of an unsuccessful request.
48 49 50 |
# File 'lib/berkeley_library/util/uris.rb', line 48 def head(uri, params: {}, headers: {}) Requester.head(uri, params: params, headers: headers) end |
#head_response(uri, params: {}, headers: {}) ⇒ RestClient::Response
Performs a HEAD request and returns the response, even in the event of a failed request.
70 71 72 |
# File 'lib/berkeley_library/util/uris.rb', line 70 def head_response(uri, params: {}, headers: {}) Requester.head_response(uri, params: params, headers: headers) end |
#path_escape(s) ⇒ Object
Escapes the specified string so that it can be used as a URL path segment, replacing disallowed characters (including /) with percent-encodings as needed.
85 86 87 88 89 90 91 92 93 94 |
# File 'lib/berkeley_library/util/uris.rb', line 85 def path_escape(s) raise ArgumentError, "Can't escape #{s.inspect}: not a string" unless s.respond_to?(:encoding) raise ArgumentError, "Can't escape #{s.inspect}: expected #{UTF_8}, was #{s.encoding}" unless s.encoding == UTF_8 ''.tap do |escaped| s.bytes.each do |b| escaped << (should_escape?(b, :path_segment) ? '%%%02X' % b : b.chr) end end end |
#safe_parse_uri(url) ⇒ URI?
Returns the specified URL as a URI, or nil if the URL cannot
be parsed.
100 101 102 103 104 105 106 |
# File 'lib/berkeley_library/util/uris.rb', line 100 def safe_parse_uri(url) # noinspection RubyMismatchedArgumentType uri_or_nil(url) rescue URI::InvalidURIError => e logger.warn("Error parsing URL #{url.inspect}", e) nil end |
#uri_or_nil(url) ⇒ URI
Returns the specified URL as a URI, or nil if the URL is nil.
79 80 81 |
# File 'lib/berkeley_library/util/uris.rb', line 79 def uri_or_nil(url) Validator.uri_or_nil(url) end |