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,
lib/berkeley_library/util/uris/requester/class_methods.rb
Defined Under Namespace
Modules: Validator Classes: Appender, Requester
Constant Summary collapse
- UTF_8 =
Encoding::UTF_8
- ALLOWED_BYTES_BY_MODE =
TODO: extend to cover other modes - host, zone, path, password, query, fragment cf. https://github.com/golang/go/blob/master/src/net/url/url.go
{ path_segment: [0x24, 0x26, 0x2b, 0x3a, 0x3d, 0x40] # @ & = + $ }.freeze
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: {}, log: true) ⇒ String
Performs a GET request and returns the response body as a string.
-
#get_response(uri, params: {}, headers: {}, log: true) ⇒ RestClient::Response
Performs a GET request and returns the response, even in the event of a failed request.
-
#head(uri, params: {}, headers: {}, log: true) ⇒ Integer
Performs a HEAD request and returns the response status as an integer.
-
#head_response(uri, params: {}, headers: {}, log: true) ⇒ 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: {}, log: true) ⇒ String
Performs a GET request and returns the response body as a string.
37 38 39 |
# File 'lib/berkeley_library/util/uris.rb', line 37 def get(uri, params: {}, headers: {}, log: true) Requester.get(uri, params: params, headers: headers, log: log) end |
#get_response(uri, params: {}, headers: {}, log: true) ⇒ RestClient::Response
Performs a GET request and returns the response, even in the event of a failed request.
62 63 64 |
# File 'lib/berkeley_library/util/uris.rb', line 62 def get_response(uri, params: {}, headers: {}, log: true) Requester.get_response(uri, params: params, headers: headers, log: log) end |
#head(uri, params: {}, headers: {}, log: true) ⇒ Integer
Performs a HEAD request and returns the response status as an integer. Note that unlike BerkeleyLibrary::Util::URIs::Requester::ClassMethods#get, this does not raise an error in the event of an unsuccessful request.
50 51 52 |
# File 'lib/berkeley_library/util/uris.rb', line 50 def head(uri, params: {}, headers: {}, log: true) Requester.head(uri, params: params, headers: headers, log: log) end |
#head_response(uri, params: {}, headers: {}, log: true) ⇒ RestClient::Response
Performs a HEAD request and returns the response, even in the event of a failed request.
74 75 76 |
# File 'lib/berkeley_library/util/uris.rb', line 74 def head_response(uri, params: {}, headers: {}, log: true) Requester.head_response(uri, params: params, headers: headers, log: log) 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.
89 90 91 92 93 94 95 96 97 98 |
# File 'lib/berkeley_library/util/uris.rb', line 89 def path_escape(s) raise ArgumentError, "Can't escape #{s.inspect}: not a string" unless s.respond_to?(:encoding) s = s.encode(UTF_8) 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.
104 105 106 107 108 109 110 |
# File 'lib/berkeley_library/util/uris.rb', line 104 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.
83 84 85 |
# File 'lib/berkeley_library/util/uris.rb', line 83 def uri_or_nil(url) Validator.uri_or_nil(url) end |